Leetcode228. Summary Ranges汇总区间
给定一个无重复元素的有序整数数组,返回数组区间范围的汇总。
示例 1:
输入: [0,1,2,4,5,7] 输出: ["0->2","4->5","7"] 解释: 0,1,2 可组成一个连续的区间; 4,5 可组成一个连续的区间。
示例 2:
输入: [0,2,3,4,6,8,9] 输出: ["0","2->4","6","8->9"] 解释: 2,3,4 可组成一个连续的区间; 8,9 可组成一个连续的区间。
class Solution {
public:
vector<string> summaryRanges(vector<int>& nums)
{
int len = nums.size();
vector<string> res;
if(len == 0)
return res;
if(len == 1)
{
res.push_back(to_string(nums[0]));
return res;
}
int start = 0;
int end = 0;
for(int i = 0; i < len; i++)
{
end = i;
if(i == 0)
continue;
if(nums[i] - nums[i - 1] != 1)
{
if(start == end - 1)
res.push_back(to_string(nums[start]));
else
res.push_back(to_string(nums[start]) + "->" + to_string(nums[end - 1]));
start = i;
}
if(i == len - 1)
{
if(start != end)
res.push_back(to_string(nums[start]) + "->" + to_string(nums[end]));
else
res.push_back(to_string(nums[start]));
}
}
return res;
}
};
Leetcode228. Summary Ranges汇总区间的更多相关文章
- 228 Summary Ranges 汇总区间
给定一个无重复元素的有序整数数组,返回数组中区间范围的汇总. 示例 1: 输入: [0,1,2,4,5,7]输出: ["0->2","4->5",& ...
- [LeetCode] 228. Summary Ranges 总结区间
Given a sorted integer array without duplicates, return the summary of its ranges. Example 1: Input: ...
- [LeetCode] Summary Ranges 总结区间
Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...
- [LeetCode228]Summary Ranges
题目: Given a sorted integer array without duplicates, return the summary of its ranges. For example, ...
- [LeetCode] 163. Missing Ranges 缺失区间
Given a sorted integer array nums, where the range of elements are in the inclusive range [lower, up ...
- [LeetCode] Missing Ranges 缺失区间
Given a sorted integer array where the range of elements are [0, 99] inclusive, return its missing r ...
- 【LeetCode】228. Summary Ranges 解题报告(Python)
[LeetCode]228. Summary Ranges 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/sum ...
- leetcode-【中等题】228. Summary Ranges
题目: 228. Summary Ranges Given a sorted integer array without duplicates, return the summary of its r ...
- Missing Ranges & Summary Ranges
Missing Ranges Given a sorted integer array where the range of elements are [lower, upper] inclusive ...
随机推荐
- 第二周课堂笔记1th
1. 三元运算 + 2. for循环 for为有限循环,while为无限循环 可迭代对象:是字符串,数字不可以 数字不可以迭代但是可以用range函数 for i in range(1 ...
- <day006>bootstrap的简单学习 + 轮播图
任务1:bootstrap的简单学习 <!DOCTYPE html> <html lang="zh-CN"> <head> <meta c ...
- <随便写> 多线程的例子
''' 一个线程在使用这个共享的时候,其他线程必须等待他结束 通过"锁"实现,作用就是防止多个线程使用这片内存空间 进程:程序的一次执行 线程:cpu运算的基本调度单位 多线程:大 ...
- Activiti流程图查看
1.测试用例查看图片 public void viewImage() throws Exception { // 创建仓库服务对对象 RepositoryService repositoryServi ...
- thinkphp 子查询
从3.0版本开始新增了子查询支持,有两种使用方式: 大理石平台检验标准 1.使用select方法 当select方法的参数为false的时候,表示不进行查询只是返回构建SQL,例如: // 首先构造子 ...
- 完全卸载之前8.0的Mysql,安装5.5mysql
完全卸载: https://blog.csdn.net/sxingming/article/details/52601250 安装mysql5.5: https://blog.csdn.net/fly ...
- 修改cmd命令默认路径
未修改之前: 修改方法: 1.win+r打开运行对话框,输入 regedit 打开注册表编辑器 2.在注册表中找到:HKEY_CURRENT_USER\Software\Microsoft\Comma ...
- We'll be fine.
可怜的人心中都有一种信念,那就是 明天会更好. Everything will be fine. 我拥见风来,嗅见花开,是避不掉的厉寒,是止不住的徘徊.
- codeforces 1136E-Nastya Hasn't Written a Legend
传送门:QAQQAQ 题意:有一个数组a和一个数组k,数组a一直保持一个性质:a[i + 1] >= a[i] + k[i].有两种操作:1,给某个元素加上x,但是加上之后要保持数组a的性质.比 ...
- 《DSP using MATLAB》Problem 7.35
代码: %% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ %% Output In ...