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

Missing Number

时间:2015-10-07 12:06:39      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:

 

 

Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array.

For example,
Given nums = [0, 1, 3] return 2.

Note:
Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity?

Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.

 

HashMap!

 1     public int missingNumber(int[] nums) 
 2     {
 3         Map<Integer,Integer> map = new HashMap<>();
 4         for(int i = 0; i < nums.length;i++)
 5         {
 6             map.put(nums[i],nums[i]);
 7         }
 8         
 9         int j;
10         for(j=0;j<nums.length;j++)
11         {
12             if(!map.containsKey(j))
13             break;
14         }
15         
16         return j;
17     }

 

Missing Number

标签:

原文地址:http://www.cnblogs.com/hygeia/p/4858411.html

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