Single Number

Single Number

问题:

Given an array of integers, every element appears twice except for one. Find that single one.

思路:

异或

我的方法:

public class Solution {

    public int singleNumber(int[] A) {

        int result = 0 ;

        for(int i = 0 ; i < A.length; i ++)

        {

            result ^= A[i] ;

        }

        return result ;

    }

}
View Code

别人的方法:

public class Solution {

    public int singleNumber(int[] A) {

        if(A == null || A.length == 0) {

            return -1;

        }

        int rst = 0;

        for (int i = 0; i < A.length; i++) {

            rst ^= A[i];

        }

        return rst;

    }

}
View Code

学习之处:

命名用rst比较简练

你可能感兴趣的:(number)