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. HDU 3709 Balanced Number 求区间内的满足是否平衡的数量 (数位dp)

    平衡数的定义是指,以某位作为支点,此位的左面(数字 * 距离)之和 与右边相等,距离是指某位到支点的距离; 题意:求区间内满足平衡数的数量 : 分析:很好这又是常见的数位dp , 不过不同的是我们这次 ...

  2. Java中Array与ArrayList的主要区别

    1)精辟阐述: 可以将 ArrayList想象成一种"会自动扩增容量的Array". 2)Array([]):最高效:但是其容量固定且无法动态改变:      ArrayList: ...

  3. Ansible故障

    常见问题一: [root@m01 ~]# ansible  -k 172.16.1.51 -m ping SSH password: [WARNING]: No hosts matched, noth ...

  4. Hello World 十大秘密

    #include <stdio.h> int main(int argc, char* argv[], char* env[]) { printf("Hello World\n& ...

  5. 关于box-shadow、border-radius不兼容ie8的解决办法

    本来从css3兼容ie9+挺好的,可是总有一些共识要求ie8+,于是就有了我们的苦逼的找解决办法.之前在网上查到一些说用 PIE.htc. But 我就是按照他说的写的没有管用.请教了一下别人才会写了 ...

  6. windows环境搭建禅道项目管理工具

    zentao官网的几个网址 http://www.zentao.net/ http://www.zentao.net/article-view-79863.html   搭建环境需要下载两个文件 1) ...

  7. spring aop 原理学习

    @EnableAspectJAutoProxy: @Import(AspectJAutoProxyRegistrar.class) 实际是创建了一个以org.springframework.aop.c ...

  8. Android NDK开发 环境配置(一) 之多重CPU的兼容性

    今天我学习Android Studio当中的NDK,为什么要学习NDK呢,是因为领导给我提了一个BUG,这个BUG就是Android 多重CPU怎样兼容性,我现在先说一下,Android Studio ...

  9. [转]Tetris(俄罗斯方块) in jQuery/JavaScript!

    本文转自:http://pwwang.com/2009/10/25/tetris-in-jquery-javascript/ All in jQuery/JavaScript + HTML! Demo ...

  10. angularjs string format

    用惯了C#的string.format,在angularjs中还不太习惯字符串的拼接,还好可以自定义String.Format String.format = function() { ) retur ...