Given a non-empty array of integers, every element appears three times except for one, which appears exactly once. Find that single one.

Note:

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

Example 1:

  1. Input: [2,2,3,2]
  2. Output: 3

Example 2:

  1. Input: [0,1,0,1,0,1,99]
  2. Output: 99

解法:参考

Java:

  1. public int singleNumber(int[] A) {
  2. int ones = 0, twos = 0;
  3. for(int i = 0; i < A.length; i++){
  4. ones = (ones ^ A[i]) & ~twos;
  5. twos = (twos ^ A[i]) & ~ones;
  6. }
  7. return ones;
  8. }  

Python:

  1. class Solution(object):
  2. def singleNumber(self, nums):
  3. """
  4. :type nums: List[int]
  5. :rtype: int
  6. """
  7. x1, x2, mask = 0, 0, 0
  8. for i in nums:
  9. x2 ^= x1 & i;
  10. x1 ^= i;
  11. mask = ~(x1 & x2);
  12. x2 &= mask;
  13. x1 &= mask;
  14. return x1  

C++:

  1. class Solution {
  2. public:
  3. int singleNumber(vector<int>& nums) {
  4. int one = 0, two = 0, three = 0;
  5. for (int i = 0; i < nums.size(); ++i) {
  6. two |= one & nums[i];
  7. one ^= nums[i];
  8. three = one & two;
  9. one &= ~three;
  10. two &= ~three;
  11. }
  12. return one;
  13. }
  14. };

C++:

  1. class Solution {
  2. public:
  3. int singleNumber(vector<int>& nums) {
  4. int a = 0, b = 0;
  5. for (int i = 0; i < nums.size(); ++i) {
  6. b = (b ^ nums[i]) & ~a;
  7. a = (a ^ nums[i]) & ~b;
  8. }
  9. return b;
  10. }
  11. };

  

  

类似题目:

[LeetCode] 136. Single Number 单独数

[LeetCode] 260. Single Number III 单独数 III

All LeetCode Questions List 题目汇总

[LeetCode] 137. Single Number II 单独数 II的更多相关文章

  1. [LeetCode] 260. Single Number III 单独数 III

    Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...

  2. LeetCode 137. Single Number II(只出现一次的数字 II)

    LeetCode 137. Single Number II(只出现一次的数字 II)

  3. Leetcode 137 Single Number II 仅出现一次的数字

    原题地址https://leetcode.com/problems/single-number-ii/ 题目描述Given an array of integers, every element ap ...

  4. LeetCode 137 Single Number II(仅仅出现一次的数字 II)(*)

    翻译 给定一个整型数组,除了某个元素外其余的均出现了三次. 找出这个元素. 备注: 你的算法应该是线性时间复杂度. 你能够不用额外的空间来实现它吗? 原文 Given an array of inte ...

  5. [LeetCode] 137. Single Number II 单独的数字之二

    Given a non-empty array of integers, every element appears three times except for one, which appears ...

  6. Leetcode 137. Single Number I/II/III

    Given an array of integers, every element appears twice except for one. Find that single one. 本题利用XO ...

  7. 详解LeetCode 137. Single Number II

    Given an array of integers, every element appears three times except for one, which appears exactly ...

  8. LeetCode 137 Single Number II 数组中除了一个数外,其他的数都出现了三次,找出这个只出现一次的数

    Given an array of integers, every element appears three times except for one, which appears exactly ...

  9. leetcode 137. Single Number II ----- java

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

随机推荐

  1. rhce 考试题目总结

    rhce 考试题目总结归类 开机需要做的事: 检查系统版本 配置yum源 修改selinux的模式 ping一下server机器 1.分区类题目 1.1 rhcsa 第十五题 添加swap分区 要点: ...

  2. Codeforces Round #560 (Div. 3) Microtransactions

    Codeforces Round #560 (Div. 3) F2. Microtransactions (hard version) 题意: 现在有一个人他每天早上获得1块钱,现在有\(n\)种商品 ...

  3. SVN (TortioseSVN) 版本控制之忽略路径(如:bin、obj)

    在SVN版本控制时,新手经常会遇到这样的问题: 1.整个项目一起提交时会把bin . gen . .project 一同提交至服务器 2.避免提交编译.本地配置等文件在项目中单独对src.res进行提 ...

  4. 学习Microsoft Visio(3)

    流程图的规范及技巧 一.流程图绘制基本要求 二.流程图绘制规范要点 在进行流程图的绘制过程中,要有一条明晰的流程主线,从而使得流程图脉络更加清晰. 通常来讲,流程图要以开始任务为起点,完成任务为终点. ...

  5. 前端性能----页面渲染(DOM)

    CSS会阻塞渲染树的构建,不阻塞DOM构建,但是在CSSOM构建完成之前,页面不会开始渲染(一片空白),CSSOM构建完成后,页面将会显示出内容. DOM(Document Object Model) ...

  6. BZOJ1113 海报PLA1(单调栈入门题)

    一,自己思考下 1,先自己思考下 N个矩形,排成一排,现在希望用尽量少的海报去cover住它们. 2,不懂. 着实不懂. 3,分析下,最优性问题对吧,然后就每什么想法了.. 虽然肯定和单调栈和单调队列 ...

  7. IIS 站点配置文件

    IIS 站点配置文件  C:/Windows/System32/inetsrv/config/applicationHost.config 配置文件示例: <system.application ...

  8. async、await总结

    一.async用法 async作为一个关键字放到函数前面,用于表示函数是一个异步函数.异步函数也就意味着该函数的执行不会阻塞后面代码的执行. 异步函数语法很简单,就是在函数前面加上async 关键字, ...

  9. 洛谷P1076 寻宝

    寻宝 模拟加优化,细节比较重要. 我们用ti[i]表示i这一层有楼梯的个数,然后我们把当前1号点的数据mod上ti[i],然后使该数不能等于0,就行了. #include <bits/stdc+ ...

  10. 微信小程序七夕节礼物

    VSCode Node.js HbuilderX 安装前端开发环境 [外链图片转存失败(img-aXUJRfXc-1565136341881)(https://upload-images.jiansh ...