[剑指offer] 排序篇

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 }
posted @ 2023-09-21 14:06  Vivid-BinGo  阅读(2)  评论(0编辑  收藏  举报