leetcode-【中等题】228. Summary Ranges
题目:
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"].
答案:
就是找连续的序列。
直接判断相邻数据大小是否相差为1即可,主要是注意最后的数字要特殊考虑一下。
链接:
https://leetcode.com/problems/summary-ranges/
代码:
#include <vector>
#include <string>
#include <sstream> using std::vector;
using std::string;
using std::ostringstream; class Solution {
private:
string num2str(int& num)
{
ostringstream stream;
stream << num;
return stream.str();
} public:
vector<string> summaryRanges(vector<int>& nums) {
unsigned int len = nums.size();
unsigned int index,inIndex;
unsigned int minIndex = ;
unsigned int maxIndex = ; vector<string> ans;
if(len == )
{
return ans;
} if(len == )
{
ans.push_back(num2str(nums[]));
return ans;
} for(index = ; index < len - ; ++ index)
{
if(nums[index] + == nums[index + ])
{
maxIndex = index + ;
}else
{
string tmp = "";
tmp += num2str(nums[minIndex]);
if(maxIndex > minIndex)
{
tmp += "->";
tmp += num2str(nums[maxIndex]);
}
ans.push_back(tmp); minIndex = index + ;
maxIndex = index + ;
}
} if(nums[len-] + == nums[len - ])
{
maxIndex = len - ;
}else
{
minIndex = len - ;
maxIndex = len - ;
} string tmp = "";
tmp += num2str(nums[minIndex]);
if(maxIndex > minIndex)
{
tmp += "->";
tmp += num2str(nums[maxIndex]);
}
ans.push_back(tmp); return ans;
}
};
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 (总结区间)
Given a sorted integer array without duplicates, return the summary of its ranges. Example 1: Input: ...
- C#解leetcode 228. Summary Ranges Easy
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. Example 1: Input: ...
- 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 ...
随机推荐
- IIC
IIC多主从,双向传输,只有两根线:一根数据,一根时钟,时钟必须由主机发出控制.初始化时主机把SCL和SDA的电平都拉高,然后在SCL保持高电平时SDA拉低形成一个开始信号,紧接着开始信号就开始发送要 ...
- dfs序
dfs序比较重要的性质:一棵子树的所有节点在dfs序里是连续一段,主要就是利用这个性质来解题 题型一:对某个点X权值加上一个数W,查询某个子树X里所有点权值和. 解:列出dfs序,实现修改一个数,查询 ...
- wordmate 词典安装
wordmate,使用 StarDict 的词典,可称为 Android 上的 StarDict 安装 wordmate 后,会在 SD 卡中生成 wordmate 文件夹,词典便放在此目录 下载词典 ...
- login
package addresslist; import java.awt.EventQueue; import java.awt.Graphics; import java.awt.event.Act ...
- windows开机记录查询
http://jingyan.baidu.com/article/3d69c5516b9a9ef0cf02d7f3.html
- Android AChartEngine 饼图渐变效果
二话不说,先上图 核心代码如下: private void initLabelChat(View rootView) { NumberFormat nf = new DecimalFormat(&qu ...
- Sphinx的配置和使用
项目中用到了,昨天老大又给讲了讲,我感觉这玩意真是强大.想把一些功能以很小的代价做好,第三方的工具是必须要掌握的. 1. 我的开发环境在Windows上,下载了sphinx-2.2.6-release ...
- android学习笔记57——电话管理器TelephoneyManager
电话管理器TelephoneyManager
- 【学】AngularJS日记(3)- $apply(), run()方法
$scope.$apply()方法可以强制$apply()里运行的函数所改变的model里的数据直接反应到view里,因为在angular的环境中,有时会用到原生js或者jquery的时候,这些行为有 ...
- Redis内存使用优化与存储
抄自http://www.infoq.com/cn/articles/tq-redis-memory-usage-optimization-storage 本文将对Redis的常见数据类型的使用场景以 ...