[leetcode]228. Summary Ranges区间统计
Given a sorted integer array without duplicates, return the summary of its ranges.
Example 1:
Input: [0,1,2,4,5,7]
Output: ["0->2","4->5","7"]
Explanation: 0,1,2 form a continuous range; 4,5 form a continuous range.
Example 2:
Input: [0,2,3,4,6,8,9]
Output: ["0","2->4","6","8->9"]
Explanation: 2,3,4 form a continuous range; 8,9 form a continuous range.
题意:
给定一个数组,统计其中元素的区间分布。
思路:
scan给定数组
若当前数字 = 前一个数字 + 1 ,则表示continuous range
若当前数字 != 前一个数字 + 1, 则表示不是continuous range, 即需要wrap前面的部分。
留心当for循环scan给定数组完毕,wrap了前面的部分,还需要把剩下的start 到 a.length-1 的部分加到result里
代码:
class Solution {
public List<String> summaryRanges(int[] a) {
List<String> result = new ArrayList<>();
if(a.length==0){return result;}
int start = a[0];
for(int i = 1; i < a.length; i++){
if(a[i] != a[i-1] +1){ // 数字不相连,则wrap前面的部分
group (result, start, a[i-1]);
start = a[i];
}
}
group (result, start, a[a.length-1]);// 不要忘记for循环之后剩下的一组
return result;
}
private void group (List<String>list, int start, int end){
if(start == end){
list.add(start+"");
}else{
list.add(start+"->"+end);
}
}
}
[leetcode]228. Summary Ranges区间统计的更多相关文章
- [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: ...
- C#解leetcode 228. Summary Ranges Easy
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 ...
- LeetCode(228) Summary Ranges
Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...
- (easy)LeetCode 228.Summary Ranges
Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...
- Java [Leetcode 228]Summary Ranges
题目描述: Given a sorted integer array without duplicates, return the summary of its ranges. For example ...
- 【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 ...
随机推荐
- java UTC时间和local时间相互转换
java UTC时间和local时间相互转换 1.local时间转UTC时间 /** * local时间转换成UTC时间 * @param localTime * @return */ public ...
- Frequently Asked Questions
转自:http://www.tornadoweb.org/en/stable/faq.html Frequently Asked Questions Why isn’t this example wi ...
- Ubuntu下Code::Blocks错误
#error This file requires compiler and library support for the ISO C++ 2011 standard. This support i ...
- css3选择符
常用的选择符 1.元素选择符 2.id选择符 3.class选择符 4.通配符 5.群组选择符 6.包含选择符 7.伪类选择符(伪类选择符CSS中已经定义好的选择器,不能随便取名) 8.伪对象选择符( ...
- c#面向对象基础5
字符串 string (1)字符串的不可变性 当给字符串重新赋值时,老值没有被销毁,而是重新开辟了一块新的空间去储存新值<------------------堆中,在栈中地址发生变化重新指向新 ...
- openx _金额
1/work/openx/lib/max/Delivery/log.php MAX_Delivery_log_logAdImpression MAX_Delivery_log_logAdRequ ...
- 黑盒测试用例设计——PICT
一.简单用法 在PICT安装目录下新建一个txt文本.把参数填入txt文本中.[内容包括(注意格式<ParamName> : <Value1>, <Value2> ...
- elasticsearch 基础知识汇总
索引分片: 从策略层面,控制分片分配的选择 磁盘限额 为了保护节点数据安全,ES 会定时(cluster.info.update.interval,默认 30 秒)检查一下各节点的数据目录磁盘使用情况 ...
- rabbitMQ 的基本知识
参考: https://www.cnblogs.com/dwlsxj/p/RabbitMQ.html
- restfull 风格 参考 https://blog.csdn.net/jaryle/article/details/52141097
https://www.cnblogs.com/xiaoxian1369/p/4332390.html :