leetcode | Single Number
555 ワード
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?
これは主にビット演算における異和オペレータを考慮し,a^b=b^a,a^a=0,0^a=aに基づいてすべての数を異和し,最後に1回しか現れない数字を得ることができる.
Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?
これは主にビット演算における異和オペレータを考慮し,a^b=b^a,a^a=0,0^a=aに基づいてすべての数を異和し,最後に1回しか現れない数字を得ることができる.
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;
}
}