Leetcode Single Number
2015-05-12 18:19
387 查看
题目:
Given an array of integers, every element appears twice except for one. Find that single one.Note:
Your algorithm should have a linear run time complexity. Could you implement it without using extra memory?
分析:
这道题既用线性的时间复杂度也不用额外的存储空间,使用一个技巧,位异或。如果数是成对出现的,那么它们的异或结果是0,而任何数和0按位异或的结果是它本身。走一趟最后所有数的异或结果就是这个单独出现的数。Java代码实现:
public class Solution {public int singleNumber(int[] nums) {
int result = 0;
for(int i=0;i<nums.length;i++)
result = result ^ nums[i];
return result;
}
}
相关文章推荐
- Leetcode 136 Single Number 仅出现一次的数字
- leetcode 135: Single Number
- leetcode 136. Single Number
- leetcode_c++:哈希:Single Number(136)
- leetcode —— single number
- LeetCode-Easy部分标签为HashTable#136 Single Number
- LeetCode:Single Number
- 4000 leetcode 136.Single Number
- LeetCode - Single number- Jan 31, 2015
- LeetCode:Single NumberⅡ
- LeetCode 136 -Single Number ( JAVA )
- Single Number 【leetcode】1分钟解题系类
- leetcode136 Single Number136 Java
- LeetCode 136. Single Number
- leetcode_single number
- leetcode[136]Single Number
- Single Number(leetcode)
- [LeetCode] Single Number
- [Leetcode] 136. Single Number
- Single Number---leetcode c# solution