[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. ...
随机推荐
- rhce 考试题目总结
rhce 考试题目总结归类 开机需要做的事: 检查系统版本 配置yum源 修改selinux的模式 ping一下server机器 1.分区类题目 1.1 rhcsa 第十五题 添加swap分区 要点: ...
- Codeforces Round #560 (Div. 3) Microtransactions
Codeforces Round #560 (Div. 3) F2. Microtransactions (hard version) 题意: 现在有一个人他每天早上获得1块钱,现在有\(n\)种商品 ...
- SVN (TortioseSVN) 版本控制之忽略路径(如:bin、obj)
在SVN版本控制时,新手经常会遇到这样的问题: 1.整个项目一起提交时会把bin . gen . .project 一同提交至服务器 2.避免提交编译.本地配置等文件在项目中单独对src.res进行提 ...
- 学习Microsoft Visio(3)
流程图的规范及技巧 一.流程图绘制基本要求 二.流程图绘制规范要点 在进行流程图的绘制过程中,要有一条明晰的流程主线,从而使得流程图脉络更加清晰. 通常来讲,流程图要以开始任务为起点,完成任务为终点. ...
- 前端性能----页面渲染(DOM)
CSS会阻塞渲染树的构建,不阻塞DOM构建,但是在CSSOM构建完成之前,页面不会开始渲染(一片空白),CSSOM构建完成后,页面将会显示出内容. DOM(Document Object Model) ...
- BZOJ1113 海报PLA1(单调栈入门题)
一,自己思考下 1,先自己思考下 N个矩形,排成一排,现在希望用尽量少的海报去cover住它们. 2,不懂. 着实不懂. 3,分析下,最优性问题对吧,然后就每什么想法了.. 虽然肯定和单调栈和单调队列 ...
- IIS 站点配置文件
IIS 站点配置文件 C:/Windows/System32/inetsrv/config/applicationHost.config 配置文件示例: <system.application ...
- async、await总结
一.async用法 async作为一个关键字放到函数前面,用于表示函数是一个异步函数.异步函数也就意味着该函数的执行不会阻塞后面代码的执行. 异步函数语法很简单,就是在函数前面加上async 关键字, ...
- 洛谷P1076 寻宝
寻宝 模拟加优化,细节比较重要. 我们用ti[i]表示i这一层有楼梯的个数,然后我们把当前1号点的数据mod上ti[i],然后使该数不能等于0,就行了. #include <bits/stdc+ ...
- 微信小程序七夕节礼物
VSCode Node.js HbuilderX 安装前端开发环境 [外链图片转存失败(img-aXUJRfXc-1565136341881)(https://upload-images.jiansh ...