首页 > 其他分享 >[剑指offer] 排序篇

[剑指offer] 排序篇

时间:2023-09-21 14:25:55浏览次数:27  
标签:idx offer int add static input 排序 public

JZ3 数组中重复的数字⭐

 1 /*
 2 * ① map/set
 3 * ② 因为"长度为n的数组里的所有数字都在0到n-1的范围内"
 4 *   所以对数组进行调整使得 numbers[idx]==idx
 5 * */
 6 public class JZ3_1
 7 {
 8     public static int duplicate(int[] numbers)
 9     {
10         for (int idx = 0; idx < numbers.length; idx++)
11         {
12             while (idx != numbers[idx])
13             {
14                 if (numbers[idx] == numbers[numbers[idx]])
15                     return numbers[idx];
16                 int temp = numbers[numbers[idx]];
17                 numbers[numbers[idx]] = numbers[idx];
18                 numbers[idx] = temp;
19             }
20         }
21         return -1;
22     }
23 }

JZ51 数组中的逆序对

  1 /* 归并 */
  2 public class JZ51_1
  3 {
  4     public static int res = 0;
  5     public static final int MOD = 1000000007;
  6 
  7     public static int InversePairs(int[] nums)
  8     {
  9         partition(nums, 0, nums.length - 1);
 10         return res % MOD;
 11     }
 12 
 13     public static void partition(int[] nums, int left, int right)
 14     {
 15         if (left < right)
 16         {
 17             int mid = (left + right) / 2;
 18             partition(nums, left, mid);
 19             partition(nums, mid + 1, right);
 20             merge(nums, left, mid, right);
 21         }
 22     }
 23 
 24     public static void merge(int[] nums, int left, int mid, int right)
 25     {
 26         int l = left, r = mid + 1, k = 0;
 27         int[] temp = new int[right - left + 1];
 28         while (l <= mid && r <= right)
 29         {
 30             if (nums[l] <= nums[r])
 31             {
 32                 temp[k++] = nums[l];
 33                 l++;
 34             }
 35             else
 36             {
 37                 temp[k++] = nums[r];
 38                 res += (mid - l + 1);
 39                 res %= MOD;
 40                 r++;
 41             }
 42         }
 43         while (l <= mid)
 44         {
 45             temp[k] = nums[l];
 46             k++;
 47             l++;
 48         }
 49         while (r <= right)
 50         {
 51             temp[k] = nums[r];
 52             k++;
 53             r++;
 54         }
 55         for (int i = 0; i < k; i++)
 56             nums[left + i] = temp[i];
 57     }
 58 }
 59 
 60 /* ⭐树状数组⭐ */
 61 public class JZ51_2
 62 {
 63     public static int res = 0;
 64     public static final int MOD = 1000000007;
 65     public static int[] tree;
 66 
 67     public static class KVCLass implements Comparable<KVCLass>
 68     {
 69         public int value;
 70         public int idx;
 71 
 72         KVCLass(int k, int v)
 73         {
 74             this.value = k;
 75             this.idx = v;
 76         }
 77 
 78         @Override
 79         public int compareTo(KVCLass o)
 80         {
 81             return value - o.value;
 82         }
 83     }
 84 
 85     public static int InversePairs(int[] nums)
 86     {
 87         KVCLass[] temp = new KVCLass[nums.length];
 88         tree = new int[nums.length + 1];
 89         for (int i = 0; i < nums.length; i++)
 90             temp[i] = new KVCLass(nums[i], i + 1);
 91         Arrays.sort(temp);
 92         for (int i = 0; i < temp.length; i++)
 93         {
 94             add(temp[i].idx);
 95             res = (res + i - sum(temp[i].idx - 1)) % MOD;
 96         }
 97         return res;
 98     }
 99 
100     private static int sum(int idx)
101     {
102         int ans = 0;
103         while (idx > 0)
104         {
105             ans = (ans + tree[idx]) % MOD;
106             idx -= (idx & (-idx));
107         }
108         return ans;
109     }
110 
111     private static void add(int idx)
112     {
113         while (idx < tree.length)
114         {
115             tree[idx] += 1;
116             idx += (idx & (-idx));
117         }
118     }
119 }

