# Accepted JAVA solution--easy to understand

• ``````List<String> list=new ArrayList();
if(nums.length==1){
return list;
}
for(int i=0;i<nums.length;i++){
int a=nums[i];
while(i+1<nums.length&&(nums[i+1]-nums[i])==1){
i++;
}
if(a!=nums[i]){
}else{
}
}
return list;``````

• good solution, but i think the first part checking length==1 is not necessary

• @magicalcan
checking length==1 is required. otherwise:
==> nums[i+1]-nums[i])==1
would give NULL POINTER EXCEPTION.

• @tajinder No, it's not necessary because && has a short-cut effect. If length == 1, i+1 < length is false and the condition checking will not check the second part.

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

String start=nums[0]+"";
String end=start; int k=0;
for(int i=1;i<nums.length;i++){
if(nums[i]-nums[i-1]==1) {
end=nums[i]+"";

continue;
}
else{

if(!start.equals(end))
else

end=start=nums[i]+"";
}
}

if(!start.equals(end))
else
return ls;
}
}``````

• +1
length == 1 check is unnecessary.

• I prefer one loop, although yours is also O(n)

• Thanks for sharing! Similar idea but I think mine is a little simpler. :) https://discuss.leetcode.com/topic/55491/very-easy-java-10-line-solution

• ``````public class Solution {
public List<String> summaryRanges(int[] nums) {
List<String> ans = new ArrayList<String>();
for(int i=0;i<nums.length;) {
int start = i;
while(i+1<nums.length&&nums[i+1]-nums[i]==1) {
i++;
}
if(nums[i]>nums[start]) {
}else if(nums[i]==nums[start]) {
}
i++;
}
return ans;
}
}
``````

• This post is deleted!

• Thanks for sharing.
BTW, I've tried the corner case, which adds the condition that the nums array is null, and it was also accepted.
I wonder why OJ accept your solution here.

if(nums == null || nums.length ==1) {
return list;
}

However, if I add the array nums <= 1, that will show error.
"java.lang.ArrayIndexOutOfBoundsException: 0"

• @magicalcan
Yes, u r right! That's my coding habit! When special case happen, always deal with it first. When length was 1, the code after first return won't ex! I assume this will reduce the load of the memory stack, I am not sure my thought was right.

• @Taj.trojan when i = 1 while(i+1<nums.length) will never ex..

• @sgoogs The nums = null and the nums.lenght =0 is different. Here the OJ will accept because the input don't include null nums, input will include empty arrays.

And, if you add nums <=1, when nums.length = 0 but not null! the nums[0] will give this error.

• @sgoogs The nums = null and the nums.lenght =0 is different. Here the OJ will accept because the input don't include null nums, input will include empty arrays.

And, if you add nums <=1, when nums.length = 0 but not null! the nums[0] will give this error.

Thanks! Do you mean that in this problem, we don't need to consider the null condition, please?

• @sgoogs No,no, always think the 'null' was good habit. For the problem, the input test case didn't include null. So you can pass! if you test in local, the null will give null exception in java

• @tanchiyuxx that's what I think. I will consider it anytime like you consider the 1 condition. LOL , even though 1 is not necessary here.
Thanks! : )

• Thanks for your sharing, and this is my c++ code with your method.

``````class Solution {
public:
vector<string> summaryRanges(vector<int>& nums) {
vector<string> res;
for (int i = 0; i < nums.size(); ++i) {
int num = nums[i];
while (i + 1 < nums.size() && nums[i] == nums[i + 1] - 1)
++i;
if (nums[i] != num)
res.push_back(to_string(num) + "->" + to_string(nums[i]));
else
res.push_back(to_string(nums[i]));
}
return res;
}
};
``````

• Similar solution with a single loop:

``````public List<String> summaryRanges(int[] nums) {
List<String> results = new ArrayList<>();
for (int start = 0, end = 1; end <= nums.length; end++)
if ((end == nums.length) || ((1 + nums[end - 1]) != nums[end])) {
String suffix = (1 + start != end) ? "->" + nums[end - 1] : "";
start = end;
}
return results;
}
``````

• Thanks.
Here is my python code:

``````class Solution(object):
def summaryRanges(self, nums):
"""
:type nums: List[int]
:rtype: List[str]
"""
res = []
i = 0
while i < len(nums):
temp = nums[i]
while i < len(nums)-1 and nums[i+1] - nums[i] == 1:
i += 1
if temp != nums[i]:
res.append(str(temp) + "->" + str(nums[i]))
else:
res.append(str(temp))
i += 1

return res
``````

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