Single Number LeetCode java

it2024-07-15  65

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 usingextra memory?分析异或,不仅能处理两次的情况,只要出现偶数次,都可以清零.

异或  x^x=0  ,    x^0=x

代码

1 // LeetCode, Single Number 2 class Solution { 3 public static int singleNumber(int A[]) { 4 for(int i=1;i<A.length;i++){ 5 A[i]^=A[i-1]; 6 } 7 return A[A.length-1]; 8 }

 

转载于:https://www.cnblogs.com/ncznx/p/9167965.html

最新回复(0)