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

public class Solution {
    public List<String> summaryRanges(int[] nums) {
        //注意从0开始遍历,使用一个temp标记当前遍历到的下标值,然后比较nums[i+1]-nums[i]==1,
        //比较是否range只有一个值,然后保存结果,需要注意的是:内层while循环需要判断i的范围!!

        ArrayList<String> res=new ArrayList<String>();
        if(nums==null||nums.length<=0) return res;
        StringBuilder seq;
        for(int i=0;i<nums.length;i++){
            int temp=nums[i];
            while(i<nums.length-1&&nums[i+1]-nums[i]==1){
                i++;
            }
            if(nums[i]!=temp){
                res.add(temp+"->"+nums[i]);
            }else{
                res.add(temp+"");
            }
        }
        return res;
    }
}
时间: 2024-10-13 03:57:43

[leedcode 228] Summary Ranges的相关文章

[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

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"]. 这个题目不难,首先对原序列进行排序,然后要注意对特殊情况的处理.同时,要掌握string类的几个重要成员函数: to_string():将数字转换为

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

【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"]. 1 class Solution { 2 public: 3 vector<string> summaryRanges(vector&l

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_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

[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/ 简单的模拟题,合并连续的数

[leedcode]Summary Ranges

双指针思路,循环时,声明一个变量保存范围起始的下标 注意:如何在数组只有一个元素,还有遍历到最后一个元素的时候,去更新结果集. public class Solution { public List<String> summaryRanges(int[] nums) { List<String> res=new ArrayList<String>(); int start=0; for(int i=1;i<=nums.length;i++){ if(i==nums

Summary Ranges —— LeetCode

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>