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 137:Single Number II

Given an array of integers, every element appears three times except for one. Find that single one. ...
  • sunao2002002
  • sunao2002002
  • 2015年06月01日 22:36
  • 5026

【LeetCode-面试算法经典-Java实现】【137-Single Number II(只字出一次的数字II)】

【137-Single Number II(只出现一次的数字II)】【LeetCode-面试算法经典-Java实现】【所有题目目录索引】原题  Given an array of integers, ...
  • DERRANTCM
  • DERRANTCM
  • 2015年08月18日 06:24
  • 4191

LeetCode || Single Number II

Single Number II  Total Accepted: 12144 Total Submissions: 37399My Submissions Given an ...
  • jiadebin890724
  • jiadebin890724
  • 2014年04月09日 23:35
  • 18977

LeetCode(137)Single NumberII

题目如下: Given an array of integers, every element appears three times except for one. Find that single...
  • feliciafay
  • feliciafay
  • 2014年02月09日 10:15
  • 3035

LeetCode题解–137. Single Number II

链接LeetCode题目:https://leetcode.com/problems/single-number-ii/难度:Medium题目 Given an array of integers...
  • sinat_14867719
  • sinat_14867719
  • 2017年04月13日 15:32
  • 178

LeetCode--single-number-ii

题目描述 Given an array of integers, every element appears threetimes except for one. Find ...
  • wl_ss
  • wl_ss
  • 2018年01月20日 21:01
  • 51

LeetCode:Single Number I & II & III

(一)题目描述:single-number Given an array of integers, every element appears **twice** except for one. Fi...
  • u011489043
  • u011489043
  • 2017年03月21日 14:26
  • 267

leetcode single number II

题目为:给定一个数组,所有的数都出现3次,只有一个数出现一次 有两种用位运算的解法,我尝试去理解它们,用一个不是很懂位运算的人的思路。如果有错误的地方,还望指教~~ 第一种: //one tw...
  • donggua_fu
  • donggua_fu
  • 2017年03月02日 09:02
  • 134

LeetCode137. Single Number II

137. Single Number II 1、原题 Given an array of integers, every element appears three times except for...
  • u010217031
  • u010217031
  • 2017年04月16日 02:24
  • 73

leetcode_middle_41_137. Single Number II

题意: 给一个整数数组,所有数字都出现过三次,只有一个数字只出现过一次,找出这个数字。 分析:...
  • pusude
  • pusude
  • 2017年02月13日 21:39
  • 60
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:leetcode Single Number II
举报原因:
原因补充:

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