# Using binary search but worst case O(n)

• ``````public class Solution {

class Range {
int start;
int end;

Range(int s, int e) {
start = s;
end = e;
}
}

public List<String> summaryRanges(int[] nums) {

List<String> resStr = new ArrayList<>();

if (nums.length == 0) {
return resStr;
}

List<Range> res = new ArrayList<>();
helper(nums, 0, nums.length - 1, res);

for (Range r : res) {
if (r.start == r.end) {
} else {
}
}

return resStr;
}

private void helper(int[] nums, int i, int j, List<Range> res) {
if (i == j || nums[j] - nums[i] == j - i) {
return;
}

int m = (i + j) / 2;

helper(nums, i, m, res);
helper(nums, m + 1, j, res);
}

private void add2res(int a, int b, List<Range> res) {
if (res.isEmpty() || res.get(res.size() - 1).end != a - 1) {
} else {
res.get(res.size() - 1).end = b;
}
}
``````

}

• Nice and clean. Didn't cross my mind to use binary search.

• I used the Binary Search too. But when I saw every discuss I felt like I overcomplicated it...

``````    public class Solution {
public List<String> summaryRanges(int[] nums) {
List<String> ans=new ArrayList<>();
if(nums.length==0) return ans;
int low=0;
int high=nums.length-1;
int index=0;
while(index<nums.length){
while(high>low){
int mid=1+(high+low)/2;
if(mid-index==nums[mid]-nums[index]){
low=mid;
}else{
high=mid-1;
}
}