[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: ...
随机推荐
- Objective-c Category使用
Objective-c Category使用 转载:http://blog.csdn.net/lovefqing/article/details/8289851 什么是Category Catego ...
- [转] JavaScript 原型理解与创建对象应用
这段时间把之前的 JavaScript 的笔记复习了一遍,又学习了一些新的内容,所以把自己的学习笔记加上个人理解在这里总结一下,并提供一个简单的应用示例,希望能帮助一些刚入门的朋友.主 要参考< ...
- css考核点整理(十二)-能描述下你在项目中都用到了哪些符合逐渐增强和优雅降级的理念的技巧吗
能描述下你在项目中都用到了哪些符合逐渐增强和优雅降级的理念的技巧吗
- Oracler读取各种格式的相关日期格式
CREATE OR REPLACE Package Pkg_Stm_Date As --Purpose:相关日期处理功能包 --获取某一天是第几周 Function ...
- Python开发【第二十篇】:缓存
Python开发[第二十篇]:缓存redis&Memcache 点击这里 Python之路[第九篇]:Python操作 RabbitMQ.Redis.Memcache.SQLAlchemy ...
- js中浮点型运算 注意点
先看张图: 这是一个JS浮点数运算Bug,导致我树状图,数据合计不正确,,,,,,两个小数相加,出来那么多位小数 (这是修该之后的) 网上找到以下解决方式: 方法一:有js自定义函数 <sc ...
- Gprinter Android SDK V2.0 使用说明
佳博特约经销商,此店购买的打印机问题优先解决哟 https://shop107172033.taobao.com/index.htm?spm=2013.1.w5002-9520741823.2.V1p ...
- An App ID with Identifier 'xxxxxx’ is not available. Please ....
1.完全关闭Xcode; 2.找到钥匙串,将钥匙串(Keychain)中的对应证书移除: 3.再次打开Xcode,通过 Preferences - Account 4. 删除原先的账号重新登录, 搞定 ...
- C#入门经典(第五版)学习笔记(三)
---------------面向对象编程简介--------------- UML表示方法: 1)方框上中下三分 2)上框写类名 3)中框写属性和字段,例如:+Description:string ...
- JSP JS 日期控件的下载、使用及注意事项
网上流行的时间日期控件比较多,个人觉得My97DatePicker的日期控件不错,值得推荐. 具体的使用过程如下: 1.下载My97DatePicker.rar或 My97DatePickerBeta ...