码迷,mamicode.com
首页 > 其他好文 > 详细

Single Number

时间:2015-10-06 15:17:44      阅读:125      评论:0      收藏:0      [点我收藏+]

标签:

  Given an array of integers, every element appears twice except for one. Find that single one.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 result = 0;
        for(int num: nums)
        {
            result^=num;
        }
        return result;
    }
}

 

Single Number

标签:

原文地址:http://www.cnblogs.com/hemoely/p/4857071.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!