(LeetCode OJ) Missing Number (268]

Source: Internet
Author: User

(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 ;}};
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.