【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"].
My Solution:
vector<string> summaryRanges(vector<int>& nums)
{
vector<string> ret;
if(nums.empty())return ret;
int low=nums[];
bool flag=false;
for(int i=;i<nums.size();i++){
while(i<nums.size() && nums[i]==nums[i-]+)i++;
int high=nums[i-];
if(low!=high)
ret.push_back(to_string(low)+"->"+to_string(high));
else
ret.push_back(to_string(low));
if(i==nums.size()){
flag=true;
break;
}
low=nums[i];
}
if(flag==false)ret.push_back(to_string(low));
return ret;
}
Better Solution:
class Solution {
public:
vector<string> summaryRanges(vector<int>& nums) {
vector<string> vec;
if(nums.empty())
return vec; int low=nums[],high=nums[];
for(int i = ; i < nums.size(); i ++)
{
if(nums[i]-nums[i-] == )high=nums[i];
else
{
string range;
if(low != high)
range = to_string(low) + "->" + to_string(high);
else
range = to_string(low);
vec.push_back(range);
low = nums[i];
high = nums[i];
}
}
string range;
if(low != high)
range = to_string(low) + "->" + to_string(high);
else
range = to_string(low);
vec.push_back(range);
return vec;
}
};
【LeetCode】228 - Summary Ranges的更多相关文章
- 【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】163. Missing Ranges 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...
- leetcode面试准备:Summary Ranges
1 题目 Given a sorted integer array without duplicates, return the summary of its ranges. For example, ...
- 【LeetCode】 454、四数之和 II
题目等级:4Sum II(Medium) 题目描述: Given four lists A, B, C, D of integer values, compute how many tuples (i ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
随机推荐
- 调试EF源代码环境配置
下载EF6的源代码,运行build编译,Nuget会自动下载所需的DLL. 打开EF的工程,可以在EF解决方案下直接新建调试用的项目代码 添加EF引用时选择解决方案中的Entity Framework ...
- 蓝缘管理系统第三版推出。springMVC4.0+shiro1.2.3+spring4.x+Mybaits3.2.8
blog.csdn.net/mmm333zzz/article/details/42059349 http://blog.csdn.net/zoutongyuan/article/details/41 ...
- python公司面试题集锦 python面试题大全
问题一:以下的代码的输出将是什么? 说出你的答案并解释. class Parent(object): x = 1 class Child1(Parent): pass class Child2(Par ...
- WebDriverExtensionsByC#
测试工具//********************************************************************************************** ...
- 使用 httpkit 来替代 jetty
Compojure 是一个基于 ring 的上层web开发框架.在 lein new compojure my-app 生成的项目中,默认是启用 jetty 服务器的,最近用到了 http-kit 中 ...
- BZOJ 2752 高速公路(road)(线段树)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2752 题意:给出一个数列A,维护两种操作: (1)将区间[L,R]之内的所有数字增加de ...
- 推荐开源Api文档生成工具——Doxygen
http://www.stack.nl/~dimitri/doxygen/index.html 非常的方便. 2步生成API文档. 具体信息见官网哟!
- Python3 学习第九弹: 模块学习二之文件管理模块
os模块 提供访问操作系统的接口 1> name 获得当前操作系统 其中 'nt' 是 windows 'posix' 是 linux 2> environ 获得当前系统的环境变量的字典, ...
- BZOJ 1935 园丁的烦恼
离线,BIT. #include<iostream> #include<cstdio> #include<cstring> #include<algorith ...
- (转)Mac OS X写了个rm时将文件放入回收站的小工具
上次由于公司里机器要面临重装,开始仓皇地将Mac本里的文件scp到我的台式机上.忙乱之中本来要删除一个无用的文件夹的,结果用rm -rf的时候tab了一下,补全出来的文件都没看清就按下了回车,毫无疑问 ...