leetcode解题报告228——Summary Ranges
2015-06-28 00:42
405 查看
Given a sorted integer array without duplicates, return the summary of its ranges.
For example, given [0,1,2,4,5,7], return [“0->2”,”4->5”,”7”].
题目比较easy直接上代码。解法较啰嗦。
For example, given [0,1,2,4,5,7], return [“0->2”,”4->5”,”7”].
题目比较easy直接上代码。解法较啰嗦。
package array; import java.util.ArrayList; import java.util.List; public class SummaryRanges { class Str { private int pre; private int aft; private int temp; public String toString() { if(this.pre == this.aft){ return ""+this.pre; } return this.pre + "->" + this.aft; } } public List<String> summaryRanges(int[] nums) { List<String> res = new ArrayList<String>(); if (nums.length < 1) { return res; } else { Str str = new Str(); str.pre=nums[0]; str.temp=nums[0]; for (int i = 1; i < nums.length; i++) { if (nums[i] - str.temp == 1) { str.temp = nums[i]; } else { str.aft = str.temp; res.add(str.toString()); str.pre = nums[i]; str.temp = nums[i]; } } str.aft = str.temp; res.add(str.toString()); } return res; } public static void main(String[] args) { int[] arr = { 0, 1, 2, 4, 5, 7}; System.out.println(new SummaryRanges().summaryRanges(arr)); } }
相关文章推荐
- Zxing在CameraManager.get().closeDriver()报错
- Python 3.5 终于将 Type Hint(类型提示)官方化
- php页面静态化—优化页面响应时间
- Android横向滑动选项卡
- [综] Sparse Representation 稀疏表示
- SDWebImage
- 解决安装laravel/homestead vagrant环境报"A VirtualBox machine with the name 'homestead' already exists."的错误
- struts2 json includeProperties返回list为空
- 苹果Mac OS系统终端命令大全介绍
- Codeforces Round #310 (Div. 1) A B C
- 记一次理想浪漫的毕旅
- vijos 1054 牛场围栏 【想法题】
- 字符串右移函数
- .Net 配置文件——继承ConfigurationSection实现自定义处理类处理自定义配置节点
- .Net 配置文件——继承ConfigurationSection实现自定义处理类处理自定义配置节点
- iOS开发——CoreBluetooth蓝牙开发(三)
- 给Android的LinearLayout添加点击事件
- 利用TLE数据确定卫星轨道(1)-卫星轨道和TLE
- Java IO流 合并流和切割流
- Redis 哈希对象