Accepted clean Java solution (two pointers)


  • 1
    public List<String> summaryRanges(int[] a) {
      List<String> res = new ArrayList<String>();
      
      if (a == null) return res;
          
      int i = 0, n = a.length;
      
      for (int j = 1; j < n; j++) {
        if ((long)a[j] - (long)a[j - 1] > 1) {
          // found a range
          res.add(getRange(a[i], a[j - 1]));
          i = j;
        }
      }
      
      // final check
      if (i < n) res.add(getRange(a[i], a[n - 1]));
      
      return res;
    }
    
    String getRange(int n1, int n2) {
      return (n1 == n2) ? String.valueOf(n1) : String.format("%d->%d", n1, n2);
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.