funcmergeSort(head *ListNode) *ListNode { if head == nil || head.Next == nil { return head } mid := divideList(head) return mergeTwoListNode(mergeSort(head), mergeSort(mid)) }
funcmergeTwoListNode(left, right *ListNode) *ListNode { res := new(ListNode) mov := res
for left != nil && right != nil { if left.Val > right.Val { mov.Next = right right = right.Next } else { mov.Next = left left = left.Next } mov = mov.Next }
if left != nil { mov.Next = left } if right != nil { mov.Next = right } return res.Next }
// divideList 会把 list 分为两个 list // 会截断原来的 node funcdivideList(node *ListNode) *ListNode { if node == nil || node.Next == nil { return node } fast := node.Next
for fast != nil && fast.Next != nil { fast = fast.Next.Next node = node.Next } mid := node.Next node.Next = nil return mid }
public List<Integer> countSmaller(int[] nums){ int n = nums.length; int[] res = newint[n], indexes = newint[n]; // 保存原始数组的数组 好在 res 数组中定位 for (int i = 0; i < n; i++) { indexes[i] = i; } mergeSort(nums, 0, n - 1, res, indexes); return Arrays.stream(res).boxed().collect(Collectors.toList()); }
publicvoidmergeSort(int[] nums, int i, int j, int[] res, int[] indexes){ if (i >= j) return;
int mid = i + (j - i) / 2; mergeSort(nums, i, mid, res, indexes); mergeSort(nums, mid + 1, j, res, indexes); merge(nums, i, mid, j, res, indexes); }
publicvoidmerge(int[]nums, int start, int mid, int end, int[] res, int[] indexes){ int i = start, j = mid + 1; int[] tmp = newint[end - start + 1], tmpIndexes = newint[end - start + 1];
int index = 0; while (i <= mid && j <= end) { if (nums[i] <= nums[j]) { // 说明 j 之前相当于 i 来说都是逆序了 tmp[index] = nums[i]; res[indexes[i]] += j - mid - 1; tmpIndexes[index++] = indexes[i++]; } else { // nums[j] < nums[i] tmp[index] = nums[j]; tmpIndexes[index++] = indexes[j++]; } }
while (i <= mid) { // 说明 j 之前相当于 i 来说都是逆序了 tmp[index] = nums[i]; res[indexes[i]] += j - mid - 1; tmpIndexes[index++] = indexes[i++]; }
// Heap 根据输入的 compare 构建不同的堆 // 默认小顶堆 -> 从大到小排列 type Heap struct { nums []int compare func(a, b int)bool }
// shiftDown 移动顶层的向下 func(h *Heap)shiftDown(k, n int) { for2*k+1 <= n { j := 2*k + 1 // 注意这个地方 大顶堆的时候 因为需要把小的东西往下沉 所以需要选择的是 子节点中 的较大值 // 小顶堆的时候 由于需要把大的东西往下沉 所以需要选取的是较小值 (因为比较小值小 这个节点一定比两个节点都小) if j+1 <= n && h.compare(h.nums[j],h.nums[j+1]) { j++ } if h.compare(h.nums[j], h.nums[k]) { break } swap(h.nums, k, j) k = j } }
// popUp 最下面的浮动到最上面 func(h *Heap)popUp(k int) { for k >= 0 { var father int if k%2 == 1 { father = k / 2 } else { father = k/2 - 1 } if h.compare(h.nums[k], h.nums[father]) { break } swap(h.nums, father, k) k = father } }
biggest := nums[0] for _, num := range nums { biggest = max(biggest, num) }
// 基数排序的桶 分为 0 - 9 lists := make([][]int, 10)
base := 1 for biggest > 0 { // 每次循环前都重置 for i := 0; i < 10; i++ { lists[i] = make([]int, 0) } // 排序数组 for _, num := range nums { i := num / base % 10 lists[i] = append(lists[i], num) }
// 根据每轮的顺序 重新赋值 nums 数组 for i, index := 0, 0; i < 10; i++ { for j := 0; j < len(lists[i]); j++ { nums[index] = lists[i][j] index++ } }
biggest /= 10 base *= 10 }
res := 0
for i := 0; i < len(nums)-1; i++ { res = max(res, nums[i+1]-nums[i]) } return res }
for _, num := range nums { counts[num-smallest]++ } res := 0 var pre *int
for i, num := range counts { // 表示没有数字 if num == 0 { continue } if pre == nil { tmp := i pre = &tmp continue } // 比较 res = max(res, i - *pre) tmp := i pre = &tmp } return res }