日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

leetcode 164. Maximum Gap | 164. 最大间距(桶排序)

發布時間:2024/2/28 51 豆豆
生活随笔 收集整理的這篇文章主要介紹了 leetcode 164. Maximum Gap | 164. 最大间距(桶排序) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目

https://leetcode.com/problems/maximum-gap/

題解

桶排序,用數組模擬桶

class Solution {public int maximumGap(int[] nums) {int N = nums.length;int digits = 0; // 最大位數for (int n : nums) {digits = Math.max(digits, String.valueOf(n).length());}String[] arr = new String[N];for (int i = 0; i < N; i++) {arr[i] = String.format("%0" + digits + "d", nums[i]);}String[] bucket = arr;for (int i = digits - 1; i >= 0; i--) {String[] newBucket = new String[arr.length];int[] count = new int[10];int[] pos = new int[10];for (int j = 0; j < N; j++) {count[bucket[j].charAt(i) - '0']++;}for (int j = 1; j < 10; j++) {pos[j] = pos[j - 1] + count[j - 1];}// fill bucketfor (int j = 0; j < N; j++) {newBucket[pos[bucket[j].charAt(i) - '0']++] = bucket[j];}bucket = newBucket;}int res = 0;for (int i = 1; i < N; i++) {res = Math.max(res, Integer.parseInt(bucket[i]) - Integer.parseInt(bucket[i - 1]));}return res;} }

超強干貨來襲 云風專訪:近40年碼齡,通宵達旦的技術人生

總結

以上是生活随笔為你收集整理的leetcode 164. Maximum Gap | 164. 最大间距(桶排序)的全部內容,希望文章能夠幫你解決所遇到的問題。

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