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(); i++)
 7         {
 8             int s=nums[i], e;
 9             while(i+1<nums.size()&&nums[i]+1==nums[i+1])    i++;
10             e=nums[i];
11             if(s<e) vect.push_back(to_string(s)+"->"+to_string(e));
12             else    vect.push_back(to_string(s));
13         }
14         return vect;
15     }
16 };

AC代码

时间: 2024-10-17 09:14:51

LeetCode Summary Ranges (统计有序数列范围)的相关文章

[LeetCode]8. 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)初始化两个游标ind1=0,ind2=1,一个左边界left=ind1:(2)若nums[ind2]-nums[ind1]==1则ind

[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

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 { pu

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