题目

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;
}
};

GitHub测试程序源码

LeetCode(228) Summary Ranges的更多相关文章

  1. LeetCode(275)H-Index II

    题目 Follow up for H-Index: What if the citations array is sorted in ascending order? Could you optimi ...

  2. LeetCode(220) Contains Duplicate III

    题目 Given an array of integers, find out whether there are two distinct indices i and j in the array ...

  3. LeetCode(154) Find Minimum in Rotated Sorted Array II

    题目 Follow up for "Find Minimum in Rotated Sorted Array": What if duplicates are allowed? W ...

  4. 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 ...

  5. LeetCode(116) Populating Next Right Pointers in Each Node

    题目 Given a binary tree struct TreeLinkNode { TreeLinkNode *left; TreeLinkNode *right; TreeLinkNode * ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. Leetcode(1)两数之和

    Leetcode(1)两数之和 [题目表述]: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标.你可以假设每种输入只会对应一 ...

随机推荐

  1. python2 学习 数据类型和变量

    数据类型和变量 数据类型 整数 Python可以处理任意大小的整数,当然包括负整数,在程序中的表示方法和数学上的写法一模一样,例如:1,100,-8080,0,等等. 计算机由于使用二进制,所以,有时 ...

  2. stm32的低功耗模式:

    一.待机模式.待机模式是低功耗中最低功耗的,内部电压调节电路被关闭,  HSE.HIS.PLL被关闭:进入待机模式后,SRAM和寄存器的内容将丢失.  (CPU停止,外设停止,RAM的数据寄存器的内容 ...

  3. 一文读懂DDD

    何为DDD DDD不是架构设计方法,不能把每个设计细节具象化,DDD是一套体系,决定了其开放性,体系中可以用任何一种方法来解决这些问题,但是如果一些关键问题没有具体方案落地,可能让团队无所适从. 有的 ...

  4. Unity3d Attribute 总结(转)

      举两个例子,在变量上使用[SerializeFiled]属性,可以强制让变量进行序列化,可以在Unity的Editor上进行赋值. 在Class上使用[RequireComponent]属性,就会 ...

  5. SLF4J user manual 专题

    System Out and Err Redirected to SLF4J The sysout-over-slf4j module allows a user to redirect all ca ...

  6. hard link && symbolic link

    hard link :硬连接,多了一个inode,指向原始的inode,通过这个硬连接删除文件,文件不会被真正删除,而是删除这个inode symolic link:符号连接相当于快捷方式

  7. springboot集成shiro实现验证码校验

    github:https://github.com/peterowang/shiro/ 这里实现验证码校验的思路是自己添加一个Filter继承FormAuthenticationFilter,Form ...

  8. cloud turbine

    turbine是聚合服务器发送事件流数据的一个工具,hystrix的监控中,只能监控单个节点,实际生产中都为集群,因此可以通过 turbine来监控集群下hystrix的metrics情况,通过eur ...

  9. @vue/cli 3.x项目脚手架 webpack 配置

    @vue/cli  是一个基于 Vue.js 进行快速开发的完整系统. @vue/cli   基于node服务  需要8.9以上版本 可以使用 nvm等工具来控制node版本  构建于 webpack ...

  10. Date-DateFormat-Calendar-Math-regex

    一.Date类(java.util) 作用:表示时间的类,精确到毫秒,以GMT 1970年1月1日0点0分0秒起算 构造方法:     Data() ---获取当前时间      Date(long ...