Skip to content

Instantly share code, notes, and snippets.

@cixuuz
Created October 4, 2017 15:13
Show Gist options
  • Select an option

  • Save cixuuz/1230642313f811ed623dc4b969f9170f to your computer and use it in GitHub Desktop.

Select an option

Save cixuuz/1230642313f811ed623dc4b969f9170f to your computer and use it in GitHub Desktop.
[163. Missing Ranges] #leetcode
public class Solution {
public List<String> findMissingRanges(int[] nums, int lower, int upper) {
List<String> res = new ArrayList<>();
for(int i : nums) {
if(i > lower) res.add(lower+((i-1 > lower)?"->"+(i-1):""));
if(i == upper) return res; // Avoid overflow
lower = i+1;
}
if(lower <= upper) res.add(lower + ((upper > lower)?"->"+(upper):""));
return res;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment