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 ...
随机推荐
- 链表c++实现一
#include <iostream>using namespace std; typedef char nodeEntry; struct Node{ //数据成员 nodeEntry ...
- sass编译
sass编译 命令行编译 单文件转换命令 sass style.scss style.css 单文件监听命令 sass --watch style.scss:style.css 文件夹监听命令 sas ...
- xmlstreaml xml过滤 格式化 报文的发送接收 struct2
有时候把东西想的过于复杂了,还是思路不清晰啊. seervlet struct2配置过程
- 在MyEclipse和Eclipse中添加Hibernate开发工具
一.插件准备 MyEclipse需要的插件:HibernateTools-3.2.4.zip Eclipse需要的插件:jbosstools-4.2.3.Final_2015-03-26_22-41- ...
- C#模拟键盘事件
public partial class Form1 : Form { public Form1() { InitializeComponent(); } [DllImport("USER3 ...
- css布局课件
1.什么是CSS盒模型 我们的网页就是通过一个一个盒子组成的. 2.一个盒子拥有的属性:宽和高(width和height).内边距(padding).边框(border).外边距(margin) wi ...
- 安装zabbix,make的时候报错
CDPATH= && /bin/bash /install/Mesa-/bin/missing aclocal-1.14 -I m4 /install/Mesa-/bin/missin ...
- correlation filters in object tracking
http://www.cnblogs.com/hanhuili/p/4266990.html Correlation Filter in Visual Tracking系列一:Visual Objec ...
- du 使用详解 linux查看目录大小 linux统计目录大小并排序 查看目录下所有一级子目录文件夹大小 du -h --max-depth=1 |grep [
常用命令 du -h --max-depth=1 |grep [TG] |sort #查找上G和T的目录并排序 du -sh #统计当前目录的大小,以直观方式展现 du -h --max-d ...
- jquery weibo 留言
<script> function getCnTime(s){ ); ))+'-'+toDou(oDate.getDate())+' '+toDou(oDate.getHours())+' ...