leetcode 228: Summary Ranges

原创 2015年06月26日 06:22:18

Summary Ranges

Total Accepted: 511 Total Submissions: 2271

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"].

[思路]

两个指针 start, end.  如果nums[end+1] = nums[end]+1, 就移动end指针, 否则, 插入字符串nums[start]->nums[end].

[CODE]

public class Solution {
    // [0,1,2,4,5,7], return ["0->2","4->5","7"]. 
    public List<String> summaryRanges(int[] nums) {
        List<String> res = new ArrayList<>();
        if(nums==null || nums.length<1) return  res;
        
        int s=0, e=0;
        while(e<nums.length) {
            if(e+1<nums.length && nums[e+1]==nums[e]+1) {
                e++;
            } else {
                if(s==e) {
                    res.add(Integer.toString(nums[s]));
                } else {
                    String str = nums[s] + "->" + nums[e];
                    res.add(str);
                }
                ++e;
                s = e;
            }
        }
        return res;
    }
}




Summary Ranges(数组中连续的数字段)

Summary Ranges Given a sorted integer array without duplicates, return the summary of its ranges. ...
  • u010032372
  • u010032372
  • 2015-06-26 11:53:18
  • 622

LeetCode - 228. Summary Ranges - 思路详解- C++

假设有一个已排序整数数组,不包含重复的元素,返回其范围摘要
  • SJWL2012
  • SJWL2012
  • 2017-01-13 20:50:21
  • 188

Leetcode_228_Summary Ranges

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

LeetCode228——Summary Ranges

一个月没写C++代码,现在感到好陌生 Given a sorted integer array without duplicates, return the summary of its range...
  • booirror
  • booirror
  • 2015-07-09 00:25:58
  • 1865

<LeetCode OJ> 228. Summary Ranges

228. Summary Ranges My Submissions Question Total Accepted: 36554 Total Submissions: 159532 Diff...
  • EbowTang
  • EbowTang
  • 2016-02-10 16:11:10
  • 825

LeetCode228:Summary Ranges

Given a sorted integer array without duplicates, return the summary of its ranges.For example, given...
  • u012501459
  • u012501459
  • 2015-06-26 16:08:43
  • 624

Leetcode刷题:228. Summary Ranges

判断连续数的范围,遍历判断,很简单: version 1: class Solution(object): def summaryRanges(self, nums): ...
  • a529975125
  • a529975125
  • 2018-03-21 23:26:26
  • 7

LeetCode 228: Summary Ranges

Given a sorted integer array without duplicates, return the summary of its ranges. For example, give...
  • sunao2002002
  • sunao2002002
  • 2015-06-26 18:32:00
  • 3877

(数组)leetcode 228: Summary Ranges

Given a sorted integer array without duplicates, return the summary of its ranges. For example, giv...
  • Nitpicking
  • Nitpicking
  • 2016-01-05 21:47:58
  • 268

LeetCode OJ 系列之228 Summary Ranges --Python

Problem: Given a sorted integer array without duplicates, return the summary of its ranges. For ex...
  • ln152315
  • ln152315
  • 2015-11-16 09:11:30
  • 776
收藏助手
不良信息举报
您举报文章:leetcode 228: Summary Ranges
举报原因:
原因补充:

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