[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: ...
随机推荐
- mysql 异常处理实例
1. 语法: DECLARE handler_action HANDLER FOR condition_value [, condition_value] ... statement handler_ ...
- 他们都没告诉你适配 Android N 需要注意什么
还记得 6.0 对 Apache Http 库的废除导致的应用崩溃吗?还记得 6.0 中 MAC id 始终返回为空导致的唯一 id 混合生成算法大幅失效吗? 1. Android 中 Java 的实 ...
- 移动端 设置 小于12px 字体 初探
1.移动端字号规范 2. 百度字号调研 3. 绕过12px 限制 4. 缩放 5. chrome 字号
- 第三篇:python高级之生成器&迭代器
python高级之生成器&迭代器 python高级之生成器&迭代器 本机内容 概念梳理 容器 可迭代对象 迭代器 for循环内部实现 生成器 1.概念梳理 容器(container ...
- HTML的Get方法URL传递中文参数,解决乱码问题
本例中有使用JQuery. 资料参考:http://www.cnblogs.com/babycool/p/3169058.html 发送的HTML页面代码: <!DOCTYPE html> ...
- TFS 服务器更换后工作区无法绑定
需要删除工作区,删除命令如下 tf workspace /delete 工作区名;创建的用户 /server:TFS服务器 例 tf workspace /delete WHQ-PC;whq /ser ...
- tomcat+nginx+redis实现均衡负载、session共享(二)
今天我们接着说上次还没完成session共享的部分,还没看过上一篇的朋友可以先看下上次内容,http://www.cnblogs.com/zhrxidian/p/5432886.html. 1.red ...
- NSString NSCFString区别
NSString 是 NSCFString的父类 在于NSString是个class cluster,一个类簇.什么是一个类簇?简单的来说,NSString是个“工厂类”,然后它在外层提供了很多方法接 ...
- Spring与Jdbc Demo
方法一:继承JdbcTemplate来实现 1.配置applicationContext <!-- 获取数据源连接 dbcp --> <bean id="dataSourc ...
- SGU 158.Commuter Train
一道简单题. 火车停的位置不是在整点就是在二分之一点,坐标*2,然后枚举火车停的位置,计算总距离即可. code: #include <iostream> #include <cma ...