228 Summary Ranges 汇总区间
给定一个无重复元素的有序整数数组,返回数组中区间范围的汇总。
示例 1:
输入: [0,1,2,4,5,7]
输出: ["0->2","4->5","7"]
示例 2:
输入: [0,2,3,4,6,8,9]
输出: ["0","2->4","6","8->9"]
详见:https://leetcode.com/problems/summary-ranges/description/
Java实现:
class Solution {
public List<String> summaryRanges(int[] nums) {
List<String> res=new ArrayList<String>();
int n=nums.length;
int i=0;
while(i<n){
int j=1;
while(i+j<n&&nums[i+j]-nums[i]==j){
++j;
}
res.add(j==1?String.valueOf(nums[i]):String.valueOf(nums[i])+"->"+String.valueOf(nums[i+j-1]));
i+=j;
}
return res;
}
}
C++实现:
class Solution {
public:
vector<string> summaryRanges(vector<int>& nums) {
vector<string> res;
int i=0,n=nums.size();
while(i<n)
{
int j=1;
while(i+j<n&&nums[i+j]-nums[i]==j)
{
++j;
}
res.push_back(j==1?to_string(nums[i]):to_string(nums[i])+"->"+to_string(nums[i+j-1]));
i+=j;
}
return res;
}
};
参考:https://www.cnblogs.com/grandyang/p/4603555.html
228 Summary Ranges 汇总区间的更多相关文章
- [LeetCode] 228. Summary Ranges 总结区间
Given a sorted integer array without duplicates, return the summary of its ranges. Example 1: Input: ...
- Leetcode228. Summary Ranges汇总区间
给定一个无重复元素的有序整数数组,返回数组区间范围的汇总. 示例 1: 输入: [0,1,2,4,5,7] 输出: ["0->2","4->5",& ...
- 【LeetCode】228. Summary Ranges 解题报告(Python)
[LeetCode]228. Summary Ranges 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/sum ...
- leetcode-【中等题】228. Summary Ranges
题目: 228. Summary Ranges Given a sorted integer array without duplicates, return the summary of its r ...
- 【刷题-LeetCode】228. Summary Ranges
Summary Ranges Given a sorted integer array without duplicates, return the summary of its ranges. Ex ...
- LeetCode 228. Summary Ranges (总结区间)
Given a sorted integer array without duplicates, return the summary of its ranges. Example 1: Input: ...
- [leetcode]228. Summary Ranges区间统计
Given a sorted integer array without duplicates, return the summary of its ranges. Example 1: Input: ...
- [LeetCode] Summary Ranges 总结区间
Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...
- Java for LeetCode 228 Summary Ranges
Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...
随机推荐
- JNI返回复杂对象之中的一个
需求: 首先说需求.近期接手一个项目.要在底层解析二进制数据,数据结构比較负责,因为server是c++server,加之開始没有考虑到移动端开发,所以协议有点扯蛋.大体是这种,一个数据包里面是map ...
- superCleanMaster
https://github.com/eltld/superCleanMaster
- gvoory脚本中关于HttpClient使用详解实例
一.gvoory脚本中关于HttpClient使用详解实例 HttpClient:是一个接口 首先需要先创建一个DefaultHttpClient的实例 HttpClient httpClient=n ...
- 基于Cocos2dx + box2d 实现的愤慨的小鸟Demo
1. Demo初始界面 2. 游戏界面 3. 精确碰撞检測 4. 下载 压缩文件文件夹 AngryBird source 愤慨的小鸟Demo源码,基于Cocos2dx C++,以及box2d技 ...
- ie6不支持png图片的解决办法
在head里引入png.js文件 <!--[if lte IE 6]> <script type="text/javascript" src="js/P ...
- Restrictions.or多个条件用法
两个条件或查询: Restrictions.or(Restrictions.in("username",list1),Restrictions.idEq(1)); 三个或多个条件查 ...
- 两大数相乘 -- javascript 实现
(function (){ var addLarge = function(n1,n2){ var carry = 0; var ret = ""; n1=n1.toString( ...
- Facebook Flux 分析
首先是actions使用了Dispatcher来定义分发事件, Store在Dispatcher里注册自己的ActionType,收到对应的Action后修改Store内部的结构 Store emit ...
- YTU 1010: 目标柏林
1010: 目标柏林 时间限制: 1000 Sec 内存限制: 64 MB 提交: 32 解决: 15 题目描述 1945年初,苏军和英美联军已从东西两面攻入德国国境. 4月初,在苏军和英美联军的 ...
- mac终端命令加密压缩文件为zip包
mac终端命令加密压缩文件为zip包,命令如下: zip -e ~/desktop/a.zip b.doc c.txt d.sql 注释:a.zip为加密后的文件 b.doc c.txt d.sql为 ...