JZ40 最小的K个数

 1 /* ⭐快排⭐ */
 2 public class JZ40_1
 3 {
 4     public static ArrayList<Integer> GetLeastNumbers_Solution(int[] input, int k)
 5     {
 6         ArrayList<Integer> res = new ArrayList<>();
 7         int left = 0, right = input.length - 1;
 8         while (true)
 9         {
10             int idx = quickSort(input, left, right);
11             if (idx < k)
12                 left = idx + 1;
13             else if (idx > k)
14                 right = idx - 1;
15             else
16                 break;
17         }
18         for (int i = 0; i < k; i++)
19         {
20             res.add(input[i]);
21             System.out.println(input[i]);
22         }
23         return res;
24     }
25 
26     private static int quickSort(int[] input, int left, int right)
27     {
28         if (left >= right) return left;
29         int pivot = input[right], low = left, high = right;
30         while (low < high)
31         {
32             while (low < high && input[low] < pivot) low++;
33             input[high] = input[low];
34             while (low < high && input[high] >= pivot) high--;
35             input[low] = input[high];
36         }
37         input[low] = pivot;
38         return low;
39     }
40 }
41 
42 /* 堆 */
43 public class JZ40_2
44 {
45     public static ArrayList<Integer> GetLeastNumbers_Solution(int[] input, int k)
46     {
47         ArrayList<Integer> res = new ArrayList<>();
48         PriorityQueue<Integer> queue = new PriorityQueue<>(k+1, new Comparator<Integer>()
49         {
50             @Override
51             public int compare(Integer o1, Integer o2)
52             {
53                 return o2 - o1;
54             }
55         });
56         for (int i = 0; i < input.length && k != 0; i++)
57         {
58             if (i < k)
59                 queue.add(input[i]);
60             else
61             {
62                 if (input[i] < queue.peek())
63                 {
64                     queue.poll();
65                     queue.add(input[i]);
66                 }
67             }
68         }
69         while (!queue.isEmpty())
70             res.add(queue.poll());
71         return res;
72     }
73 }

JZ41 数据流中的中位数⭐

 1 /* 堆 */
 2 public class JZ41_1
 3 {
 4     public static PriorityQueue<Integer> minHeap = new PriorityQueue<>();
 5     public static PriorityQueue<Integer> maxHeap = new PriorityQueue<>(new Comparator<Integer>()
 6     {
 7         public int compare(Integer o1, Integer o2)
 8         {
 9             return o2 - o1;
10         }
11     });
12     public static int count = 0;
13 
14     public static void Insert(Integer num)
15     {
16         count++;
17         if (count % 2 != 0)
18         {
19             if (!minHeap.isEmpty() && num > minHeap.peek())
20             {
21                 maxHeap.add(minHeap.poll());
22                 minHeap.add(num);
23             }
24             else
25                 maxHeap.add(num);
26         }
27         else
28         {
29             if (!maxHeap.isEmpty() && num < maxHeap.peek())
30             {
31                 minHeap.add(maxHeap.poll());
32                 maxHeap.add(num);
33             }
34             else
35                 minHeap.add(num);
36         }
37     }
38 
39 //    public static void Insert(Integer num)
40 //    {
41 //        count++;
42 //        if (count % 2 != 0)
43 //        {
44 //            minHeap.add(num);
45 //            maxHeap.add(minHeap.poll());
46 //        }
47 //        else
48 //        {
49 //            maxHeap.add(num);
50 //            minHeap.add(maxHeap.poll());
51 //        }
52 //    }
53 
54     public static Double GetMedian()
55     {
56         if (minHeap.size() == maxHeap.size())
57             return (minHeap.peek() + maxHeap.peek()) / 2.0;
58         else
59             return maxHeap.peek() * 1.0;
60     }
61 }

