228. Summary Ranges (everyday promlems) broken problems
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的更多相关文章
- leetcode-【中等题】228. Summary Ranges
题目: 228. Summary Ranges Given a sorted integer array without duplicates, return the summary of its r ...
- 【LeetCode】228. Summary Ranges 解题报告(Python)
[LeetCode]228. Summary Ranges 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/sum ...
- 【刷题-LeetCode】228. Summary Ranges
Summary Ranges Given a sorted integer array without duplicates, return the summary of its ranges. Ex ...
- Java for LeetCode 228 Summary Ranges
Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...
- LeetCode(228) Summary Ranges
Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...
- 228. Summary Ranges
Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...
- (easy)LeetCode 228.Summary Ranges
Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...
- 【LeetCode】228 - Summary Ranges
Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...
- Java [Leetcode 228]Summary Ranges
题目描述: Given a sorted integer array without duplicates, return the summary of its ranges. For example ...
随机推荐
- Linux内核硬件访问技术
① 驱动程序控制设备,主要是通过访问设备内的寄存器来达到控制目的.因此我们讨论如何访问硬件,就成了如何访问这些寄存器. ② 在Linux系统中,无论是内核程序还是应用程序,都只能使用虚拟地址,而芯片手 ...
- windows系统打开火狐浏览器提示“无法加载你的firefox配置文件”
win7系统自带IE浏览器,还是有部分用户使用不习惯,选择下载第三方浏览器,比如:火狐.谷歌.360浏览器等.最近有Win7系统用户在重新安装火狐浏览器后发现打不开,并提示“无法加载你的firefox ...
- ie中html页面无法加载css
今天写代码发生一个很尴尬的问题,码了一天的代码在ie下一调试居然没有样式,打开F12查看元素果然没有样式,在其他浏览器完全没问题,ie就出事. ie肯定没问题,问题还是处在代码上了,百度了一下说是把& ...
- 五种I/O模型的学习
来自 http://www.52im.net/thread-1935-1-1.html 4.互联网服务端处理网络请求的原理 首先看看一个典型互联网服务端处理网络请求的典型过程:<ignore ...
- 为什么 c++中函数模板和类模板的 声明与定义需要放到一起?
将模板的声明与定义写在一起实在很不优雅.尝试用“传统”方法,及在.h文件里声明,在.cpp文件里定义, 然后在main函数里包含.h头文件,这样会报链接错误.why!!!!!!!!!!!!! 这是因为 ...
- OS---存储器
1.存储器的层次结构 1.1 概述 理想情况下,存储器应当速度非常快.并且与处理器的速度匹配.容量大且价格低廉: 实际情况,无法满足上述三个条件: 于是在现在OS中,存储器采用 层次结构 来组织: ...
- javasript数据类型以及如何判断数据类型
在javascript里面一共有5种基本的数据类型,分别是:Number,String,Boolean,Null,Undefined7种引用类型,分别是:Object类型,Array类型,Date类型 ...
- density peak聚类算法
一个ppt很好讲解了density peak算法的要义:https://pan.baidu.com/s/1oCR-gF1o1kfV-L7HnIa8og 算法来源自论文:Clustering by fa ...
- Unity 物体旋转会发生变形
当游戏对象的 "父物体们" 有一个是缩放的,也就是Scale不是(1,1,1)的时候,旋转这个游戏对象它就会出现变形的现象.
- 牛客网Java刷题知识点之什么是单例模式?解决了什么问题?饿汉式单例(开发时常用)、懒汉式单例(面试时常用)、单例设计模式的内存图解
不多说,直接上干货! 什么是单例设计模式? 解决的问题:可以保证一个类在内存中的对象唯一性,必须对于多个程序使用同一个配置信息对象时,就需要保证该对象的唯一性. 如何保证? 1.不允许其他程序用new ...