现在的位置: 首页 > 综合 > 正文

LeetCode – Single Number

2019年04月17日 ⁄ 综合 ⁄ 共 290字 ⁄ 字号 评论关闭

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;
    }
}

抱歉!评论已关闭.