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"].
class Solution {
public:
vector<string> summaryRanges(vector<int>& nums) {
int n=nums.size();
int i;
int left,right;
char str[];
left=;
right=;
vector<string> res;
if(n<)
return res;
for(i=;i<n;i++)
{
if(i==)
{
left=nums[i];
right=left;
}
else if(right+==nums[i])
{
right++;
}
else
{
if(left!=right)
{
sprintf(str,"%d->%d",left,right);
string tmp(str);
res.push_back(tmp);
}
else
{
sprintf(str,"%d",left);
string tmp(str);
res.push_back(tmp);
}
left=nums[i];
right=left; }
}
if(left!=right)
{
sprintf(str,"%d->%d",left,right);
string tmp(str);
res.push_back(tmp);
}
else
{
sprintf(str,"%d",left);
string tmp(str);
res.push_back(tmp);
}
return res;
}
};
Summary Ranges的更多相关文章
- 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 ...
- leetcode面试准备:Summary Ranges
1 题目 Given a sorted integer array without duplicates, return the summary of its ranges. For example, ...
- 【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] Summary Ranges 总结区间
Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...
- Java for LeetCode 228 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(228) Summary Ranges
Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...
随机推荐
- view.performClick()触发点击事件
1.主要作用 自动触发控件的点击事件 2.界面的布局文件 activity_main.xml <RelativeLayout xmlns:android="http://schema ...
- Swift 中的函数(下)
学习来自<极客学院:Swift中的函数> 工具:Xcode6.4 直接上基础的示例代码,多敲多体会就会有收获:百看不如一敲,一敲就会 import Foundation /******** ...
- Jmeter代理录制脚本
录制的原理: 1.LR/Jmeter录制是针对网络通讯协议层面的,它只关心客户端与服务器端的通讯包2.LR/Jmeter的并发测试实际上就是并发客户端与服务器端的通讯过程3.压力是通过多进程/多线程方 ...
- Effective Java 08 Obey the general contract when overriding equals
When it's the case that each instance of the class is equal to only itself. 1. Each instance of the ...
- WebApi 使用PUT和DELETE时报405的问题
最近两天写了个项目,里面有一个接口是用谓词delete接收请求. 本地完全没问题,但是当发布到服务器上之后(IIS7.5),就报出 405.0 - Method Not Allowed 很明显是配置问 ...
- wordpress安装记录
wordpress 已经完全部署到Linux后,进行开始安装的时候,数据库信息都填入好了(前提是:链接信息输入都正确) 然后点击会报错,说是链接数据库失败(数据库是建在阿里云服务器上的),但是具体不知 ...
- php基本语法
<?phpecho "hello test01";$var ="PHP";echo "$var";echo "<br ...
- JavaScript中点号“.”的多义性
点号「.」在JavaScript中有两种语义 语义1.表示算术中的小数点(浮点数),如 2.5 语义2.取对象属性.方法,如 [].push(2) 这几乎没有任何难理解的地方,但下面这个问题则很有趣. ...
- luluzero的angularJs学习之路_angularJs示例代码
最近开始自学 angularJs这个前端MVC框架,感觉在前端实现MVC很酷有木有.哈哈哈... 先说说我对前端MVC的一个基本的理解吧(刚开始学习接触得还比较浅显,理解可能会有些不到位,还请各位大神 ...
- php 利用activeMq+stomp实现消息队列
php 利用activeMq+stomp实现消息队列 一.activeMq概述 ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线.ActiveMQ 是一个完全支持JMS1.1和J ...