(LeetCode OJ) Missing Number (268]
268. Missing NumberMy Submissions QuestionTotal Accepted: 31740 Total Submissions: 83547 Difficulty: Medium
Given an array containing n distinct numbers taken from0, 1, 2, ..., n, Find the one that is missing from the array.
For example,
Given nums =[0, 1, 3]Return2.
Note:
Your algorithm shocould run in linear runtime complexity. cocould 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.
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
// First of all: // I have taken this question, and I have misunderstood the question. Why do I think I did it! // Later I realized that some numbers were randomly selected from 0 to size (), and one of them was lost. // others' algorithms: Mathematical introduction, 0 to size () minus the current array and sumclass Solution {public: int missingNumber (vector
& Nums) {int sum = 0; for (int num: nums) sum + = num; int n = nums. size (); return (n * (n + 1)/2-sum ;}};