/* * 163.Missing Ranges * 2016-6-3 by Mingyang * 自己的代码未贴出来。原理就是只要这个是count发的下一个,就只加count, * 否则就把count加了以后再加自己 */
public List<String> findMissingRanges(int[] a, int low, int high) { List<String> res = new ArrayList<String>(); int next = low; for (int i = 0; i < a.length; i++) { if (a[i] < next) continue; if (a[i] == next) { next++; continue; } res.add(getRange(next, a[i] - 1)); next = a[i] + 1; } if (next <= high) res.add(getRange(next, high)); return res; } public String getRange(int n1, int n2) { return (n1 == n2) ? String.valueOf(n1) : String.format("%d->%d", n1, n2); }