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?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
class Solution { public: int missingNumber(vector<int>& nums) { int *book = new int [nums.size() + 1]; for(int i = 0; i <= nums.size(); i++) { book[i] = 0; } for(int i = 0; i < nums.size(); i++) { book[nums[i]] = 1; } for(int i = 0; i <= nums.size(); i++) { if(book[i] == 0) { return i; } } } }; |
❤ 点击这里 -> 订阅《PAT | 蓝桥 | LeetCode学习路径 & 刷题经验》by 柳婼