LeetCode——Summary Ranges
Description:
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"].
确定有序数组的区间
需要注意各种边界细节,每次确定区间之后需要清空。
public class Solution {
public List<String> summaryRanges(int[] nums) {
List<String> list = new ArrayList<String>();
if(nums.length == 0)
return list;
if(nums.length == 1) {
list.add(nums[0]+"");
return list;
}
boolean flag = false;
StringBuilder ele = new StringBuilder();
for(int i=0; i<nums.length; i++) {
int start = 0, end = 0; if(!flag) {
start = nums[i];
flag = true;
ele.append(start+"");
} if(i<nums.length-1 && nums[i] != nums[i+1]-1) {
end = nums[i];
flag = false;
if(nums[i] != start) {
ele.append("->" + end);
}
} if(ele != null) {
if(i == nums.length-2 && nums[i] == nums[i+1]-1) {
end = nums[i+1];
ele.append("->" + end);
list.add(ele.toString());
ele.delete(0, ele.length());
break;
}
if(!flag || flag&&i==nums.length-1) {
list.add(ele.toString());
ele.delete(0, ele.length());
}
}
}
return list;
}
}
LeetCode——Summary Ranges的更多相关文章
- [LeetCode] Summary Ranges 总结区间
Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...
- (leetcode)Summary Ranges
Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...
- 228. [LeetCode] Summary Ranges
Given a sorted integer array without duplicates, return the summary of its ranges. Example 1: Input: ...
- LeetCode Summary Ranges (统计有序数列范围)
题意:给出个有序不重复数列(可能负数),用缩写法记录这个数列. 思路:找每个范围的起始和结束即可. class Solution { public: vector<string> summ ...
- leetcode面试准备:Summary Ranges
1 题目 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
Summary Ranges Given a sorted integer array without duplicates, return the summary of its ranges. Ex ...
- [LeetCode] Missing Ranges 缺失区间
Given a sorted integer array where the range of elements are [0, 99] inclusive, return its missing r ...
- leetcode-【中等题】228. Summary Ranges
题目: 228. Summary Ranges Given a sorted integer array without duplicates, return the summary of its r ...
随机推荐
- Jackson2.1.4 序列化格式化时间
public class User { private int id; private Date birthday; private double money; private String name ...
- synchronize模块
synchronize模块 使用rsync同步文件,其参数如下: archive: 归档,相当于同时开启recursive(递归).links.perms.times.owner.group.-D选项 ...
- 传说中的纯CSS圆角代码
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...
- tensorflow函数解析:Session.run和Tensor.eval的区别
tensorflow函数解析:Session.run和Tensor.eval 翻译 2017年04月20日 15:05:50 标签: tensorflow / 机器学习 / 深度学习 / python ...
- Mysql各种存储引擎的特性以及如何选择存储引擎
几个常用存储引擎的特点 下面我们重点介绍几种常用的存储引擎并对比各个存储引擎之间的区别和推荐使用方式. 特点 Myisam BDB Memory InnoDB Archive 存储限制 没有 没有 有 ...
- am335x i2c分析
/***************************************************************************** * am335x i2c分析 * i2c驱 ...
- 50个必备的实用jQuery代码段(转)
1. 如何创建嵌套的过滤器: //允许你减少集合中的匹配元素的过滤器, //只剩下那些与给定的选择器匹配的部分.在这种情况下, //查询删除了任何没(:not)有(:has) //包含class为“s ...
- Google Analytics10条有用教程(转)
几乎每个网站都会统计自身的浏览状况:日IP.PV.跳出率.转换率.浏览者属性等等.了解这些数据有助于更好地了解浏览者的属性.知道网站在什么地方存在缺陷,为更好地提供服务.提高网站收入都有所帮助. 对于 ...
- jquery -- checkbox选中无选中状态
最近在工作中使用jquery操作checkbox,使用下面方法进行全选.反选: var ischecked=allCheckObj.is(':checked'); ischecked?checksOb ...
- bootstrap -- css -- 辅助类
文本 文本颜色 如果文本是个链接,则鼠标移动到链接文本上的时候,文本会变暗 .text-muted:灰色 .text-primary:浅蓝色 .text-success:绿色 .text-info:深 ...