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"].
Credits:
==============
题目,返回数组范围的 集合.
思路:
模拟整个过程,利用标准库中提供了to_string函数,
就没有什么大问题.
============
code如下:
class Solution {
public:
vector<string> summaryRanges(vector<int>& nums) {
vector<string> re;
if(nums.empty()) return re;
int n = nums.size();
for(int i = ;i<n;){
//string tt = to_string(nums[i]);
string t = to_string(nums[i])+"->";
int j = i+;
while(j<n && nums[j]==nums[j-]+){
j++;
}
if(i==(n-) || j==(i+)){
re.push_back(to_string(nums[i]));
}else{
t+=to_string(nums[j-]);
re.push_back(t);
}
i = j;
}///for
for(auto i:re) cout<<i<<endl;
return re;
}
};
228. Summary Ranges的更多相关文章
- leetcode-【中等题】228. Summary Ranges
题目: 228. Summary Ranges Given a sorted integer array without duplicates, return the summary of its r ...
- 【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 ...
- Java for LeetCode 228 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 ...
- (easy)LeetCode 228.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 ...
- Java [Leetcode 228]Summary Ranges
题目描述: Given a sorted integer array without duplicates, return the summary of its ranges. For example ...
- C#解leetcode 228. Summary Ranges Easy
Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...
随机推荐
- Win 環境構建調試用TreeWalker
語法 var ppTreeWalker = document.createTreeWalker(pRootNode, ulWhatToShow, pFilter, fEntityReferenceEx ...
- code::blocks(版本10.05) 配置opencv2.4.3
(1)首先下载opencv2.4.3, 解压缩到D:下: (2)配置code::blocks, 具体操作如下: 第一步, 配置compiler, 操作步骤为Settings -> Compil ...
- POJ 3461 裸的KMP
直接贴代码吧 #include<cstdio> #include<cstring> ],T[]; ]; int n,m; void getfail() { f[] = ; f[ ...
- Qt 串口学习2
未命名 (2) 1 新建串口 //new serial portmy_serialport= new QSerialPort(); 2给串口设定名字 my_serialport->setPort ...
- Baxter机器人---安装SDK包(二)
原创博文,转载请标明出处:--周学伟http://www.cnblogs.com/zxouxuewei/ 一.frist baxter robot workspace root@zxwubuntu-A ...
- java03实验截图
- PHP 将json的stdClass Object转成数组array
PHP和JS通讯通常都用json,但是PHP要用json的数据,通过json_decode转出来的数组并不是标准的array,所以需要用这个函数进行转换. function object_array( ...
- POJ1419 Graph Coloring(最大独立集)(最大团)
Graph Coloring Time Limit: 1000MS Memor ...
- phpwind将服务器数据同步到本地之后网站不显示或者排版错误
在将phpwind的数据同步到本地服务器之后 如果访问本地服务器的首页不能显示的话 首先要查看global.php文件中的D_P变量,官方默认 的此变量应该指向和R_P变量是同一个文件夹即网站的根目录 ...
- nginx和apache下的url rewrite
将服务器上面的数据同步到本地之后,发现打开首页显示不正常,本地服务器是apache,经过打开url rewrite之后本地首页正常显示. 原因是phpwind本身支持了url rewrite的功能,但 ...