LeetCode - Single Number

来源:互联网 发布:java手机游戏编程 编辑:程序博客网 时间:2024/05/18 04:01

The kind of problem when you have no idea, it is very difficult. But once you know how to make it, it is very simple. Just use the XOR operation and go over the array. The numbers show up twice will disappear eventually.


public class Solution {    public int singleNumber(int[] A) {        int ans = A[0];                for(int i=1;i<A.length;i++)        {        ans ^= A[i];        }                return ans;    }}


0 0