[LeetCode] 137. Single Number II 单独数 II
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:
Input: [2,2,3,2]
Output: 3
Example 2:
Input: [0,1,0,1,0,1,99]
Output: 99
解法:参考
Java:
public int singleNumber(int[] A) {
int ones = 0, twos = 0;
for(int i = 0; i < A.length; i++){
ones = (ones ^ A[i]) & ~twos;
twos = (twos ^ A[i]) & ~ones;
}
return ones;
}
Python:
class Solution(object):
def singleNumber(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
x1, x2, mask = 0, 0, 0
for i in nums:
x2 ^= x1 & i;
x1 ^= i;
mask = ~(x1 & x2);
x2 &= mask;
x1 &= mask;
return x1
C++:
class Solution {
public:
int singleNumber(vector<int>& nums) {
int one = 0, two = 0, three = 0;
for (int i = 0; i < nums.size(); ++i) {
two |= one & nums[i];
one ^= nums[i];
three = one & two;
one &= ~three;
two &= ~three;
}
return one;
}
};
C++:
class Solution {
public:
int singleNumber(vector<int>& nums) {
int a = 0, b = 0;
for (int i = 0; i < nums.size(); ++i) {
b = (b ^ nums[i]) & ~a;
a = (a ^ nums[i]) & ~b;
}
return b;
}
};
类似题目:
[LeetCode] 136. Single Number 单独数
[LeetCode] 260. Single Number III 单独数 III
All LeetCode Questions List 题目汇总
[LeetCode] 137. Single Number II 单独数 II的更多相关文章
- [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 ...
- LeetCode 137. Single Number II(只出现一次的数字 II)
LeetCode 137. Single Number II(只出现一次的数字 II)
- Leetcode 137 Single Number II 仅出现一次的数字
原题地址https://leetcode.com/problems/single-number-ii/ 题目描述Given an array of integers, every element ap ...
- LeetCode 137 Single Number II(仅仅出现一次的数字 II)(*)
翻译 给定一个整型数组,除了某个元素外其余的均出现了三次. 找出这个元素. 备注: 你的算法应该是线性时间复杂度. 你能够不用额外的空间来实现它吗? 原文 Given an array of inte ...
- [LeetCode] 137. Single Number II 单独的数字之二
Given a non-empty array of integers, every element appears three times except for one, which appears ...
- Leetcode 137. Single Number I/II/III
Given an array of integers, every element appears twice except for one. Find that single one. 本题利用XO ...
- 详解LeetCode 137. Single Number II
Given an array of integers, every element appears three times except for one, which appears exactly ...
- LeetCode 137 Single Number II 数组中除了一个数外,其他的数都出现了三次,找出这个只出现一次的数
Given an array of integers, every element appears three times except for one, which appears exactly ...
- leetcode 137. Single Number II ----- java
Given an array of integers, every element appears three times except for one. Find that single one. ...
随机推荐
- 如何查看已购买的office密钥
登陆这个网址https://account.microsoft.com/services/ 声明 :转载请注明来源sogeisetsu.cnblogs.com
- JQuery DOM操作(属性操作/样式操作/文档过滤)
jQuery——入门(三)JQuery DOM操作(属性操作/样式操作/文档过滤) 一.DOM属性操作 1.属性 (1).attr() 方法 语法:$(selector).attr(name|prop ...
- 大数据开发keras框架环境配置小结
系统安装问题 win10+ubuntu16.04 在win10在需要security boot设置成disable,否则安装完后无法设置启动项. 安装完ubuntu重启,系统会直接进入win10,需要 ...
- Gym-100648B: Hie with the Pie(状态DP)
题意:外卖员开始在0号节点,有N个人点了外卖,(N<=10),现在告诉两两间距离,问怎么配送,使得每个人的外卖都送外,然后回到0号点的总时间最短,注意,同一个点可以多次经过. 思路:TSP问题( ...
- 记录一次编译安装Pg_rman缺少依赖包的问题
系统版本:CentOS版本6.10(最终版) pg_rman:https://github.com/ossc-db/pg_rman -bash-4.1$ makegcc -Wall -Wmissing ...
- Random Walk——高斯消元法
题目 有一个 $N \times M$ 大小的格子,从(0, 0)出发,每一步朝着上下左右4个格子中可以移动的格子等概率移动.另外有些格子有石头,因此无法移至这些格子.求第一次到达 $(N-1, M- ...
- ES6对象的个人总结
属性初始值的简写: 当一个对象的属性与本地变量同名时,不需要再写冒号和值,直接写属性名即可 let fullName = '杨三', age = 19; let obj = { fullName: f ...
- 使用gitstats分析git 仓库代码
gitstats 是一个很不错的git 代码提交分析工具,可以帮助我们生成图表统计结果 工具文档信息 gitstats http://gitstats.sourceforge.net/ 安装 使用ce ...
- Myschool试题
题目: 1.查询所有学生记录,包含年级名称2.查询S1年级下的学生记录 一.com.myschool.dao 1 BaseDao package com.myschool.dao; import ja ...
- mysql 字符类以及重复元字符
字符类 [:alnum:]=[a-zA-Z0-] [:alpha:]=[a-zA-Z] [:digit:]=[-] [:lower:]=[a-z] [:upper:]=[A-Z] [:xdigit:] ...