Java [Leetcode 137]Single Number II
Posted 王小二的小博
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java [Leetcode 137]Single Number II相关的知识,希望对你有一定的参考价值。
题目描述:
Given an array of integers, every element appears three times except for one. Find that single one.
解题思路:
具体参考Detailed explanation and generalization of the bitwise operation method for single numbers
讲的实在是很全面,而且拓展到了一般的情况,膜!!
代码如下:
public class Solution{ public int singleNumber(int[] nums){ int x1 = 0; int x2 = 0; int mask = 0; for(int i : nums){ x2 ^= x1 & i; x1 ^= i; mask = ~(x1 & x2); x2 &= mask; x1 &= mask; } return x1; } }
以上是关于Java [Leetcode 137]Single Number II的主要内容,如果未能解决你的问题,请参考以下文章
LeetCode136 Single Number, LeetCode137 Single Number II, LeetCode260 Single Number III