228. Summary Ranges
题目:
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"].
链接: http://leetcode.com/problems/summary-ranges/
题解:
总结Range。也是从头到尾走一遍。当nums[i] - 1> nums[i - 1],我们处理之前的数字们。当遍历到最后一个元素的时候也要考虑如何处理。
Time Complexity - O(n), Space Complexity - O(1)。
public class Solution {public List<String> summaryRanges(int[] nums) {List<String> res = new ArrayList<>();if(nums == null || nums.length == 0)return res;int lo = 0;StringBuilder sb = new StringBuilder();for(int i = 0; i < nums.length; i++) {if(i > 0 && (nums[i] - 1 > nums[i - 1])) {if(lo == i - 1)res.add(Integer.toString(nums[lo]));else {res.add(sb.append(nums[lo]).append("->").append(nums[i - 1]).toString());sb.setLength(0);}lo = i;}if(i == nums.length - 1) {if(lo == i)res.add(Integer.toString(nums[lo]));elseres.add(sb.append(nums[lo]).append("->").append(nums[i]).toString());}}return res;} }
二刷:
- 方法和一刷一样。我们主要使用一个变量lo来保存每个interval的左边界。
- 每次当 i > 0并且 nums[i] - 1 > nums[i]的时候,我们进行判断
- 假如 lo = i - 1,那么我们只有一个数字,直接把这个数字加入到结果
- 否则 我们要把 nums[lo] + "->" + nums[i - 1]这个字符串加入到结果
- 更新 lo = i
- 当i = len - 1的时候,我们也要根据上面的逻辑判断一遍
- 最后返回结果.
Java:
可以使用StringBuilder来减少空间复杂度。
Time Complexity - O(n), Space Complexity - O(1)。
public class Solution {public List<String> summaryRanges(int[] nums) {List<String> res = new ArrayList<>();if (nums == null || nums.length == 0) {return res;}int lo = 0, len = nums.length;for (int i = 0; i < len; i++) {if (i > 0 && (nums[i] - 1 > nums[i - 1])) {if (lo == i - 1) {res.add(nums[lo] + "");} else {res.add(nums[lo] + "->" + nums[i - 1]);}lo = i;}if (i == len - 1) {if (lo == len - 1) {res.add(nums[lo] + "");} else {res.add(nums[lo] + "->" + nums[len - 1]);}}}return res;} }
三刷:
不知道上面在写什么...这回主要使用一个变量count和一个StringBuilder sb。遍历整个数组,当count = 0的时候,我们在sb中加入当前nums[i]。 当nums[i] - nums[i - 1]时,我们增加count。否则,这时nums[i] - nums[i - 1] > 1,假如count > 1,则形成了一个range,我们在sb中append一个符号"->",再append上一个数字,把sb输出到结果。否则count = 1,我们直接输出sb到结果。 最后运行完毕时假如count仍然>0,我们做相应步骤,把sb输出到结果。
Java:
public class Solution {public List<String> summaryRanges(int[] nums) {List<String> res = new ArrayList<>();if (nums == null || nums.length == 0) return res;StringBuilder sb = new StringBuilder();int count = 0;for (int i = 0; i < nums.length; i++) {if (count == 0) {sb.append(nums[i]);count++;} else if (nums[i] - nums[i - 1] == 1) {count++;} else {if (count > 1) sb.append("->").append(nums[i - 1]);res.add(sb.toString());sb.setLength(0);sb.append(nums[i]);count = 1;}}if (count > 1) sb.append("->").append(nums[nums.length - 1]);res.add(sb.toString());return res;} }
简化一下。分析的时候要考虑起始状态,过程分支以及结束时的边界条件。
public class Solution {public List<String> summaryRanges(int[] nums) {List<String> res = new ArrayList<>();if (nums == null || nums.length == 0) return res;StringBuilder sb = new StringBuilder(nums[0] + "");int count = 1;for (int i = 1; i < nums.length; i++) {if (nums[i] - nums[i - 1] == 1) {count++;} else {if (count > 1) sb.append("->").append(nums[i - 1]);res.add(sb.toString());sb.setLength(0);sb.append(nums[i]);count = 1;}}if (count > 1) sb.append("->").append(nums[nums.length - 1]);res.add(sb.toString());return res;} }
Update:
Different logic. This time we use a sliding window. If we found out nums[i] > nums[i - 1] + 1, we need to add result to res. here if i - 1 != lo, we need to add a range into res, otherwise we need to add a single number into res. We also need to do double check while we finished running the loop.
public class Solution {public List<String> summaryRanges(int[] nums) {List<String> res = new ArrayList<>();if (nums == null || nums.length == 0) return res;StringBuilder sb = new StringBuilder();int lo = 0, len = nums.length;for (int i = 1; i < len; i++) {if (nums[i] > nums[i - 1] + 1) {if (i - 1 != lo) sb.append(nums[lo]).append("->").append(nums[i - 1]);else sb.append(nums[lo]);res.add(sb.toString());lo = i;sb.setLength(0);}}if (lo == len - 1) sb.append(nums[lo]);else sb.append(nums[lo]).append("->").append(nums[len - 1]);res.add(sb.toString());return res;} }
Reference:
https://leetcode.com/discuss/42229/10-line-c-easy-understand
https://leetcode.com/discuss/42199/6-lines-in-python
https://leetcode.com/discuss/42342/idea-1-liner-group-by-number-index
https://leetcode.com/discuss/42290/accepted-java-solution-easy-to-understand
转载于:https://www.cnblogs.com/yrbbest/p/4996505.html
228. Summary Ranges相关推荐
- LeetCode 228. Summary Ranges
228. Summary Ranges Given a sorted integer array without duplicates, return the summary of its range ...
- LeetCode 228: Summary Ranges
Given a sorted integer array without duplicates, return the summary of its ranges. For example, give ...
- 228 Summary Ranges 汇总区间
给定一个无重复元素的有序整数数组,返回数组中区间范围的汇总. 示例 1: 输入: [0,1,2,4,5,7] 输出: ["0->2","4->5", ...
- LeetCode Summary Ranges(简单的数组处理)
Given a sorted integer array without duplicates, return the summary of its ranges. For example, g ...
- Summary Ranges
https://leetcode.com/problems/summary-ranges/ Given a sorted integer array without duplicates, retur ...
- leetcode解题笔记-Summary Ranges
题目要求: 给定一个排序好的int数组,按照间隔输出字符串 个人解法: 1.设一个临时变量,用来存->后面的值, 2. 遍历一次数组,注意要判断是否到最后一位. 代码: public stati ...
- 继续过中等难度.0309
. 8 String to Integer (atoi) 13.9% Medium . 151 Reverse Words in a String 15.7% Mediu ...
- number to string - LeetCode【数字 = 字符串】
228. Summary Ranges 让我们找出连续的序列,然后首尾两个数字之间用个"->"来连接,那么我只需遍历一遍数组即可,每次检查下一个数是不是递增的,如果是,则继续 ...
- Python JAVA Solutions for Leetcode
Python & JAVA Solutions for Leetcode (inspired by haoel's leetcode) Remember solutions are only ...
最新文章
- 写给java初学者,从零开始学习java开发的完整学习路线
- 利用velocity模板以及itext生成pdf
- Gym - 101471D Money for Nothing(决策单调性+分治+贪心)
- 宏定义 object-c 单例
- building a new horizon
- 光影之路 GPU架构发展史(4/4)
- mysql从一个表查询插入另一个表存在时更新_漫谈MySQL的锁机制
- 拓端tecdat|r语言多均线股票价格量化策略回测
- DH算法(密钥交换算法)
- java 提交mac地址栏_Mac系统快捷键大全 - 米扑博客
- 2020年8月程序员工资统计,平均14401元,下跌势头止住了
- 找不到网站的服务器 dns 地址,为什么网站一直显示找不到服务器DNS地址?
- Mac相关配置(本地host,端口被占用)
- 使用vba操作工作表,实现报表汇总
- python模拟生态系统
- python中lt是什么意思_python里的tplt什么意思 Python的format格式化输出
- 线程安全(thread safe)是什么?
- 详解诊断数据库ODX-F
- unity常用组件功能介绍
- 足坛诗人--贺炜的5大经典解说