【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 ...
随机推荐
- scala函数式编程
1.作为值的函数 在Scala中,函数和数字一样,可以在变量中存放函数.可以把函数赋值给一个变量,格式为:val foee=fun _(函数名+空格+_)形式 2.匿名函数 在scala中,不需要给每 ...
- Case 架构的实际应用-1
We use testlink to manage cases, and the frame is below: Project Name -All Features(Modules) -Featur ...
- UI抑制限制(UI Suppression Limitations)
运行UI抑制的Lync进程使你的应用程序可以访问同一个Lync客户端终端,SIP处理和所有非抑制Lync客户端使用的媒体处理,但具有以下限制.SDK中的可见组件不可用,除了视频窗体(video win ...
- hdu 3177
题目大意:向体积为v的山洞中搬运n个物品,每个物品具有(a,b) 属性.其中a是停放体积,b是移动体积.输出这个山东是否能放下这n个物品 解题思路: 1)当前物品能否放进山洞取决于当前物品的的移动体积 ...
- eclipse中servers(服务器)的配置
eclipse中servers(服务器)的配置 使用eclipse+tomcat时,很多人喜欢安装tomcat插件,以便一键启动tomcat,但我不喜欢给eclipse安装一些非必须的插件,而ecli ...
- Oracle —— 表结构相关的SQL
1.表基本信息(Table) select * from user_tables t, user_tab_comments c where c.table_name = t.table_name an ...
- Jquery-json
第三方插件: jquery.json-2.4.js Jquery-json 是 jQuery 的一个插件,可轻松实现对象和 JSON 字符串之间的转换.可序列化 JavaScript 对象.数值.字符 ...
- 深入理解Java对象的序列化与反序列化的应用
当两个进程在进行远程通信时,彼此可以发送各种类型的数据.无论是何种类型的数据,都会以二进制序列的形式在网络上传送.发送方需要把这个Java对象转换为字节序列,才能在网络上传送:接收方则需要把字节序列再 ...
- POJ2186 POPULAR COW
链接:http://poj.org/problem?id=2186 题意:给你N个点,然后在给你N条有向边,然后让你找出这样的点S,S满足条件图上任意一点都能到达S. 要想满足任意一点都能到达,首先满 ...
- Ios中比较两个日期之间的时间差距
1.比较两个日期之间的时间差距 // 1.日历对象(标识:时区相关的标识) NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIde ...