LeetCode | Single Number II【转】
题目:
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?
思路:
一个比较好的方法是从位运算来考虑。每一位是0或1,由于除了一个数其他数都出现三次,我们可以检测出每一位出现三次的位运算结果再加上最后一位即可。
class Solution {
public:
int singleNumber(int A[], int n) {
int ones = , twos = , threes = ;
for(int i = ; i < n; i++)
{
threes = twos & A[i]; //已经出现两次并且再次出现
twos = twos | ones & A[i]; //曾经出现两次的或者曾经出现一次但是再次出现的
ones = ones | A[i]; //出现一次的 twos = twos & ~threes; //当某一位出现三次后,我们就从出现两次中消除该位
ones = ones & ~threes; //当某一位出现三次后,我们就从出现一次中消除该位
}
return ones; //twos, threes最终都为0.ones是只出现一次的数
}
};
LeetCode | Single Number II【转】的更多相关文章
- [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(找出数组中只出现一次的数2)
问题: Given an array of integers, every element appears three times except for one. Find that single o ...
- LeetCode:Single Number II
题目地址:here 题目大意:一个整数数组中,只有一个数出现一次,其余数都出现3次,在O(n)时间,O(1)空间内找到这个出现一次的数 对于”只有一个数出现一次,其余数出现2次“的情况,很简单,只要把 ...
- [leetcode]Single Number II @ Python
原题地址:http://oj.leetcode.com/problems/single-number-ii/ 题意:Given an array of integers, every element ...
- [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 single one. ...
- Leetcode Single Number II (面试题推荐)
还记得<剑指offer>和<编程之美>等书上多次出现的找一个数组中仅仅出现一次的数那个题吗? leetcode也有这道题 链接here 相信大家都知道用异或在O(n)的时间复 ...
- LeetCode——Single Number II
Description: Given an array of integers, every element appears three times except for one. Find that ...
- leetcode Single Number II - 位运算处理数组中的数
题目描述: 给定一个包含n个整数的数组,除了一个数出现一次外所有的整数均出现三次,找出这个只出现一次的整数. 题目来源: http://oj.leetcode.com/problems/single- ...
随机推荐
- XAML Region标签功能
XAML本身没有Region标签功能,很郁闷.现在有插件可以实现该功能了! <!-- Region (Any Text You Want) --> Your Code <!-- ...
- 【转】Xcelsius2008 水晶易表问题 部分汇总
要使用 Xcelsius 2008,需要安装 Adobe Flash 吗? 若要正常运行 Xcelsius 2008,必须安装 Adobe Flash Player 版本 9.如果在安装过程中没有安装 ...
- oracle中利用trigger,sequence自动生成ID
http://zhanghong.iteye.com/blog/865937 1. 首先创建数据库表 SQL> create table customer( 2 id number(8) no ...
- A Simple C++ Template Class that Matches a String to a Wildcard Pattern
A recently implemented enhanced wildcard string matcher, features of which including, Supporting wil ...
- ACM Binary String Matching
Binary String Matching 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 Given two strings A and B, whose alp ...
- ACM: A Simple Problem with Integers 解题报告-线段树
A Simple Problem with Integers Time Limit:5000MS Memory Limit:131072KB 64bit IO Format:%lld & %l ...
- To Do List
妈呀...发现不发博文公布自己要学的东西压力少太多了.......... 然后就会变得颓废..................... 求大家监督QAQ....To Do List是近3天左右目标,3天 ...
- 透过现象看现象(SQL501N错误处理)
某日一直运行比较正常的报表系统,突然报存储过程执行失败,查看DB2 错误返回码为sql501n,查看此错误原因如下: [db2inst1@limt ~]$ db2 ? sql501n SQL0501N ...
- 51nod算法马拉松12
A 第K大区间 不妨考虑二分答案x,则问题转化成计算有多少个区间满足众数出现的次数>=x. 那么这个问题我们使用滑动窗口,枚举右端点,则左端点肯定单调递增,然后维护一个简单的数组就能资瓷添加元素 ...
- Bouncy Castle内存溢出
现象: 堆内存溢出,java.lang.OutOfMemoryError: Java heap space 用jmap查看,显示 num #instances #bytes ...