题目

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. aix OPATH ISSUE

    issue 1: OPatch cannot find a valid oraInst.loc file to locate Central Inventory (OPatch failed with ...

  2. HTTP状态码完整版

    HTTP 状态代码的完整列表   1xx(临时响应) 用于表示临时响应并需要请求者执行操作才能继续的状态代码. 代码 说明 100(继续) 请求者应当继续提出请求.服务器返回此代码则意味着,服务器已收 ...

  3. 关于使用mybatis的分页插件问题

    首先我需要导入架包 1.pagehelper 如果你是在mybatis中配置分页‘ 如下代码 <plugins> <plugin interceptor="com.gith ...

  4. html页面和jsp页面的区别

    html页面: html页面是静态页面,可以使用html+css+js实现页面的各种效果,单纯使用html布局出来的页面是设定好的页面,可以使用本地浏览器打开.同时搭配使用ajax实现数据交互效果的页 ...

  5. css 居中问题总结

    html代码: <div class="box"> <div class="box-item"> 文字 </div> < ...

  6. 织梦dedecms手机版上下篇链接错误的解决方法

    打开 \include\arc.archives.class.php 1. 找到 $this->PreNext['pre'] = "上一篇:<a href='$mlink'> ...

  7. FAST FW150R软件版本升级解决一些网页无法加载问题

    家里用的移动宽带,通过无线路由器无线上网.上taobao.天猫都很快,但是一上京东.苏宁易购界面加载很慢,界面无法显示,怀疑是无线路由问题,然后直接通过网线相连接,发现问题消失,决定对无线路由软件版本 ...

  8. 安装cadence遇到vcredist.msi找不到问题

    在新装的win7 64位系统上安装cadence遇到了如下问题,最后一个群里面的大哥帮了大忙,解决办法如下: 用windowsinstallercleanup 将KB2467175清理掉再装caden ...

  9. Linux 的数字权限意义

    三个组 每个都有三个权限 r w x每个权限用二进制 0 和 1 标示 1即为有此权限 0 标示无权限  ower    group  other  r w x    r w x  r w x 每个组 ...

  10. 重温Javascript(一)-基本概念

    工作中要用到JavaScript,一组复习笔记. 一些看法 1. 想想JavaScript目前最常用的宿主环境,浏览器或者服务端V8,都是单线程,所以不用过多的考虑并发的问题,如果是协程来实现异步的方 ...