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”].
分析
题目要求很明显,要求对给定的一组有序整数序列按照连续性分组显示;
一次遍历记录连续子序列的首尾元素,然后转换为string格式,整数和字符串格式类型转换需要特别注意INT_MIN时特殊处理;
AC代码
class Solution {
public:
vector<string> summaryRanges(vector<int>& nums) {
if (nums.empty())
return vector<string>();
int sz = nums.size();
vector<string> ret;
int start = nums[0], end = nums[0];
for (int i = 1; i < sz; ++i)
{
if (nums[i] == end + 1)
end = nums[i];
else{
string tmp;
if (end == start)
tmp = IntToString(start);
else
tmp = IntToString(start) + "->" + IntToString(end);
ret.push_back(tmp);
start = nums[i];
end = nums[i];
}
}//for
//加上最后一组
string tmp;
if (end == start)
tmp = IntToString(start);
else
tmp = IntToString(start) + "->" + IntToString(end);
ret.push_back(tmp);
return ret;
}
string IntToString(long num)
{
if (num == 0)
return "0";
else if (num == INT_MIN)
return "-2147483648";
string str;
bool flag = num < 0 ? false : true;
num = abs(num);
while (num)
{
char c = num % 10 + '0';
num /= 10;
str += c;
}//while
reverse(str.begin(), str.end());
if (flag)
return str;
else
return "-" + str;
}
};
LeetCode(228) Summary Ranges的更多相关文章
- LeetCode(275)H-Index II
题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...
- LeetCode(220) Contains Duplicate III
题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...
- LeetCode(154) Find Minimum in Rotated Sorted Array II
题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...
- LeetCode(122) Best Time to Buy and Sell Stock II
题目 Say you have an array for which the ith element is the price of a given stock on day i. Design an ...
- LeetCode(116) Populating Next Right Pointers in Each Node
题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...
- LeetCode(113) Path Sum II
题目 Given a binary tree and a sum, find all root-to-leaf paths where each path's sum equals the given ...
- LeetCode(107) Binary Tree Level Order Traversal II
题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...
- LeetCode(4)Median of Two Sorted Arrays
题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
- Leetcode(1)两数之和
Leetcode(1)两数之和 [题目表述]: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标.你可以假设每种输入只会对应一 ...
随机推荐
- 079 Word Search 单词搜索
给定一个二维面板和一个单词,找出该单词是否存在于网格中.这个词可由顺序相邻的单元格内的字母构成,其中“相邻”单元格是那些水平相邻或垂直相邻的单元格.同一个单元格内的字母不允许被重复使用.例如,给定 二 ...
- Java基础学习_01 概述及环境配置
一.概述 1.Java语言平台版本 1.1J2SE(Java 2 Platform Standard Edition)标准版 为开发普通桌面和商务应用程序提供的解决方案,该技术体系是其他两者的基础,可 ...
- C#基础文件file的各种套路
File的各种套路 //创建一个文件 //File.Create(@"C:\Users\SpringRain\Desktop\new.txt"); //Console.WriteL ...
- Linux netstat命令详解和使用例子(显示各种网络相关信息)
netstat命令用于显示与IP.TCP.UDP和ICMP协议相关的统计数据,一般用于检验本机各端口的网络连接情况.netstat是在内核中访问网络及相关信息的程序,它能提供TCP连接,TCP和UDP ...
- Java常用函数式接口--Consumer接口andThen()方法使用案例(二)
Java常用函数式接口--Consumer接口使用案例
- Git 连接远程仓库Github
创建SSH Key. 在用户主目录下,看看有没有.ssh目录,如果有,再看看这个目录下有没有id_rsa和id_rsa.pub这两个文件,如果已经有了,可直接跳到下一步. 如果没有,打开Shell(W ...
- Docker的下载安装以及简单使用
Docker的简介 Docker是一个基于GO语言开发的应用容器,它是一款适合运维人员和后段开发人员学习的开源容器引擎.Docker容器可以让开发的应用或者依赖包存储其中,可以运行在任何的Linux ...
- mitmproxy抓包软件在mac上边的安装
官网介绍:mitmproxy is a free and open source interactive HTTPS proxy. mitmproxy 是用 Python 和 C 开发的一个中间人代理 ...
- 通过CMD命令行创建和使用Android 模拟器 AVD
进行Android APP测试时,若手持android手机设备稀少的情况下,我们可以通过创建Android模拟器AVD来代替模拟android手机设备,本文就具体介绍如何创建和使用AVD. 1.创建A ...
- JavaScript_3_输出
1. JavaScript通常用于操作HTML元素,可以使用getElementById(id)方法. JavaScript由Web浏览器来执行. 2. document.write()仅仅向文档输出 ...