[leetcode]Summary Ranges

原创 2015年11月18日 14:31:03

Easy题,先上题目:

Given a sorted integer array without duplicates, return the summary of its ranges.

For example, given [0,1,2,4,5,7], return [“0->2”,”4->5”,”7”].

此题又暴露了思维不够严谨,从WA的两次原因就可以看出:

  1. 忽略了输入可能就一个值的可能;
  2. 忽略了输入可能为空的可能

    附上代码:

public class Solution {
    public List<String> summaryRanges(int[] nums) {
        List<String> resList = new ArrayList<String>();
             if(nums==null || nums.length<1) 
                 return  resList;
             int i, index = 0, len;
             String tmp = null;
             if(nums.length == 1){
                 tmp = nums[0] + "";
                 resList.add(tmp);
             }else{
                 len = 1;
                 for(i = 1; i < nums.length; i++){
                     if(nums[i] - nums[i - 1] != 1){
                         if(len > 1){
                             tmp = String.format("%d->%d", nums[index], nums[i - 1]);
                             resList.add(tmp);
                             len = 1;
                         }else{
                             tmp = nums[index] + "";
                             resList.add(tmp);
                         }
                         index = i;
                     }else{
                         len ++;
                     }
                 }
                 if(len == 1){
                     tmp = nums[i - 1] + "";
                     resList.add(tmp);
                 }else{
                     tmp = String.format("%d->%d", nums[index], nums[i - 1]);
                     resList.add(tmp);
                 }
             }       
             return resList;
    }
}

题目链接:https://leetcode.com/problems/summary-ranges/

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Leetcode_228_Summary Ranges

本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/46762039 Given a sorted in...

leetcode--Summary Ranges

题目: Given a sorted integer array without duplicates, return the summary of its ranges. For example...

【LeetCode OJ 228】Summary Ranges

题目链接:https://leetcode.com/problems/summary-ranges/ 题目: Given a sorted integer array without dupl...

Leetcode——228. Summary Ranges

题目Given a sorted integer array without duplicates, return the summary of its ranges.For example, giv...

LeetCode228——Summary Ranges

leetcode刷题日记——Summary Ranges 1.题目内容: Given a sorted integer array without duplicates, return the su...

228. Summary Ranges

题意: Given a sorted integer array without duplicates, return the summary of its ranges.For example, g...

Summary Ranges

题目名称 Summary Ranges—LeetCode链接描述 Given a sorted integer array without duplicates, return the summa...

228. Summary Ranges

Given a sorted integer array without duplicates, return the summary of its ranges. For example, giv...
  • iFuMI
  • iFuMI
  • 2016年03月22日 17:13
  • 201

Summary Ranges 问题解答

前段时间刷了几道leetcode上面的题,觉得比较有意思,但由于没有记录,所以过一段时间就忘记了。计划在这里对每刷的一道题都进行总结,本次的题如下:题目 Given a sorted integer...

Summary Ranges

1 题目描述 Given a sorted integer array without duplicates, return the summary of its ranges. For exa...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:[leetcode]Summary Ranges
举报原因:
原因补充:

(最多只允许输入30个字)