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 解答的更多相关文章

  1. leetcode-【中等题】228. Summary Ranges

    题目: 228. Summary Ranges Given a sorted integer array without duplicates, return the summary of its r ...

  2. Missing Ranges & Summary Ranges

    Missing Ranges Given a sorted integer array where the range of elements are [lower, upper] inclusive ...

  3. leetcode面试准备:Summary Ranges

    1 题目 Given a sorted integer array without duplicates, return the summary of its ranges. For example, ...

  4. 【LeetCode】228. Summary Ranges 解题报告(Python)

    [LeetCode]228. Summary Ranges 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/sum ...

  5. 【刷题-LeetCode】228. Summary Ranges

    Summary Ranges Given a sorted integer array without duplicates, return the summary of its ranges. Ex ...

  6. [LeetCode] Summary Ranges 总结区间

    Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...

  7. Java for LeetCode 228 Summary Ranges

    Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...

  8. Summary Ranges

    Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...

  9. (leetcode)Summary Ranges

    Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...

随机推荐

  1. ps&&/proc/pid/xxx

    ps 如果想看一个进程的启动时间,可以用lstart来看 [root@jiangyi02.sqa.zmf /home/ahao.mah] #ps -eo pid,lstart,etime,cmd |g ...

  2. poj 3616 Milking Time(dp)

    Description Bessie ≤ N ≤ ,,) hours (conveniently labeled ..N-) so that she produces as much milk as ...

  3. execl csv导出

    方维js方法:function export_csv() { var inputs = $(".search_row").find("input:[type!='chec ...

  4. IOS 判断设备类型

    - (NSString*)deviceString { // 需要#import "sys/utsname.h" struct utsname systemInfo; uname( ...

  5. Android设备的ID

    Android的开发者在一些特定情况下都需要知道手机中的唯一设备ID.例如,跟踪应用程序的安装,生成用于复制保护的DRM时需要使用设备的唯一ID.在本文档结尾处提供了作为参考的示例代码片段. 范围 本 ...

  6. NO.14 两个div并排,左边为绝对宽度,右边为相对宽度

    两个div并排,左边为绝对宽度,右边为相对宽度,这个问题,我也经常遇到,我一般的处理方法是将最大的容器padding-left固定宽度,左边的固定宽度的一块position:absolute,然后ri ...

  7. ajax参数中出现空格

    jquery中发起ajax请求时的参数名中不能有空格.如果是get请求参数的中的空格会变成“+”符而在post请求中看不到这种变化,但无论哪种情况都无法与服务接口的参数就行匹配(此时进行调试也不会触发 ...

  8. Button简单实例1

    1.XML按钮定义 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" an ...

  9. A Bit Of Knowledge

    iOS推崇使用png格式的图片,说这样不会失帧 imageNamed 和 imageWithContentOfFile的区别 imageNamed会使用系统缓存,对重复加载的图片速度会快一些,效果好. ...

  10. (转) Friendship and inheritance

    原地址: http://www.cplusplus.com/doc/tutorial/inheritance/ Friend functions In principle, private and p ...