首页 > 其他 > 详细

(lleetcode)Single Number

时间:2015-10-01 23:03:56      阅读:348      评论:0      收藏: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?

1)使用map

class Solution {
public:
    int singleNumber(vector<int>& nums) {
        unordered_map<int,int> ret;
        int i =0;
        while(i < nums.size())
        {
            ret[nums[i]]++;
            i++;
        }
        for(unordered_map<int,int>::iterator it = ret.begin();it != ret.end(); ++it)
        {
            if(it->second == 1) 
            {
                return it->first;break;    
            }
        }
    }
};

 

(lleetcode)Single Number

原文:http://www.cnblogs.com/chdxiaoming/p/4851630.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!