# My java easy understanding solution

• ``````public List<String> summaryRanges(int[] nums) {
List<String> result = new ArrayList<String>();
if(nums.length == 0) return result;
int start = nums[0];
int end = nums[0];
for(int i = 1; i < nums.length; i++){
if(nums[i] != nums[i - 1] + 1){
else result.add(String.valueOf(start) + "->" + String.valueOf(end));
start = nums[i];
end = nums[i];
}else{
end = nums[i];
}
}
else result.add(String.valueOf(start) + "->" + String.valueOf(end));
return result;
}``````

• ``````public class Solution {
public List<String> summaryRanges(int[] nums) {
List<String> results = new ArrayList<String>();
if (nums.length == 0) return results;

int beginVal = nums[0];
for (int i = 1; i < nums.length; i++) {
if (nums[i] + 0L - nums[i-1] > 1) {
beginVal = nums[i];
}
}

return results;
}

private String constructRange(int beginVal, int endVal) {
if (beginVal == endVal) {
return beginVal + "";
}
return beginVal + "->" + endVal;
}
}``````

• what's with the 0L thing ?

• it casts integer to long

• I know that it got casted to Long, but I didn't get: why do you convert it to a long when the differene (1) could be handled by an int ?

• what if num[i] is 'INTEGER.MAX_VALUE' and num[i-1] is 'INTEGER.MIN_VALUE', would that be overflow situation?

• got it, so you could have done it the other way around, like:
if(num[i]!=num[i-1]+1), without converting it to Long, right ?

• You can try with the test set. I believe it would still be overflow.

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