• 力扣164最大间距


    1.前言

            因为昨天写了一个基数排序,今天我来写一道用基数排序实现的题解,希望可以帮助你理解基数排序。

    这个题本身不难,就是线性时间和线性额外空间(O(n))的算法,有点难实现

     基数排序的时间复杂度是O(d*(n+radix)),其中d是最大值的位数,n是数组长度,radix是基数(10)然后化简就是 O(n) 

    2.思路分析

    1. 首先,找到待排序数组中的最大值,确定最大值的位数。假设最大值是max,位数是d。

    2. 创建10个桶(0-9),每个桶用来存放对应位数上的数字。

    3. 从低位(个位)开始,根据当前位数的值将待排序数组中的数字放入对应的桶中。

    4. 将桶中的数字按照顺序取出,覆盖原数组,然后进入下一位数的排序。

    5. 重复步骤3和步骤4,直到排完最高位(即 d 位)。

    6. 最后,遍历排序后的数组,计算相邻数字之间的差值,找到最大的差值,即为最大间距。

    3.代码实现 

    这里我获取最大值最小值使用了stream流,下面我来介绍一下

    int maxVal = Arrays.stream(arr).max().getAsInt();  获取最大值

    //Arrays.stream(arr) 将数组转换为一个流。
    //max() 方法找到流中的最大值,返回一个 OptionalInt 对象。
    //getAsInt() 方法从 OptionalInt 对象中获取最大值作为 int 类型的值。如果最大值不存在(即数组为空),则会抛出 NoSuchElementException 异常。

    1. /*
    2. * 基数排序实现 求相邻元素的差值(最大间距)
    3. *
    4. * */
    5. import java.util.ArrayList;
    6. import java.util.Arrays;
    7. import java.util.List;
    8. public class sortHomework3 {
    9. public static int maximumGap(int[] nums) {
    10. radixSort(nums);
    11. int r = 0;
    12. for (int i = 1; i < nums.length; i++) {
    13. r = Math.max(r,nums[i] - nums[i - 1]);
    14. }
    15. return r;
    16. }
    17. public static void radixSort(int[] arr) {
    18. if (arr == null || arr.length == 0){
    19. return;
    20. }
    21. int max = Arrays.stream(arr).max().getAsInt();//获得最大值,确定最高位数
    22. int min = Arrays.stream(arr).min().getAsInt();//获得最小值
    23. int digit = 1; // 从最低位开始排序
    24. int base = 10; // 基数为10,即十进制(是个桶)
    25. // 转换负数为正数
    26. if (min < 0) {
    27. max -= min;
    28. for (int i = 0; i < arr.length; i++) {
    29. arr[i] -= min;
    30. }
    31. }
    32. while(max / digit > 0){
    33. countingSort(arr, base, digit);
    34. digit *= base;//处理更高位数
    35. }
    36. //排序完毕后
    37. // 将转换后的正数转换回负数
    38. if (min < 0) {
    39. for (int i = 0; i < arr.length; i++) {
    40. arr[i] += min;
    41. }
    42. }
    43. }
    44. private static void countingSort(int[] arr, int base, int digit) {
    45. // 定义桶的大小 (里面的泛型<表示动态数组>)为10个桶
    46. List> buckets = new ArrayList<>(10);
    47. for (int i = 0; i < 10; i++) {
    48. buckets.add(new ArrayList<>()); // 创建空的桶(不创建空桶默认里面存的都是null不是桶)
    49. }
    50. for (int i : arr) {
    51. int index = i / digit % base;//获得位数
    52. buckets.get(index).add(i);//添加到集合中
    53. }
    54. int k = 0;
    55. //将元素在插入arr中
    56. for (int i = 0; i < buckets.size(); i++) {
    57. if (buckets.get(i).isEmpty()){
    58. continue;
    59. }
    60. //把各个桶中的元素存储到数组中
    61. for (int j = 0; j < buckets.get(i).size(); j++) {
    62. arr[k++] = buckets.get(i).get(j);
    63. }
    64. //取出来一个桶,咱就删除一个桶
    65. buckets.get(i).clear();
    66. }
    67. }
    68. public static void main(String[] args) {
    69. int[] arr = {5, 2, 8000, 3, 1};
    70. int[] expected = {1, 2, 3, 5, 8};
    71. System.out.println(Arrays.toString(arr));
    72. maximumGap(arr);
    73. System.out.println(Arrays.toString(arr));
    74. }
    75. }

  • 相关阅读:
    第二周学习报告
    redis的原理和源码-sentinel哨兵的原理和源码解析(下)
    ui设计要学插画吗?优漫动游
    Leetcode刷题:删除排序数组中的重复项
    【OpenCV-Python】教程:4-2 Harris角点检测
    并发刺客(False Sharing)——并发程序的隐藏杀手
    java.lang.ClassNotFoundException: freemarker.cache.TemplateLoader
    el-autocomplete 必填校验问题
    【附源码】Python计算机毕业设计千益校园帮跑腿信息平台
    java计算机毕业设计springboot+vue员工管理系统
  • 原文地址:https://blog.csdn.net/m0_74749208/article/details/133817813