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;} }?
二刷:
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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 电子排布
- 下一篇: Effective Java阅读笔记——