【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"].
My Solution:
vector<string> summaryRanges(vector<int>& nums)
{
vector<string> ret;
if(nums.empty())return ret;
int low=nums[];
bool flag=false;
for(int i=;i<nums.size();i++){
while(i<nums.size() && nums[i]==nums[i-]+)i++;
int high=nums[i-];
if(low!=high)
ret.push_back(to_string(low)+"->"+to_string(high));
else
ret.push_back(to_string(low));
if(i==nums.size()){
flag=true;
break;
}
low=nums[i];
}
if(flag==false)ret.push_back(to_string(low));
return ret;
}
Better Solution:
class Solution {
public:
vector<string> summaryRanges(vector<int>& nums) {
vector<string> vec;
if(nums.empty())
return vec; int low=nums[],high=nums[];
for(int i = ; i < nums.size(); i ++)
{
if(nums[i]-nums[i-] == )high=nums[i];
else
{
string range;
if(low != high)
range = to_string(low) + "->" + to_string(high);
else
range = to_string(low);
vec.push_back(range);
low = nums[i];
high = nums[i];
}
}
string range;
if(low != high)
range = to_string(low) + "->" + to_string(high);
else
range = to_string(low);
vec.push_back(range);
return vec;
}
};
【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
题目: 228. Summary Ranges Given a sorted integer array without duplicates, return the summary of its r ...
- 【LeetCode】163. Missing Ranges 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 遍历 日期 题目地址:https://leetcode ...
- leetcode面试准备:Summary Ranges
1 题目 Given a sorted integer array without duplicates, return the summary of its ranges. For example, ...
- 【LeetCode】 454、四数之和 II
题目等级:4Sum II(Medium) 题目描述: Given four lists A, B, C, D of integer values, compute how many tuples (i ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
随机推荐
- html 页面表单如果是disabled,则不能提交到服务器端,request.getParameter得到的将为null
html 页面表单如果是disabled,则不能提交到服务器端,request.getParameter得到的将为null 解决方法:使用hidden 利用javascript赋值,传递到后台
- VNC常用操作及常见问题解决办法汇总
VNC登录用户缺省是root,但在安装oracle时必须用oracle用户的身份登录,下面我们就以oracle为例说明如何配置VNC,从而可以使用不同的用户登录到主机.步骤描述如下: 步骤一:修 ...
- 很实用的js限制不让输入其他字符,只让输入数字和 js生成UUID
onkeyup="this.value=this.value.replace(/\D/g,'')" js生产UUID var createUUID = (function (uui ...
- 38-语言入门-38-Coin Test
题目地址: http://acm.nyist.net/JudgeOnline/problem.php?pid=204 描述As is known to all,if you throw a co ...
- 视觉(3)blepo
视觉(3)blepo 把matlab转成c程序有好办法了,从网上下载了一个函数库blepo,转换为c几乎是一行对一行,openCv经常涉及到的内存申请和释放这里都不用管.高兴!看看这段程序比较一下差别 ...
- android 事件处理机制之requestDisallowInterceptTouchEvent
当手指触摸到屏幕时,系统就会调用相应View的onTouchEvent,并传入一系列的action.当有多个层级的View时,在父层级允许的情 况下,这个action会一直向下传递直到遇到最深层的Vi ...
- 线程中无法实例化spring注入的服务的解决办法
问题描述 在Java Web应用中采用多线程处理数据,发现Spring注入的服务一直报NullPointerException.使用注解式的声明@Resource和XML配置的bean声明,都报空指针 ...
- Qt之命令行编译(nmake)
简述 前两节讲解了如何在Visual Studio和Qt Creator中搭建Qt开发环境,并分享了我们第一个小程序-Hello World. 下面分享如何使用命令行来编译Qt程序.当然,MSVC和M ...
- Calculate drive total/free/available space
using System; using System.Collections.Generic; using System.IO; using System.Text; namespace Consol ...
- RPi 2B Android telnet ssh
/*********************************************************************** * RPi 2B Android telnet ssh ...