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. Codeforces 316C2 棋盘模型

    Let's move from initial matrix to the bipartite graph. The matrix elements (i, j) for which i + j ar ...

  2. photoshop特效字体

    一.3D效果字 3D效果文字给人以纵伸感.立体感和真实感,是商家常用到的一种宣传文字.虽然Photoshop软件是平面软件,但是在制作3D效果文字时却游刃有余. 3D效果字的制作可分以下三步完成. 输 ...

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

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

  4. Pycharm在线/手动离线安装第三方库-以scapy为例(本地离线添加已经安装的第三方库通过添加Path实现)

    在线安装运行Pycharm,打开需要添加scapy文件的项目,以TestScapy为例           点击工具栏的File选项,选中Settings,单击打开                  ...

  5. 第五章:引用类型(一)-Object和Array

    引用类型 引用类型的值(对象)是引用类型的一个实例 引用类型是一种数据结构,用于将数据与功能组织在一起 也常被称为类, Object 对象的两种创建方式 使用new操作符 对象字面量表示法 Array ...

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

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

  7. spring webapp的配置文件放置在项目外的方法

    在web.xml中,填写     <context-param>         <param-name>CFG_HOME</param-name>         ...

  8. Unity Transform

    public class PlayerControll : MonoBehaviour { Transform playerTransform; Animation playerAnimation; ...

  9. Hash表的原理

    哈希的概念:Hash,一般翻译做“散列”,也有直接音译为“哈希”的,就是把任意长度的输入(又叫做预映射, pre-image),通过散列算法,变换成固定长度的输出,该输出就是散列值.这种转换是一种压缩 ...

  10. css有关鼠标移动上去图片变透明度变化

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...