[Leetcode] Single Number
2016-09-28 12:40
323 查看
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?
利用两个相同的数异或等于0,还有异或交换律。
public class Solution {
public int singleNumber(int[] nums) {
int num=0;
int len=nums.length;
while(len>0)
{num=num^nums[len-1];
len--;
}
return num;
}
}
Note:
Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
利用两个相同的数异或等于0,还有异或交换律。
public class Solution {
public int singleNumber(int[] nums) {
int num=0;
int len=nums.length;
while(len>0)
{num=num^nums[len-1];
len--;
}
return num;
}
}
相关文章推荐
- [LeetCode] Single Number
- Single Number 【leetcode】1分钟解题系类
- Leetcode Single Number
- leetcode[136]Single Number
- [LeetCode]136. Single Number
- [Leetcode] 136. Single Number
- leetcode:Single Number
- Single Number---leetcode c# solution
- LeetCode 136. Single Number
- [leetcode] 136. Single Number
- 【LeetCode刷题记录】Single Number
- leetcode--single number
- LeetCode-E-Single Number
- 【Leetcode】Single Number 系列问题
- LeetCode——Single Number
- leetcode笔记:Single Number 异或的使用
- [LeetCode]: 136:Single Number
- LeetCode, Single Number
- [LeetCode] Single Number
- LeetCode 136. Single Number