标签:idx,offer,int,add,static,input,排序,public
From: https://www.cnblogs.com/VividBinGo/p/17719835.html

相关文章

  • 15-Vue核心-列表过滤和列表排序
    列表过滤 监视属性,实现列表过滤<!DOCTYPEhtml><htmllang="en"><head><metacharset="UTF-8"><title>基本列表</title><!--引入Vue--><scripttype="text/javascript......
  • 一键实现冒泡排序算法,代码质量有保障!
    近年来,深度学习和神经语言模型作为提高开发人员生产力的手段,尤其是2022年11月30日,ChatGPT这一现象级热点得出横空出世,在全球范围内形成了热烈的讨论,其中关于自动化代码生成和其它软件工程方面受到了极大的关注。 软件开发过程涵盖了各种代码生成任务,包括代码自动生成、代码翻......
  • 一键实现冒泡排序算法,代码质量有保障!
    近年来,深度学习和神经语言模型作为提高开发人员生产力的手段,尤其是2022年11月30日,ChatGPT这一现象级热点得出横空出世,在全球范围内形成了热烈的讨论,其中关于自动化代码生成和其它软件工程方面受到了极大的关注。软件开发过程涵盖了各种代码生成任务,包括代码自动生成、代码翻译和......
  • mybatis实现多字段动态排序
    背景在复杂项目中,可能会对数据表多个字段进行排序,不理解的话可结合需求看。需求现在有一张User表男同学先按age降序排序,后按height降序排序,最后按id升序排序女同学先按age升序排序,后按weight降序排序,最后按id升序排序不合理?现实可能就是这么的不合理。实现排序对(字段......
  • Sql Server分组排序及生成序列号
    1--1.分组排序2SELECT*,ROW_NUMBER()OVER(PARTITIONBY@fileName,@fileName1ORDERBYIDDESC)ASrowNumFROM@tableName34--2.生成序列号5SELECT*,ROW_NUMBER()over(orderbyidASC)ASrowNumFROM@tableName 翻译搜索复制......
  • [剑指offer] 位运算篇
    JZ65 不用加减乘除做加法⭐1/*^模拟不进位相加,&模拟进位(递归)*/2publicclassJZ65_13{4publicstaticintAdd(intnum1,intnum2)5{6if(num2==0)returnnum1;7returnAdd(num1^num2,(num1&num2)<<1);8}......
  • 软件测试|Python如何将列表从大到小排序
    简介在编程中,对列表进行排序是一个常见的操作,有时候我们需要将列表按照从大到小的顺序进行排列。Python提供了多种方法来实现这一目标。在本文中,我们将深入探讨几种将列表从大到小排序的方法,帮助您根据不同情况选择最合适的方式。使用sorted()函数Python的sorted()函数可以接收一......
  • 表格的自定义排序 编辑 拖拽 缩放
    终于能闲下来做点自己想做的事情了.. 简单表格排序  可以双击编辑自定义编辑后的规则 可拖动列进行列替换 可推动边框进行列宽度的缩放  ie6下中文不自动换行 非ie下字母和数字也不自动换行确实让人恼火 chrome浏览器下点击运行好像问题很大 拿到本地测试会比较好<!......
  • 冒泡排序
    importjava.util.Arrays;publicclassarrayDemo7{publicstaticvoidmain(String[]args){int[]arrays={5,2,3,1,4,6};sortArrays(arrays);System.out.println(Arrays.toString(arrays));}publicstaticint[]sortArrays(int[]arrays){for(intlength=ar......
  • 十大排序算法总结及其Java代码实现
    概述基于比较的排序算法,常见的有以下几种算法最好最坏平均空间稳定性思想注意事项冒泡排序O(n)O(n^2)O(n^2)O(1)是比较最好情况需要额外判断选择排序O(n^2)O(n^2)O(n^2)O(1)否比较顺序选择元素,交换次数较多,不适合大规模数据堆排序O(nlogn)O(nlogn)O(nlogn)O(1)否选择需要使用到数据......