leetcode 136. Single Number
Given a non-empty 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?
Example 1:
Input:
Output: 1
Example 2:
Input:
Output: 4
class Solution {
public int singleNumber(int[] nums) {
if(nums.length == 0) return 0;
if(nums.length == 1) returnnums;
Arrays.sort(nums);
for(int i = 1; i< nums.length;i++ ){
if(nums == nums) continue;
else{
if(i == nums.length-1) return nums;
if(i == 1) return nums;
if(nums == nums) i++;
else{ return nums; }
}
}
return 0;
}
}
class Solution {
public int singleNumber(int[] nums) {
int re = 0;
for(int i = 0 ; i < nums.length; i++) re = re ^ nums;
return re;
}
}
页:
[1]