题目

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. 这个匿名对象没有实现IComparable接口

    https://www.cnblogs.com/felixnet/p/5193086.html https://docs.microsoft.com/zh-cn/dotnet/api/system.i ...

  2. D. Edges in MST 图论

    http://codeforces.com/contest/160/problem/D base on 克鲁斯卡尔, 首先每次都是对权值相同的边进行统一处理,假如加入了当前这条边出现了回路,那就能确定 ...

  3. Corn 表达式

    CronTrigger CronTriggers往往比SimpleTrigger更有用,如果您需要基于日历的概念,而非SimpleTrigger完全指定的时间间隔,复发的发射工作的时间表.CronTr ...

  4. Windows下用cpu模式跑通目标检测py-faster-rcnn 的demo.py

    关键字:Windows.cpu模式.Python.faster-rcnn.demo.py 声明:原文发表在博客园,未经允许不得转载!!!本篇blog过程已经多名读者实践验证,有人反馈报错TypeErr ...

  5. Elasticsearch优化

    2.out of memory错误 因为默认情况下es对字段数据缓存(Field Data Cache)大小是无限制的,查询时会把字段值放到内存,特别是facet查询,对内存要求非常高,它会把结果都放 ...

  6. Oracle中文乱码,字符集问题处理

    1. 右键计算机,选择属性,增加环境变量 NLS_LANG:SIMPLIFIED CHINESE_CHINA.ZHS16GBK 2.进入注册表,依次单击HKEY_LOCAL_MACHINE --> ...

  7. java Integer判等的大坑

    在-128 至 127 范围内的赋值,Integer 对象是在IntegerCache.cache 产生,会复用已有对象,这个区间内的 Integer 值可以直接使用==进行 判断,但是这个区间之外的 ...

  8. HDU 3530Subsequence(单调队列)

    题意 题目链接 给出$n$个数,找出最长的区间,使得区间中最大数$-$最小数 $>= m$ 且$<= k$ Sol 考虑维护两个单调队列. 一个维护$1 - i$的最大值,一个维护$1 - ...

  9. 字典(dict),增删改查,嵌套

    一丶字典 dict 用{}来表示  键值对数据  {key:value}  唯一性 键 都必须是可哈希的 不可变的数据类型就可以当做字典中的键 值 没有任何限制 二丶字典的增删改查 1.增 dic[k ...

  10. 洛谷 P3387 【模板】缩点

    题目背景 缩点+DP 题目描述 给定一个n个点m条边有向图,每个点有一个权值,求一条路径,使路径经过的点权值之和最大.你只需要求出这个权值和. 允许多次经过一条边或者一个点,但是,重复经过的点,权值只 ...