LeetCode——Summary Ranges

Description:

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

确定有序数组的区间

需要注意各种边界细节,每次确定区间之后需要清空。

public class Solution {
    public List<String> summaryRanges(int[] nums) {
        List<String> list = new ArrayList<String>();
        if(nums.length == 0)
            return list;
        if(nums.length == 1) {
            list.add(nums[0]+"");
            return list;
        }
        boolean flag = false;
        StringBuilder ele = new StringBuilder();
        for(int i=0; i<nums.length; i++) {
            int start = 0, end = 0;

            if(!flag) {
                start = nums[i];
                flag = true;
                ele.append(start+"");
            }

            if(i<nums.length-1 && nums[i] != nums[i+1]-1) {
                end = nums[i];
                flag = false;
                if(nums[i] != start) {
                    ele.append("->" + end);
                }
            }

            if(ele != null) {
                if(i == nums.length-2 && nums[i] == nums[i+1]-1) {
                    end = nums[i+1];
                    ele.append("->" + end);
                    list.add(ele.toString());
                    ele.delete(0, ele.length());
                    break;
                }
                if(!flag || flag&&i==nums.length-1) {
                    list.add(ele.toString());
                    ele.delete(0, ele.length());
                }
            }
        }
        return list;
    }
}
时间: 2024-10-21 05:26:27

LeetCode——Summary Ranges的相关文章

[LeetCode] Summary Ranges 总结区间

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"]. Credits:Special thanks to @jianchao.li.fighter for adding this problem and

LeetCode -- Summary Ranges

题目描述: 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"]. Credits:Special thanks to @jianchao.li.fighter for adding this probl

LeetCode Summary Ranges (统计有序数列范围)

题意:给出个有序不重复数列(可能负数),用缩写法记录这个数列. 思路:找每个范围的起始和结束即可. 1 class Solution { 2 public: 3 vector<string> summaryRanges(vector<int>& nums) { 4 if(nums.empty()) return vector<string>(); 5 vector<string> vect; 6 for(int i=0; i<nums.size

[LeetCode]228.Summary Ranges

题目 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"]. 代码 /*--------------------------------------- * 日期:2015-08-04 * 作者:SJF01

LeetCode:Summary Ranges - 获取数组中数字的范围

1.题目名称 Summary Ranges(获取数组中数字的范围) 2.题目地址 https://leetcode.com/problems/summary-ranges/ 3.题目内容 英文:Given a sorted integer array without duplicates, return the summary of its ranges. 中文:给出一个整数数组,返回这个数组中数字的范围 例如:给出数组 [0,1,2,4,5,7],返回 ["0->2",&quo

[LeetCode][JavaScript]Summary Ranges

Summary Ranges 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"]. https://leetcode.com/problems/summary-ranges/ 简单的模拟题,合并连续的数

[LeetCode] Missing Ranges 缺失区间

Given a sorted integer array where the range of elements are [0, 99] inclusive, return its missing ranges.For example, given [0, 1, 3, 50, 75], return [“2”, “4->49”, “51->74”, “76->99”] 这道题让我们求缺失区间,跟之前那道Summary Ranges很类似,这道题让我们求缺失的空间,给了一个空间的范围[lo

leetcode_228题——Summary Ranges

Summary Ranges Total Accepted: 9278 Total Submissions: 48419My Submissions Question Solution 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&quo

Java for LeetCode 228 Summary Ranges

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"]. 解题思路: JAVA实现如下: public List<String> summaryRanges(int[] nums) { List