【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 ...
随机推荐
- Echarts - js
<script type="text/javascript"> var myChart; myChart = echarts.init(document.getElem ...
- Difference between Pragma and Cache-control headers?
Pragma is the HTTP/1.0 implementation and cache-control is the HTTP/1.1 implementation of the same c ...
- hdu1005 Number Sequence(数论)
Number Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...
- dojo 九 effects dojo/_base/fx 和 dojo/fx
官方教程:Dojo Effects这里讲学习一下dojo如何实现淡入.淡出.滑动等效果.实现这些特殊的效果有两个包 dojo/_base/fx 和 dojo/fx.dojo/_base/fx 中提供了 ...
- JavaScript —— 对象的取值与赋值
可能是因为用惯了 Java ,对一个对象取值/赋值喜欢用 setXXX() 和 getXXX() . 在 JavaScript 中使用 setValue() 时,遇到了个奇怪的问题,所以查了下 Jav ...
- Java —— 时区(夏令时)问题
有没有遇到过这样的情况:数据库里的是时间是“1991-4-14”,但是Java取出来后就成了“1991-4-13”. 解决方法一: 先把时区设成GMT,把 根据夏时制自动调节时钟 的选项去掉. 再把时 ...
- 寻找最小的k个数
1. 能想到的最直接的办法,就是对数组进行排序,最好的排序算法的时间复杂性为O(n*logn),这一个方法请参照各种排序算法. 2. 另外申请一个k空间数组,依次更改里面的最大值,每做一次最多要扫描一 ...
- [HZNUOJ1524]排队买票(DP)
题目链接:http://acm.hznu.edu.cn/JudgeOnline/problem.php?id=1524 简单分析后可以知道每一个手持两元的小朋友前面,售票员手里至少有一个一元. 假设d ...
- ubuntu 安装 rabbitmq-server
Rabbitmq 是用 erlang 语言写的,所以我们需要安装 Erlang,安装 erlang 又需要安装 python 与 simplejson,所以我们从python开始: 1.安装 pyth ...
- Android - View绘图原理总结
Android系统的视图结构的设计也采用了组合模式,即View作为所有图形的基类,Viewgroup对View继承扩展为视图容器类,由此就得到了视图部分的基本结构--树形结构 View定义了绘图的 ...