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的更多相关文章

  1. leetcode@ [136/137] Single Number & Single Number II

    https://leetcode.com/problems/single-number/ Given an array of integers, every element appears twice ...

  2. leetcode 136 137 Single Number

    题目描述(面试常考题) 借助了异或的思想 class Solution { public: int singleNumber(vector<int>& nums) { ; ; i ...

  3. Leetcode 136 137 260 SingleNumber I II III

    Leetccode 136 SingleNumber I Given an array of integers, every element appears twice except for one. ...

  4. leetcode 136. Single Number 、 137. Single Number II 、 260. Single Number III(剑指offer40 数组中只出现一次的数字)

    136. Single Number 除了一个数字,其他数字都出现了两遍. 用亦或解决,亦或的特点:1.相同的数结果为0,不同的数结果为1 2.与自己亦或为0,与0亦或为原来的数 class Solu ...

  5. leetcode 136 Single Number, 260 Single Number III

    leetcode 136. Single Number Given an array of integers, every element appears twice except for one. ...

  6. LeetCode(137) Single Number II

    题目 Given an array of integers, every element appears three times except for one. Find that single on ...

  7. LeetCode 136. Single Number(只出现一次的数字)

    LeetCode 136. Single Number(只出现一次的数字)

  8. 【LeetCode】137. Single Number II 解题报告(Python)

    [LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...

  9. [ActionScript 3.0] 用TextField的方法getCharIndexAtPoint(x:Number, y:Number):int实现文字在固定范围内显示

    有时候我们遇到一行文字过多时必须固定文字的显示范围,但由于中英文所占字节数不一样,所以不能很好的用截取字符的方式去统一显示范围的大小,用TextField的getCharIndexAtPoint(x: ...

随机推荐

  1. javascript中涉及到汉字的比较

    在使用js中的"=="进行字符串的比较时,发现在英文情况下是ok的,但在中文比较时则不行了. 在网上搜索,提供了一个解决方法,使用 stringObject.localeCompa ...

  2. Visual Studio 调试技巧 (二)-- 为中断设置条件

    今天尽是干货.我们来讨论如何为中断设置条件吧. 就像习大大讲的精确扶贫一样,如果我们能很精确地,仅在需要的时候把断点命中,以查看这个时候的程序数据,我们就能显著地提高 Debug 的效率.为断点设置条 ...

  3. jad批量反编译class和jadeclipse集成到eclipse的设置方法

    安装jad配置 1.从http://varaneckas.com/jad/下载windows版本的jad.exe 2.安装完毕后配置jad的系统环境变量 批量解压jar和class文件 1.使用7zi ...

  4. 第三篇:python高级之生成器&迭代器

    python高级之生成器&迭代器   python高级之生成器&迭代器 本机内容 概念梳理 容器 可迭代对象 迭代器 for循环内部实现 生成器 1.概念梳理 容器(container ...

  5. (转)webstorm快捷键

    Ctrl+N 查找类 Ctrl+Shift+N 查找文件 Ctrl+Alt+L 格式化代码 Ctrl+Alt+O 优化导入的类和包 Alt+Insert 生成代码(如get,set方法,构造函数等) ...

  6. 转载:修改xshell中文乱码的问题(管用)

    执行echo $LANG命令输出的是当前的编码方式,执行locale命令得到系统中所有可用的编码方式.要让Xshell不显示乱码,则要将编码方式改为UTF-8. 在Xshell中[file]-> ...

  7. (转)VS2012网站发布详细步骤

    2.弹出网站发布设置面板,点击<新建..>,创建新的发布配置文件: 4. 在配置中,要选择“Release”——发布模式(Release   称为发布版本,它往往是进行了各种优化,使得程序 ...

  8. PHP 正则表达式匹配 preg_match 与 preg_match_all 函数

    --http://www.5idev.com/p-php_preg_match.shtml 正则表达式在 PHP 中的应用 在 PHP 应用中,正则表达式主要用于: 正则匹配:根据正则表达式匹配相应的 ...

  9. 表中查询重复的数据,如何通过sql语句查询?

    1.最直观的思路:要知道所有名字有重复人资料,首先必须知道哪个名字重复了:select name from emp group by name having count(*)>1所有名字重复人的 ...

  10. xcode7 icon图标设置