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

LeetCode——Single Number

时间:2015-06-25 00:06:10      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:

Description:

Given an array of integers, every element appears twice except for one. Find that single one.

找出只出现一次的数。

public class Solution {
    public int singleNumber(int[] nums) {
        Map<Integer, Integer> map = new HashMap<Integer, Integer>();
        
        for(int i=0; i<nums.length; i++) {
            if(map.containsKey(nums[i])) {
                int times = map.get(nums[i]);
                map.put(nums[i], times+1);
            }
            else
                map.put(nums[i], 1);
        }
        Set<Integer> set = map.keySet();
        for(int i : set) {
            if(map.get(i) == 1) {
                return i;
            }
        }
        return 0;
        
    }
}

 

LeetCode——Single Number

标签:

原文地址:http://www.cnblogs.com/wxisme/p/4598908.html

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