(easy)LeetCode 228.Summary Ranges
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"].
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and creating all test cases.
代码如下:
public class Solution {
public List<String> summaryRanges(int[] nums) {
List<String>list=new ArrayList<String>();
int len=nums.length;
if(len==0) return list;
int begin=nums[0];
int end=nums[0];
for(int i=0;i<len;i++){
begin=nums[i];
while(i+1<len && nums[i+1]-nums[i]==1)
i++;
end=nums[i];
String s=null;
if(begin!=end)
s=""+begin+"->"+end;
else
s=""+begin;
list.add(s);
}
return list;
}
}
运行结果:

(easy)LeetCode 228.Summary Ranges的更多相关文章
- [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 ...
- LeetCode 228. Summary Ranges (总结区间)
Given a sorted integer array without duplicates, return the summary of its ranges. Example 1: Input: ...
- 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 ...
- Java [Leetcode 228]Summary Ranges
题目描述: Given a sorted integer array without duplicates, return the summary of its ranges. For example ...
- [leetcode]228. Summary Ranges区间统计
Given a sorted integer array without duplicates, return the summary of its ranges. Example 1: Input: ...
- 【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 ...
随机推荐
- JavaScript中类的实现机制
理解类的实现机制在前面已经讲过,在JavaScript中可以使用function关键字来定义一个“类”.现在介绍如何为类添加成员.其过程很简单,在函数内通过this指针引用的变量或者方法都会成为类的成 ...
- 【Reporting Services 报表开发】— 表达式
一.常用的SSRS原始函数可以打开文本框的表达式中看到,如图1 图1 如下为SSRS中设计报表时常用的运算函数: 运算符/函数 说明 + 前后位数字则为加法,前后为字符串则为链接符号 - 数值减法 * ...
- 转(linux shell)
请把如下字符串 stu494 e222f stu495 bedf3 stu496 92236 stu497 49b91 转为如下形式: stu494=e222f stu495=bedf3 stu496 ...
- R(八): R分词统计-老九门
分析文本内容基本的步骤:提取文本中的词语 -> 统计词语频率 -> 词频属性可视化.词频:能反映词语在文本中的重要性,一般越重要的词语,在文本中出现的次数就会越多.词云:让词语的频率属性可 ...
- WINRARA 排除 .svn 文件夹
加入-x*\.svn -x*\.svn\* 即可: rar.exe u -m3 -s -r -o+ -x*.db -x*.zip -x*.rar -x*\.svn -x*\.svn\* zmv9net ...
- HackerRank "Chocolate in Box" !
XOR -> 0 is the key (make it even pair): http://www.cnblogs.com/lautsie/p/3908006.html Something ...
- 剑指offer系列60---第一个只出现一次的字符
[题目]在一个字符串(1<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符的位置. * 若为空串,返回-1.位置索引从0开始 * [思路]1 首先遍历字符串数组,添 ...
- postgresql plpythonu例子
以下代码仅作为参考之用 select md5, crc32, record->'UserModerAnalysis'->'base_info'->'file_malware' as ...
- Exception error message with incorrect line number
In Release mode the number in front of the exception is NOT the line of code. Instead it's an offset ...
- SecureCRT控制台显示中文字符的设置