136. 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 using extra memory?

链接: http://leetcode.com/problems/single-number/

题解:

位运算,用异或处理掉出现两次的数字即可。

Time Complexity - O(n), Space Complexity - O(1)。

public class Solution {

    public int singleNumber(int[] A) {

        int result = 0;

        

        for(int i : A){

            result ^= i;

        }

        

        return result;

    }

}

 

测试:

你可能感兴趣的:(number)