leetcode Single Number II

原创 2013年12月02日 11:02:51

Single Number II

 Total Accepted: 4273 Total Submissions: 14218My Submissions

Given an array of integers, every element appears three times except for one. Find that single one.

Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?


Similar to I, when the number accumulates for three times, set the bit to 0. I.e., how to simulate a ternary system using a binary system. 

I wrote an wrong code like :

class Solution {
 public:
  int singleNumber(int A[], int n) {
    int ones = 0, twos = 0, threes = 0, i, res = 0;
    if (n == 1)
      return A[0];
    for (i = 0; i < n; ++i) {
      res |= A[i];
      threes = twos & A[i];
      res ^= threes;
      
      twos = (twos | (ones & A[i])) ^ threes;
      ones = ones ^ A[i];
    }
    return res;
  }
};

The mistake is that the variable ones needs to be cleared based on the variable threes. At the same time, the variable res seems redundant. A right code is :

class Solution {
 public:
  int singleNumber(int A[], int n) {
    int ones = 0, twos = 0, threes = 0, i, res = 0;
    if (n == 1)
      return A[0];
    for (i = 0; i < n; ++i) {
      //res |= A[i];
      threes = twos & A[i];
      //res ^= threes;
      
      twos = (twos | (ones & A[i])) & (~threes);
      ones = (ones ^ A[i]) & (~threes);
    }
    return ones;
  }
};
However, the code seems not in a standard way. I tried to fix the code in an accessible code:

class Solution {
 public:
  int singleNumber(int A[], int n) {
    int ones = 0, twos = 0, threes = 0, mask = -1, i, res = 0;
    if (n == 1)
      return A[0];
    for (i = 0; i < n; ++i) {
      //res |= A[i];
      threes = twos & A[i];
      //res ^= threes;
      mask = (~threes);
      twos = (twos | (ones & A[i])) & mask;
      mask &= (~twos);
      ones = (ones | A[i] ) & mask;
    }
    return ones;
  }
};

At last, the problem could be changed like this:

Given an array of integers, every element appears m times except for one for n times. Find that the one of n times.

  int findNumber(int A[], int size, int m, int n) {
    int mask = -1, i, j;
	vector<int> times(m+1,0);
	
	for (i = 0; i < size; ++i) {
	  mask = times[0] = -1;
	  times[m] = 0;
	  for (j = m; j >= 1; --j) {
	    times[j] = (times[j] | (times[j-1] & A[i])) & mask;
		mask &= (~times[j]);
	  }
	}
	return times[n];
  }



相关文章推荐

[leetcode] Single Number II

转自:http://www.tuicool.com/articles/UjQV7n 题目描述: 给定一个包含n个整数的数组,除了一个数出现一次外所有的整数均出现三次,找出这个只出现一次...

[题解][LeetCode][Single Number II]

题目: 题解: Code: class Solution: # @param A, a list of integer ...

LeetCode(137) Single Number II

题目Given an array of integers, every element appears three times except for one. Find that single one...
  • fly_yr
  • fly_yr
  • 2015年10月22日 13:19
  • 297

【leetcode78】Single Number II

题目描述:给定一个数组,里面除了一个数字,其他的都出现三次。求出这个数字原文描述:Given an array of integers, every element appears three tim...

【LeetCode】137. Single Number II

137. Single Number II Given an array of integers, every element appears three times except for one...

[Leetcode]Single Number II

Given an array of integers, every element appears three times except for one. Find that single one. ...

leetcode:Single Number II

题目: Given an array of integers, every element appears three times except for one. Find that sin...

leetcode Single Number II

http://www.cnblogs.com/daijinqiao/p/3352893.html PS:对于这种作者,孤只想默默点个赞 题目描述:给定一个包含n个整数的数组,除了一个数出现...
  • youqika
  • youqika
  • 2013年11月26日 10:52
  • 485

Leetcode Single Number II

转载自:http://www.cnblogs.com/daijinqiao/p/3352893.html 对于除出现一次之外的所有的整数,其二进制表示中每一位1出现的次数是3的整数倍,将所有这些1清...

LeetCode - Single Number II分析

原题如下: "Given an array of integers, every element appears three times except for one. Find that sin...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode Single Number II
举报原因:
原因补充:

(最多只允许输入30个字)