首页 > 其他 > 详细

Single Number

时间:2015-10-06 15:16:06      阅读:229      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!