Summary Ranges 解答
Question
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"].
Solution
Traverse array, remember not to forget the last element.
public class Solution {
public List<String> summaryRanges(int[] nums) {
int length = nums.length;
List<String> result = new ArrayList<String>();
if (length == 0)
return result;
StringBuffer tmp = new StringBuffer();
int startNum = nums[0];
int endNum = nums[0];
if (length == 1) {
tmp.append(startNum);
result.add(tmp.toString());
return result;
}
for (int i = 0; i < length - 1; i++) {
if (nums[i] != nums[i + 1] - 1) {
tmp = new StringBuffer();
endNum = nums[i];
if (startNum == endNum) {
tmp.append(startNum);
} else {
tmp.append(startNum);
tmp.append("->");
tmp.append(endNum);
}
result.add(tmp.toString());
startNum = nums[i + 1];
}
}
tmp = new StringBuffer();
endNum = nums[length - 1];
if (startNum == endNum) {
tmp.append(startNum);
} else {
tmp.append(startNum);
tmp.append("->");
tmp.append(endNum);
}
result.add(tmp.toString());
return result;
}
}
Summary Ranges 解答的更多相关文章
- leetcode-【中等题】228. Summary Ranges
题目: 228. Summary Ranges Given a sorted integer array without duplicates, return the summary of its r ...
- Missing Ranges & Summary Ranges
Missing Ranges Given a sorted integer array where the range of elements are [lower, upper] inclusive ...
- 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] 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 ...
- 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 ...
随机推荐
- C#开发客户端、JAVA和tomcat开发服务端
hessian入门,Hello和文件上传范例,C#客户端+Java Tomcat后台 2.Hello范例1)后台--定义Java接口:package org.migle.hessian; public ...
- iOS 推送证书制作 (JAVA/PHP)
// aps_development.cer 转化成pem openssl x509 -in aps_development.cer -inform der -out PushChatCert.pem ...
- 再一次强调,ORACLE外键必须加索引
外键加索引是常识,必须牢记.本来不想写这样的简单案例.可是连续遇到好几起外键不加索引导致性能问题,所以还是写一下. 一个兄弟问我 delete from Sa_Sales_Comm_Detail s ...
- 给SharePoint页面加入自己定义页脚Custom footer
给SharePoint页面加入自己定义页脚Custom footer 在公司做站点设计项目时,须要在页面上加入页脚. 非常多人都把页脚忽视了,认为没什么多大用处,事实上 ...
- dev grdicontrol 根据条件改变行颜色,字体颜色等
dev grdicontrol 根据条件改变行背景 dev grdicontrol 根据条件改变字体颜色 dev grdicontrol 根据条件改变某个单元格背景 不用写代码 打开gridc ...
- JSTL 格式化输出 Calendar
今天遇到一个jstl在页面输出Calendar对象的问题,网上各种百度都说不能直接格式化,但是经过自己的尝试,原来是可以直接格式化的. 做个备忘吧. 对象的createTime字段类型是Calenda ...
- CGBitmapContextCreate函数
CGBitmapContextCreate函数参数详解 函数原型: CGContextRef CGBitmapContextCreate ( void *data, size_t width, ...
- css-布局1-基本属性
<!DOCTYPE html>CSS4-布局1-基本属性 属性:displayvisibilityfloatclear HTML元素类型:行内元素,块级元素 块级元素:最大的区别:换行行内 ...
- 如何用浏览器调试js代码
按F12打开调试工具
- nginx搭建flv、mp4流媒体服务
1.安装pcre-8.33.tar.bz2 #tar -xvf 1.pcre-8.33.tar.bz2 #cd pcre-8.33/ #./configure #make && mak ...