【leetcode】【137】Single Number II
2015-11-10 16:55
148 查看
1.问题描述
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?
2.问题分析
这个题与Single Number 是一个系列的,都是进行的位运算。但是问题I只需要进行异或操作就可以完成,但这个题显然没有这么简单。我参考了Discuss点击打开链接,里面的分析还是非常有用的我贴出主要的In Single Number, it is easy to think of XOR solution because XOR manipulation has such properties:
Commutative: A^B == B^A, this means XOR applies to unsorted arrays just like sorted. (1^2^1^2==1^1^2^2)
Circular: A^B^...^B == A where the count of B's is a multiple of 2.
So, if we apply XOR to a preceding zero and then an array of numbers, every number that appears twice will have no effect on the final result. Suppose there is a number H which appears just once, the final XOR result will be 0^H^...H where H appears as many
as in input array.
When it comes to Single Number II (every one occurs K=3 times except one occurs M times, where M is not a multiple of K), we need a substitute of XOR (notated as @) which satisfies:
Commutative: A@B == B@A.
Circular: A@B@...@B == A where the count of B's is a multiple of K.
We need to MAKE the @ operation. This general solution suggests that we maintain a state for each bit, where the state corresponds to how many '1's have appeared on that bit, so we need a int[32] array.
这可以从本质上解决这类题,但是链接里的第一个回答,我还是没有弄明白
3.Java AC代码
public int singleNumber(int[] nums) { int k = 3; int res = 0; int[] bitCount = new int[32]; for (int i = 0; i < bitCount.length; i++) { bitCount[i] = 0; } for (int i = 0; i < nums.length; i++) { int tmp = nums[i]; for (int j = 0; j < bitCount.length; j++) { int hasBit = tmp & (1 << j); if (hasBit != 0) { bitCount[j] = (bitCount[j] + 1) % k; } } } for (int i = 0; i < bitCount.length; i++) { if (bitCount[i] > 0) { res |= (1 << i); } } return res; }
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树
- [原创]java局域网聊天系统