LeetCode[136] 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?

思路:

直接用异或搞定,简单粗暴效率高。

代码:

public class Solution {
public int singleNumber(int[] nums) {
int result = 0;
for(int i = 0;i < nums.length;i++)
result ^= nums[i];
return result;
}
}

欢迎关注公众号: FullStackPlan 获取更多干货

Copyright © 2016 - 2017 LBD All Rights Reserved.

访客数 : | 访问量 :