[LeetCode#136, 137]Single Number, Single Number 2
The question: Single Number
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?
My analysis:
This problem could be solved in two very tricky ways.
At here, I only use "XOR" method, and I would discuss the methond of using "digits counting" later.
The key idea: the property of XOR.
x ^ x = 0
x ^ y ^ x = y (the order could in random arrangement)
Thus for this problem. Since we have only one number appear once, other number apper perfectly twice.
we XOR all numbers in the array, and we would finally get the number that only appears once.
int ret = A[0];
for (int i = 1; i < A.length; i++) {
ret ^= A[i];
}
My solution:
public class Solution {
public int singleNumber(int[] A) {
if (A.length == 0 || A == null)
return 0;
int ret = A[0];
for (int i = 1; i < A.length; i++) {
ret ^= A[i];
}
return ret;
}
}
The question: 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?
My analysis:
This problem involvs many skills in mainpulating on a Integer.
The basic idea is:
The single number's each digit would only appear once, while other number's each digit would appear third times.
We count the appearance of each digts of all number in the array, then the digit does not appear times of three is a digit of the single number.
The skills in implementation:
1. how to get a interger's binary representation.
1.1 we need first have a interger array int[32] to record the appearance for each digit.
1.2 we use "digit" operator to move the target digit to the last digit, and use '&' to get the digit.
for (int i = 0; i < 32; i++) { //elegant while loop
for (int j = 0; j < A.length; j++) {
num[i] += (A[j] >> i) & 1;
}
}
Note: if we want to get the digit at i, we move it rightward i-1 digits. Note here num[i] is the counter the digits appear
at i+1 th position. 2. how to recover the integer from digits?
for (int i = 0; i < 32; i++) {
ret += (num[i] % 3) << i;//note the num[i] store the digit at position i+1.
}
My solution:
public class Solution {
public int singleNumber(int[] A) {
int[] num = new int[32];
int ret = 0;
for (int i = 0; i < 32; i++) {
for (int j = 0; j < A.length; j++) {
num[i] += (A[j] >> i) & 1;
}
}
for (int i = 0; i < 32; i++) {
ret += (num[i] % 3) << i;
}
return ret;
}
}
[LeetCode#136, 137]Single Number, Single Number 2的更多相关文章
- leetcode@ [136/137] Single Number & Single Number II
https://leetcode.com/problems/single-number/ Given an array of integers, every element appears twice ...
- leetcode 136 137 Single Number
题目描述(面试常考题) 借助了异或的思想 class Solution { public: int singleNumber(vector<int>& nums) { ; ; i ...
- Leetcode 136 137 260 SingleNumber I II III
Leetccode 136 SingleNumber I Given an array of integers, every element appears twice except for one. ...
- leetcode 136. Single Number 、 137. Single Number II 、 260. Single Number III(剑指offer40 数组中只出现一次的数字)
136. Single Number 除了一个数字,其他数字都出现了两遍. 用亦或解决,亦或的特点:1.相同的数结果为0,不同的数结果为1 2.与自己亦或为0,与0亦或为原来的数 class Solu ...
- leetcode 136 Single Number, 260 Single Number III
leetcode 136. Single Number Given an array of integers, every element appears twice except for one. ...
- LeetCode(137) Single Number II
题目 Given an array of integers, every element appears three times except for one. Find that single on ...
- LeetCode 136. Single Number(只出现一次的数字)
LeetCode 136. Single Number(只出现一次的数字)
- 【LeetCode】137. Single Number II 解题报告(Python)
[LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...
- [ActionScript 3.0] 用TextField的方法getCharIndexAtPoint(x:Number, y:Number):int实现文字在固定范围内显示
有时候我们遇到一行文字过多时必须固定文字的显示范围,但由于中英文所占字节数不一样,所以不能很好的用截取字符的方式去统一显示范围的大小,用TextField的getCharIndexAtPoint(x: ...
随机推荐
- VMware安装CentOS后网络设置
在使用CentOS虚拟机后,出现了无法上网的情况,使用主机ping虚机地址可以ping通,而虚机ping不通主机,同时虚机也无法ping通其他的网址或ip,显示内容为Network is unreac ...
- iOS的内存管理
在Objective-C 这种面向对象的语言里,内存管理是个重要的概念.要想用一门语言写出内存使用效率高而且又没有bug的代码,就得掌握其内存管理模型的种种细节. 一旦理解了这些规则,你就会发现,其实 ...
- .Net实现IO操作
IO操作需要的web.config里的节点配置 <configuration> <appSettings> <!--上传文件类型要求--> <a ...
- reactjs 入门
地址搜集:http://www.cocoachina.com/webapp/20150604/12035.html class 属性需要写成 className ,for 属性需要写成 htmlFor ...
- dnw for linux: Ubuntu下可用,无需编译驱动,mini2440可用
1.安装所需库文件 sudo apt-get install libusb-dev 2.源代码如下 /* dnw2 linux main file. This depends on libusb. * ...
- 表中查询重复的数据,如何通过sql语句查询?
1.最直观的思路:要知道所有名字有重复人资料,首先必须知道哪个名字重复了:select name from emp group by name having count(*)>1所有名字重复人的 ...
- OC - 30.如何封装自定义布局
概述 对于经常使用的控件或类,通常将其分装为一个单独的类来供外界使用,以此达到事半功倍的效果 由于分装的类不依赖于其他的类,所以若要使用该类,可直接将该类拖进项目文件即可 在进行分装的时候,通常需要用 ...
- iOS app闪退的一般原因
1.函数无限递归爆栈(表视图返回Cell和返回行高的方法互相调用)2.某对象无法解析某个方法(没做类型转换.或者代理没实现某个方法)3.访问了某个已经被释放的对象(ARC之后不太有)4.从Bundle ...
- mysql,left join on
转自http://www.oschina.net/question/89964_65912 觉得很有帮助,用来学习. 即使你认为自己已对 MySQL 的 LEFT JOIN 理解深刻,但我敢打赌,这篇 ...
- apache-php安装mysql简单方法
1.启用mysql功能,在php.ini中 extension=php_mysql.dll extension=php_mysqli.dll 2. 修改extension_dir = "ex ...