顿搜
飞过闲红千叶,夕岸在哪
类目归类
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?
Input: 14,14,23,35,23
Ouput: 35
public int singleNumber(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
int result = nums[0];
for (int i = 1; i < nums.length; i++) {
result ^= nums[i];
}
return result;
}public class LeetCode0136 {
public int singleNumber(int[] nums) {
if (nums == null || nums.length == 0) {
return 0;
}
int result = nums[0];
for (int i = 1; i < nums.length; i++) {
result ^= nums[i];
}
return result;
}
public static void main(String[] args) {
LeetCode0136 leetcode = new LeetCode0136();
int[] nums = { 14, 14, 23, 35, 23 };
System.out.println(leetcode.singleNumber(nums));
}
}