LeetCode之Missing Number
生活随笔
收集整理的這篇文章主要介紹了
LeetCode之Missing Number
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、題目
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.
2、代碼實現
public class Solution {public int missingNumber(int[] nums) {if (nums == null) {return 0;}int result = 0;Map<Integer, Integer> map = new HashMap<Integer, Integer>();for (int i = 0; i < nums.length; ++i) {map.put(nums[i], 2);}for (int i = 0; i <= nums.length; ++i) {Integer in = map.get(i);if (in == null) {return i;}}return result;}
} ?
3、注意的地方
這個地方需要注意 for (int i = 0; i <= nums.length; ++i) {Integer in = map.get(i);if (in == null) {return i;}} 一定要記得寫 i <= nums.length ,不是i < length總結
以上是生活随笔為你收集整理的LeetCode之Missing Number的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LeetCode之Find the Di
- 下一篇: LeetCode之Find All Nu