228. [LeetCode] Summary Ranges
Given a sorted integer array without duplicates, return the summary of its ranges.
Example 1:
Input: [0,1,2,4,5,7]
Output: ["0->2","4->5","7"]
Explanation: 0,1,2 form a continuous range; 4,5 form a continuous range.
Example 2:
Input: [0,2,3,4,6,8,9]
Output: ["0","2->4","6","8->9"]
Explanation: 2,3,4 form a continuous range; 8,9 form a continuous range.
class Solution {
public:
vector<string> summaryRanges(vector<int>& nums) {
const int size_n = nums.size();
vector<string> res;
if ( == size_n) return res;
for (int i = ; i < size_n;) {
int start = i, end = i;
while (end + < size_n && nums[end+] == nums[end] + ) end++;
if (end > start) res.push_back(to_string(nums[start]) + "->" + to_string(nums[end]));
else res.push_back(to_string(nums[start]));
i = end+;
}
return res;
}
};
class Solution {
public:
vector<string> summaryRanges(vector<int>& nums) {
vector<string> result;
if(!nums.size()) return result;
int low = nums[], high = nums[];
for(int i = ; i < nums.size(); i++){
if (nums[i] == high + )
high++;
else{
result.push_back(low == high ? to_string(low) : to_string(low) + "->" + to_string(high));
low = high = nums[i];
}
}
result.push_back(low == high ? to_string(low) : to_string(low) + "->" + to_string(high));
return result;
}
};
228. [LeetCode] Summary Ranges的更多相关文章
- LeetCode(228) Summary Ranges
题目 Given a sorted integer array without duplicates, return the summary of its ranges. For example, g ...
- [LeetCode] Summary Ranges 总结区间
Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...
- (leetcode)Summary Ranges
Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...
- LeetCode——Summary Ranges
Description: Given a sorted integer array without duplicates, return the summary of its ranges. For ...
- LeetCode Summary Ranges (统计有序数列范围)
题意:给出个有序不重复数列(可能负数),用缩写法记录这个数列. 思路:找每个范围的起始和结束即可. class Solution { public: vector<string> summ ...
- 【LeetCode】228. Summary Ranges 解题报告(Python)
[LeetCode]228. Summary Ranges 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/sum ...
- 【刷题-LeetCode】228. Summary Ranges
Summary Ranges Given a sorted integer array without duplicates, return the summary of its ranges. Ex ...
- leetcode-【中等题】228. Summary Ranges
题目: 228. Summary Ranges Given a sorted integer array without duplicates, return the summary of its r ...
- leetcode面试准备:Summary Ranges
1 题目 Given a sorted integer array without duplicates, return the summary of its ranges. For example, ...
随机推荐
- Java语言实现通过Ajax抓取后台数据及图片
1.Java语言实现通过Ajax抓取后台数据及图片信息 1.1数据库设计: create table picture( pic_id number not null, pic_name )not nu ...
- ArcMap中用python的split方法提取字段的值
提取PROPERTY_L字段空格分隔符前面的地址编号 提取前:5105 ABERDEEN LANE 提取后:5105 提取的表达式:!PROPERTY_L!.split(" ")[ ...
- P1015 回文数解题思路(非原创)
测试 #include<bits/stdc++.h> using namespace std; int n,m,step; string nn; int len,nex; bool dfs ...
- dfs板子题-Hdu1283Vegetables
题目描述毕业后,Vegetable在一家建筑公司找到了工作.他所在的城市将要进行整修,要求把所有空地修成公园. 市区是一个N*M的矩形,Vegetable拿到了该市的地图,现在判断共要修几处公园? 注 ...
- 移动端触摸滑动插件Swiper使用指南
Swiper是一款开源.免费.功能十分强大的移动端内容触摸滑动插件,目前的最新版本是Swiper4.Swiper主要面向的是手机.平板电脑等移动设备,帮助开发者轻松实现触屏焦点图.触屏Tab切换.触屏 ...
- HBase(3)-安装与Shell操作
一. 安装 1. 启动Zookeeper集群 2. 启动Hadoop集群 3. 上传并解压HBase -bin.tar.gz -C /opt/module 4. 修改配置文件 #修改habse-env ...
- Java实例 Part4:数组及其常用操作
目录 Part4:数组及其常用操作 Example01:将二维数组的行列交换 Example02:使用选择排序法对数组进行排序 Example03:使用冒泡排序法对数组进行排序 Example04:使 ...
- Linux下C语言编译的问题
在Linux下编程发现一个诡异的现象,就是在链接一个静态库的时候总是报错,类似下面这样的错误: (.text+0x13): undefined reference to `func' 关于undefi ...
- xshell sftp可用命令,sftp: cannot open d: to write![解决]
sftp可用命令: cd 路径 更改远程目录到“路径” lcd 路径 更改本地目录到“路径” chgrp group path 将文件“path”的组更改为“group” chmod mode pat ...
- 20155210 2016-2017-2 《Java程序设计》第10周学习总结
20155210 2016-2017-2 <Java程序设计>第10周学习总结 教材学习内容总结 计算机网络概述 网络编程的实质就是两个(或多个)设备(例如计算机)之间的数据传输.按照计算 ...