Java [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"].
解题思路:
相邻的两个数字若差距为1,则继续向后读,直到不满足条件为止,则把起始到终点的结果表示出来。如此,直到读到数组的最后位置为止。
代码如下:
public class Solution {
public List<String> summaryRanges(int[] nums) {
List<String> result = new ArrayList<String>();
int length = nums.length;
for(int i = 0; i < nums.length; i++){
int front = nums[i];
while(i + 1 < nums.length && nums[i + 1] - nums[i] == 1)
i++;
int end = nums[i];
if(front == end)
result.add(front + "");
else
result.add(front + "->" + end);
}
return result;
}
}
Java [Leetcode 228]Summary Ranges的更多相关文章
- [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. 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. For example, give ...
- (easy)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. 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 ...
随机推荐
- ubuntu中磁盘挂载与卸载
问题描述: ubuntu中磁盘的挂载和卸载 问题解决: (1)ubuntu中磁盘挂载 注: 如上所示,使用命令df查看磁盘使用情况 ...
- [转载]MongoDB 标准连接字符串
MongoDB 标准连接字符串 mongodb://[username:password@]host1[:port1][,host2[:port2],…[,hostN[:portN]]][/[data ...
- 盘点 DevOps 世界的杰出女性(一)
[编者按]IT 领域从来不缺乏杰出的女性存在,近日,DevOps.com 主编 Alan Shimel 盘点了 DevOps 领域的杰出女性,首期为六个,本文系 OneAPM 工程师编译整理. 以下为 ...
- HDU 3397 Sequence operation (区间合并,操作比较多)
费了我一天半的时间,到处debug,后来才发现,主要是建树的时候只在叶子节点对lazy1和lazy2进行初始化了,父节点都没初始化...晕. 具体见代码吧. #include <iostream ...
- POJ 2246 Matrix Chain Multiplication(结构体+栈+模拟+矩阵相乘)
题意:给出矩阵相乘的表达式,让你计算需要的相乘次数,如果不能相乘,则输出error. 思路: 参考的网站连接:http://blog.csdn.net/wangjian8006/article/det ...
- HDU 2473 Junk-Mail Filter(并查集+删点,设立虚父节点/找个代理)
题意:有N封邮件, 然后又两种操作,如果是M X Y , 表示X和Y是相同的邮件.如果是S X,那么表示对X的判断是错误的,X是不属于X当前所在的那个集合,要把X分离出来,让X变成单独的一个.最后问集 ...
- POJ1860Currency Exchange(SPFA)
http://poj.org/problem?id=1860 题意: 题目中主要是说存在货币兑换点,然后现在手里有一种货币,要各种换来换去,最后再换回去的时候看能不能使原本的钱数增多,每一种货币都有 ...
- Android 解决ListView中每一项与button冲突
在listView的item里面如果有button,ImageButton等控件,会使得ListView不会被点击,解决方法是: ①在Button上面添加属性 android:focusable=&q ...
- Java程序员学C#基本语法两个小时搞定(对比学习)
对于学习一门新的语言,关键是学习新语言和以前掌握的语言的区别,但是也不要让以前语言的东西,固定了自己的思维模式,多看一下新的语言的编程思想. 1.引包 using System;java用import ...
- [hackerrank]The Love-Letter Mystery
https://www.hackerrank.com/contests/w3/challenges/the-love-letter-mystery 简单题. #include <cstdlib& ...