<leetcode oj>missing Number "268"

Source: Internet
Author: User

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"

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.