[leetcode#163] Missing Ranges

Source: Internet
Author: User

problem:

Given a sorted integer array where the range of elements is [lower, Upper] inclusive, return its missin G Ranges.

For example, given [0, 1, 3, 50, 75] , lower = 0 and upper =, return["2", "4->49", "51->74", "76->99"].

General Analysis:

This kind problem are easy, it just test your proramming skills. Basic idea:according to the problem, the gap between and Numbers:nums[i], nums[i+1] should be recorded. There could be following situation:------------------------------------------------------------------------------------------------ Case1:no Gap (Nums[i] + 1 = = Nums[i+1]) We need to the record nothing.------------------------------------------------------------------------------------------------ Case2:the gap ' s length is 1. (Nums[i] + 2 = nums[i+1]) We need to record one number. Nums[i]+1.------------------------------------------------------------------------------------------------ Case3:the gap ' s length is larger than 1. (Nums[i] + 2 < nums[i+1]) We need to record the range. [Nums[i]+1, Nums[i+1]-1]. Write in ThisThe IS-a little ugly, what's more we had lower and upper must be included. We could use of variables for this purpose: (to compute the gap between nums[i-1] and Nums[i])--------------------------- ---------------------------------------------------------------------after:the number after nums[i-1]pre:the number before nums[i]------------------------------------------------------------------------------------------------if(Pre = =After ) Ret.add (pre+ "");Else if(Pre >After ) Ret.add ( after+ "+" +pre);= Nums[i] + 1; Skill:to involve the lower and upper, we could assign"After" with "lower" before scanning the nums. and assign "pre" with "upper"After the scan.intafter =Lower;intPre; for(inti = 0; i < nums.length; i++) {...} Pre=Upper;if(Pre = =After ) Ret.add (pre+ "");Else if(Pre >After ) Ret.add ( after+ "+" + Pre);

Wrong solution:

 Public classSolution { PublicList<string> Findmissingranges (int[] Nums,intLowerintUpper) {        if(Nums = =NULL)            Throw NewIllegalArgumentException ("Nums is null"); List<String> ret =NewArraylist<string> (); if(Nums.length = = 0) {String temp= lower + "+" +Upper;            Ret.add (temp); returnret; }        intafter =Lower; intPre;  for(inti = 0; i < nums.length; i++) {Pre= Nums[i]-1; if(Pre = =After ) Ret.add (pre+ ""); Else if(Pre >After ) Ret.add ( after+ "+" +pre); after= Nums[i] + 1; } Pre=Upper; if(Pre = =After ) Ret.add (pre+ ""); Else if(Pre >After ) Ret.add ( after+ "+" +pre); returnret; }}

MistakeAnalysis:

Error Case:[], 1, 1output:["1->1"]expected:["1"]mistake Analysis:i has failed to consdier the corner CaseWhen nums.length = 0, and lower and upper share the same value. Thus We should not use",". Fix:if(Lower <Upper) {String temp= lower + "+" +Upper; Ret.add (temp);} Else{ret.add (lower+ "");} Lesson:when the output should be genereated forDifferent format (like "Num1", "Num1-num2"), you should is careful with your handling with corner Case.

Solution:

 Public classSolution { PublicList<string> Findmissingranges (int[] Nums,intLowerintUpper) {        if(Nums = =NULL)            Throw NewIllegalArgumentException ("Nums is null"); List<String> ret =NewArraylist<string> (); if(Nums.length = = 0) {            if(Lower <Upper) {String temp= lower + "+" +Upper;            Ret.add (temp); } Else{ret.add (lower+ ""); }            returnret; }        intafter =Lower; intPre;  for(inti = 0; i < nums.length; i++) {Pre= Nums[i]-1; if(Pre = =After ) Ret.add (pre+ ""); Else if(Pre >After ) Ret.add ( after+ "+" +pre); after= Nums[i] + 1; } Pre=Upper; if(Pre = =After ) Ret.add (pre+ ""); Else if(Pre >After ) Ret.add ( after+ "+" +pre); returnret; }}

[leetcode#163] Missing Ranges

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.