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

Example 1:

Input:  [0,1,2,4,5,7]
Output: ["0->2","4->5","7"]
Explanation: 0,1,2 form a continuous range; 4,5 form a continuous range.

Example 2:

Input:  [0,2,3,4,6,8,9]
Output: ["0","2->4","6","8->9"]
Explanation: 2,3,4 form a continuous range; 8,9 form a continuous range.

broken solution : check the boundary

class Solution {
public List<String> summaryRanges(int[] nums) {
List<String> res = new ArrayList<>();
int n = nums.length;
if(n==0) return res;
else if(n==1) {
res.add(nums[0]+"");
return res;
}
int i = 0;
while(i<n-1){
//case for only n-1
if(nums[i] +1 == nums[i+1] ){ //
int start = nums[i];
i = i+1; while(nums[i] +1 == nums[i+1]){//i+1<n i++;
}
int end = nums[i];
String str = start + "->" + end;
res.add(str);
}
else if(nums[i] +1 != nums[i+1] ){
res.add(nums[i]+"");}
i++;
}
//check the last element
if(nums[n-1] == nums[n-2]+1) {
if(n>=3){
String[] temp = res.get(res.size()-1).split("->");
res.remove(res.size()-1);
res.add(temp[0] + "->" + nums[n-1]);
}else {
//n ==2
res.add(nums[n-2] + "->" + nums[n-1]);
}
}else {
res.add(nums[n-1]+"");
}
return res;
}
}

revision correct one: always check the boundary

two cases: 1: 1,2,3  2: [1,2,4,5,7]

class Solution {
public List<String> summaryRanges(int[] nums) {
List<String> res = new ArrayList<>();
int n = nums.length;
if(n==0) return res;
else if(n==1) {
res.add(nums[0]+"");
return res;
}
int i = 1;
while(i<=n-1){
//case for only n-1
if(nums[i-1] +1 == nums[i] ){ //
int start = nums[i-1];
i = i+1; while(i<n && nums[i-1] +1 == nums[i]){//i+1<n
i++;
}
int end = nums[i-1];
String str = start + "->" + end;
res.add(str);
}
else if(nums[i-1] +1 != nums[i] ){
res.add(nums[i-1]+"");}
i++;
}
//check the last element
if(nums[n-1] == nums[n-2]+1) { }else {
res.add(nums[n-1]+"");
}
return res;
}
}

two pointer solution

class Solution {
public List<String> summaryRanges(int[] nums) {
List<String> res = new ArrayList<>();
int n = nums.length;
if(n==0) return res;
else if(n==1) {
res.add(nums[0]+"");
return res;
}
//use two pointers
int i = 0;int j = 0;//i and j
while(i<n){
j = i+1;
while(j < n){
if(nums[j-1]+1 == nums[j]){
j++;
}else {
break;
}
}
if(j == i+1){
res.add(nums[i]+"");
}else {
res.add(nums[i]+"->"+nums[j-1]);
}
i = j;
}
return res;
}
}

ren ruoyousuopcheng, biypusuozhi

228. Summary Ranges (everyday promlems) broken problems的更多相关文章

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

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

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

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

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

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

  4. Java for LeetCode 228 Summary Ranges

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

  5. LeetCode(228) Summary Ranges

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

  6. 228. Summary Ranges

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

  7. (easy)LeetCode 228.Summary Ranges

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

  8. 【LeetCode】228 - Summary Ranges

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

  9. Java [Leetcode 228]Summary Ranges

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

随机推荐

  1. KS光盘制作 for rhel6.5 and rhel7.2

    ############################## RHEL6.5 KS光盘制作--1.复制光盘到本地mkdir -p /opt/rhel6mount /dev/cdrom /mediacp ...

  2. logback日志简记

    %date{HH:mm:ss.SSS} [%thread] %-5level %logger{20}:%line - %msg%n 输出: 09:54:09.657 [main] INFO  c.e. ...

  3. Linux学习grep,sed,awk工具的使用

    1.grep:根据模式搜索文本并将符合模式的文本显示出来(pattern模式:由文本字符和正则表达式元字符所匹配出来的条件) 注:alias grep='grep --color' -c:打印符合要求 ...

  4. redis的三种启动方式,个人常用第二种

    redis的启动方式1.直接启动  进入redis根目录,执行命令:  #加上‘&’号使redis以后台程序方式运行 1 ./redis-server & 2.通过指定配置文件启动  ...

  5. 【Java】Java中的Collections类——Java中升级版的数据结构【转】

    一般来说课本上的数据结构包括数组.单链表.堆栈.树.图.我这里所指的数据结构,是一个怎么表示一个对象的问题,有时候,单单一个变量声明不堪大用,比如int,String,double甚至一维数组.二维数 ...

  6. 性能测试工具Jmeter01-简介

    Jmeter介绍: Apache JMeter是Apache组织的开放源代码项目,是一个纯Java桌面应用,用于压力测试和性能测试.最初被设计用于Web应用测试后来扩展到其它测试领域 Jmeter有啥 ...

  7. java Redis工具类

    redis就是一个nosql数据库,做存储做缓存的,java代码中就是嵌入了一个客户端,读取与存储数据而已. 先来一个简单的工具类: package com.ming.redis; import re ...

  8. DEDE SQL标签可以获取文档静态链接地址

    在DedeCMS的系统里面,我可以通过由使用SQL语句来配合织梦标签进行更多的个性化调用.比如:推荐会员.推荐企业等.但是我们发现文档链接的底层模板地址的是动态的,那么我们要如何来进行转换,让他链接到 ...

  9. 13、Selenium+python+API分类总结

    Selenium+python+API分类总结 http://selenium-python.readthedocs.org/index.html 分类 方法 方法描述 客户端操作 __init__( ...

  10. mysql 使用 unix 方式显示日期和时间

    1.UNIX中文为时间戳.该方式显示从1970年1月1日开始经过的秒数. 2.函数 UNIX_TIMESTAMP() 返回时间戳格式的时间, FROM_UNIXTIME() 将时间戳格式的时间转换为普 ...