日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

228. Summary Ranges

發布時間:2024/1/17 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。