268. Missing Numbermy SubmissionsQuestionTotal Accepted: 31740 Total Submissions: 83547 Difficulty: Medium
Given an array containing n distinct numbers taken from 0, 1, 2, ..., n
, find the one and that's missing from the array.
For example,
Given nums = [0, 1, 3]
return 2
.
Note:
Your algorithm should run in linear runtime complexity. Could implement it using only constant extra space complexity?
Credits:
Special thanks to @jianchao. Li.fighter for adding the problem and creating all test cases.
Subscribe to see which companies asked this question
Hide TagsArray Math Bit manipulationHide Similar Problems(h) First Missing Positive (M) Single number (h) Find the Duplicate number
Thought first://I really took the problem, test instructions understand wrong, I thought it was doing it! Later only to understand that the original is random from 0 to size () selected some number, which has a lost, grass//Other people's algorithm: mathematical introduction, 0 to the sum of size () minus the current array and Sumclass solution {public: int Missingnumber (vector<int>& nums) { int sum = 0; for (int num:nums) sum + = num; int n = nums.size (); Return (n * (n + 1))/2-sum; }};
<leetcode oj>missing Number "268"