美文网首页
[leetcode]single number

[leetcode]single number

作者: 这是朕的江山 | 来源:发表于2016-07-27 19:41 被阅读11次

    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?

    解题思路:使用异或,数组中相同的数两两异或全部为0,最后剩下的就是那个落单的数。

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

    相关文章

      网友评论

          本文标题:[leetcode]single number

          本文链接:https://www.haomeiwen.com/subject/gnnojttx.html