美文网首页
136. Single Number

136. Single Number

作者: juexin | 来源:发表于2017-01-09 18:29 被阅读0次

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?

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

相关文章

网友评论

      本文标题:136. Single Number

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