Given a sorted integer array where the range of elements is [lower, upper] inclusive, return its missing ranges.
For example, given [0, 1, 3, 2,--], lower = 0 and upper =, return ["", "4->49", "51->74", "76->99"].
Thinking Analysis: This problem is basically to investigate the application of double pointers, starting from lower-1, traversing the nums array to find missing range, you can use the pre and cur a previous two pointer movement implementation. Note Lower and upper processing, lower can be larger than the smallest number of nums, upper can be smaller than the largest number in nums, so consider to be thoughtful.
Refer to Code (title in book)
Public list<string> findmissingranges (int[] nums, int lower, int upper) { list<string> res = new ArrayList <String> (); int pre = LOWER-1; for (int i = 0; i < nums.length; i++) { if (i = = nums.length) { cur = upper + 1; } else cur = nums[i]; if (Cur-pre >= 2) { int missstart = pre + 1; int missend = cur-1; if (missend = = Missstart) res.add (missend); else Res.add (Missstart + "+" + missend); Pre = cur; } else { pre = cur; } } return res; }
Leetcode Missing Ranges [leetcode book problem]