LeetCode OJ: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"].

就是概括区间的方式把一个数组表示下来,例如123用1->3代替,自己写的很乱 ,维护两个指针就可以了:

 1 class Solution {
 2 public:
 3     vector<string> summaryRanges(vector<int>& nums) {
 4         int start = 0;
 5         stringstream str;
 6         vector<string> ret;
 7         int sz = nums.size();
 8         if(sz == 0) return ret;
 9         if(sz == 1){
10             str << nums[0];
11             ret.push_back(str.str());
12             return ret;
13         }
14         //str << nums[0];
15         start = nums[0];
16         for(int i = 1; i < sz; ++i){
17             if(nums[i] == nums[i-1] + 1) continue;
18             if(start != nums[i - 1])  //防止单个数字是一个区间的情况
19                 str << start << "->" << nums[i - 1];
20             else
21                 str << start;
22             ret.push_back(str.str());
23             start = nums[i];
24             str.str("");
25         }
26         if(nums[sz - 1] == nums[sz - 2] + 1){
27             str << start << "->" << nums[sz - 1];
28             ret.push_back(str.str());
29         }else{
30             str << nums[sz - 1];
31             ret.push_back(str.str());
32         }
33         return ret;
34     }
35 };
时间: 2024-11-01 20:57:23

LeetCode OJ:Summary Ranges(概括区间)的相关文章

[LeetCode] 163. Missing Ranges 缺失区间

Given a sorted integer array nums, where the range of elements are in the inclusive range [lower, upper], return its missing ranges. Example: Input: nums = [0, 1, 3, 50, 75], lower = 0 and upper = 99,Output: ["2", "4->49", "51-

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

题目 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][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] 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 problem and

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]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"]. 意:找出排序数组(无重复数字)中的各个连续的范围 算法思想 基本思想如下,给定排序数组 N<a1,a2,a3,...,an>,

leetCode(50):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"]. 本题我采用的是最直观的算法,但整数转化为字符还有些坎坷.应该还有简单的算法,但目前没有时间去研究了. class Solution { public