[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 ...
随机推荐
- 6.19-response(响应),session(会话技术,服务器端技术) 内置对象,application(内置对象),pageContext (内置对象),cookie(客户端技术)
一.response(响应) 页面重定向 response.sendRedirect(""); 转发: request.getRequestDispatcher("&qu ...
- 函数图 https://www.processon.com/mindmap/5b5077fae4b040415ae39c64
---恢复内容结束---
- c#面向对象基础4
一.namespace 命名空间 作用:解决不同类重名的问题 我们可以认为类是属于命名空间的 当我们需要再一个类中与另一个类建立关系时,通过命名空间来区别不同的类.所以需要我们这样做:导入命名空间 ...
- Redis 几个全局命令, 以及事物
1, 清空当前数据库的所有数据 => flushdb 2, 清空所有数据库的所有数据 => flushall 3, key 值检索命令 => scan num match if 会 ...
- JAVA JDBC 各大数据库的连接字符串和连接类
oracle: driverClass:oracle.jdbc.OracleDriver url:jdbc:oracle:thin:@127.0.0.1:1521:dbname mys ...
- 机器学习入门-随机森林温度预测的案例 1.datetime.datetime.datetime(将字符串转为为日期格式) 2.pd.get_dummies(将文本标签转换为one-hot编码) 3.rf.feature_importances_(研究样本特征的重要性) 4.fig.autofmt_xdate(rotation=60) 对标签进行翻转
在这个案例中: 1. datetime.datetime.strptime(data, '%Y-%m-%d') # 由字符串格式转换为日期格式 2. pd.get_dummies(features) ...
- 6 python 继承与派生
1.什么是继承? 继承指的是类与类之间的关系,是一种什么“是”什么的关系,继承的功能之一就是用来解决代码重用问题 继承是一种创建新类的方式,在python中,新建的类可以继承一个或多个父类 父类又可以 ...
- 为什么NoSql快--磁盘顺序写
数据写入方式 1. update-in-place原地更新 2. append-only btree/copy on write tree顺序文件末尾追加 数据被按照特定方式放置,提升读性能, ...
- MySQL的事务处理及隔离级别
事务是DBMS得执行单位.它由有限得数据库操作序列组成得.但不是任意得数据库操作序列都能成为事务.一般来说,事务是必须满足4个条件(ACID) 原子性(Autmic):事务在执行性,要 ...
- <U+FEFF> character showing up in files. How to remove them?
You can easily remove them using vim, here are the steps: 1) In your terminal, open the file using v ...