Single Number I&& II——还没看,倒过头来再看
2015-06-16 19:57
281 查看
Single Number I
Given an array of integers, every element appears twice except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
直接异或解决。
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?
分析1
由于int型由32bit表示,因此可以用一个长度为32的int数组保存各个比特位上1出现的次数。最后,将数组各元素对3取余,那么32位数组就纪录了只出现了一次的整数的二进制表示。
分析2
我们也没必要开 int bit[32]的数组去统计的
我们来模拟二进制加法
用两位来计数,到3就把该位清零。
bit2 bit1
bit1 如果该位来的是1 ,保持0,1,0,1。。。(也就是xor,异或),如果是0就不变
当bit1=1的时候再来一个1,那么bit2=1
当这两个bit同时为1,说明这是3啦(二进制你想想嘛),该位清零。
分析3:
其实就是设置三个标志位,出现一次标志位1对应的bit变为1,出现两次标志位2对应的bit变为1,出现三次标志位三对应的bit变为1.
理解了这种思路,代码也就不难写了。
Given an array of integers, every element appears twice except for one. Find that single one.
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
直接异或解决。
class Solution { public: int singleNumber(int A[], int n) { int res=0; for(int i=0;i<n;i++) { res=res^A[i]; } return res; } };
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?
分析1
由于int型由32bit表示,因此可以用一个长度为32的int数组保存各个比特位上1出现的次数。最后,将数组各元素对3取余,那么32位数组就纪录了只出现了一次的整数的二进制表示。
public class SingleNumberII { private final static int INTEGER_DIGITS = 32; public int singleNumber(int[] A) { if (A == null || A.length <= 0) { return 0; } int ret = 0; int[] count = new int[INTEGER_DIGITS]; for (int i = 0; i < INTEGER_DIGITS; ++i) { for (int j = 0; j < A.length; ++j) { count[i] += (A[j] >> i) & 0x0001; } ret |= (count[i] % 3) << i; } return ret; } }
分析2
我们也没必要开 int bit[32]的数组去统计的
我们来模拟二进制加法
用两位来计数,到3就把该位清零。
bit2 bit1
bit1 如果该位来的是1 ,保持0,1,0,1。。。(也就是xor,异或),如果是0就不变
当bit1=1的时候再来一个1,那么bit2=1
当这两个bit同时为1,说明这是3啦(二进制你想想嘛),该位清零。
class Solution { public: int singleNumber(int A[], int n) { int ones = 0, twos = 0, threes = 0; for(int i = 0; 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是只出现一次的数 } };
分析3:
其实就是设置三个标志位,出现一次标志位1对应的bit变为1,出现两次标志位2对应的bit变为1,出现三次标志位三对应的bit变为1.
理解了这种思路,代码也就不难写了。
class Solution { public: int singleNumber(vector<int>& nums) { int one=0; int two=0; int 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|two; } };
相关文章推荐
- pt-online-schema-change 修改字段属性产生死锁
- 彻底搞定C语言指针详解完整版
- 微软100题(91)智力题
- Android 中对于图片的内存优化方法
- LeetCode:Reverse LinkedList
- data_struction_test3:二叉查找树,查找值最小的结点
- 查看linux的版本
- 第12章-图形用户接口(2)---内部类
- HBuilder:一个不错的web前端IDE(代码编辑器)
- LeetCode 111 Minimum Depth of Binary Tree
- 浅析Scanf源码
- servletContext
- Android应用程序实现欢迎引导页面的方法实现
- Unity3d_数组类型
- Java如何对HashMap按值进行排序
- 重新理解计算机(一)
- leetcode--Best Time to Buy and Sell Stock II
- 如何成为编程大牛
- webservice客户端 发生undefined element declaration 's:schema'
- Tar.gz加密解密总结