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. mongodb 基础语法

    参考原文:菜鸟教程 目录 一.数据库二.文档三.索引四.聚合 一.数据库 show dbs -- 查看所有数据库 use DATABASE_NAME -- 如果数据库不存在,则创建数据库,否则切换到指 ...

  2. 基本数据类型 list and tuple 04

    列表和元组 一,列表 1.列表 由[]括起来 可以存放各种数据类型:  存放量比较大 2.列表的索引和切片  列表也有索引 lst [i] i 即列表中各元素的位置 2.1列表的切片 lst[star ...

  3. hive export import

    create database target_db; drop table target_db.kylin_account; dfs -rm -r /tmp/kylin_account; export ...

  4. java——cmd命令编译带包名的源程序

    .java文件的绝对路径:C:\eclipse-workspace\test_01\src\test\try.java try.java的包名为:package test; 在cmd中 cd C:\e ...

  5. 查看Oracle当前连接数

    SQL> select count(*) from v$session #当前的连接数 SQL> Select count(*) from v$session where status=' ...

  6. spring MVC设置不拦截静态资源

    问题产生: 因为我们在web.xml中写了 拦截所有请求,当然包括了静态资源,所以页面需要引用css或js的话,该请求也会被拦截,例如: 在style.css中写一个简单样式,加个背景颜色  body ...

  7. Django-5.2 模型层 多表操作

    7.3 多表操作 一.创建模型 实例:我们来假定下面这些概念,字段和关系作者模型:一个作者有姓名和年龄.作者详细模型:把作者的详情放到详情表,包含生日,手机号,家庭住址等信息.作者详情模型和作者模型之 ...

  8. Zend Optimizer安装、配置

    Zend Optimizer用优化代码的方法来提高php应用程序的执行速度.实现的原理是对那些在被最终执行之前由运行编译器(Run-Time Compiler)产生的代码进行优化.这里,我们下载最新版 ...

  9. mysql中操作符LIKE与通配符%的使用

    mysql中通配符%用来通配其他字符,操作符LIKE用来查询字段中存在相同的字符 SELECT t.userId,t.cellphone,t.idNo,t.* FROM t_person t WHER ...

  10. Graphics绘制类及打印机设置相关

    Graphics 有两个多个方法 这里面介绍3个: 1.Graphics.drawString():绘制.画字符串........... e.Graphics.DrawString("新乡市 ...