题目链接
法一(随机选择算法 - 找第k个最大的数)
private void swap(int[] nums, int i, int j) {
if (i != j) {
nums[i] ^= nums[j];
nums[j] ^= nums[i];
nums[i] ^= nums[j];
}
}
private int randPartition(int[] nums, int left, int right) {
int random = new Random().nextInt(right - left + 1) + left;
swap(nums, left, random);
int pivot = nums[left];
while (left < right) {
while (left < right && nums[right] > pivot) {
right--;
}
nums[left] = nums[right];
while (left < right && nums[left] <= pivot) {
left++;
}
nums[right] = nums[left];
}
nums[left] = pivot;
return left;
}
private int randSelect(int[] nums, int left, int right, int k) {
if (left >= right) {
return nums[left];
}
int pivotIndex = randPartition(nums, left, right);
int m = right - pivotIndex + 1;
if (k > m) {
return randSelect(nums, left, pivotIndex - 1, k - m);
} else if (k < m) {
return randSelect(nums, pivotIndex + 1, right, k);
} else {
return nums[pivotIndex];
}
}
public int findKthLargest(int[] nums, int k) {
return randSelect(nums, 0, nums.length - 1, k);
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
法二(随机选择算法 - 找第k大的数)
private void swap(int[] nums, int i, int j) {
if (i != j) {
nums[i] ^= nums[j];
nums[j] ^= nums[i];
nums[i] ^= nums[j];
}
}
private int randPartition(int[] nums, int left, int right) {
int random = new Random().nextInt(right - left + 1) + left;
swap(nums, left, random);
int pivot = nums[left];
while (left < right) {
while (left < right && nums[right] > pivot) {
right--;
}
nums[left] = nums[right];
while (left < right && nums[left] <= pivot) {
left++;
}
nums[right] = nums[left];
}
nums[left] = pivot;
return left;
}
private void randSelect_2(int[] nums, int left, int right, int k) {
if (left >= right) {
return;
}
int pivotIndex = randPartition(nums, left, right);
if (k < pivotIndex) {
randSelect_2(nums, left, pivotIndex - 1, k);
} else if (k > pivotIndex) {
randSelect_2(nums, pivotIndex + 1, right, k);
} else {
return;
}
}
public int findKthLargest_2(int[] nums, int k) {
randSelect_2(nums, 0, nums.length - 1, nums.length - k);
return nums[nums.length - k];
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
法三(优先级队列)
public int findKthLargest_3(int[] nums, int k) {
PriorityQueue<Integer> minHeap = new PriorityQueue<>();
for (int i = 0; i < k; i++) {
minHeap.offer(nums[i]);
}
for (int i = k; i < nums.length; i++) {
if (nums[i] > minHeap.peek()) {
minHeap.poll();
minHeap.offer(nums[i]);
}
}
return minHeap.peek();
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
法四(计数排序)
public int findKthLargest_4(int[] nums, int k) {
int max = nums[0], min = nums[0];
for (int num : nums) {
if (num > max) {
max = num;
}
if (num < min) {
min = num;
}
}
int[] count = new int[max - min + 1];
for (int num : nums) {
count[num - min]++;
}
for (int i = max - min; i >= 0; i--) {
k -= count[i];
if (k <= 0) {
return min + i;
}
}
return nums[0];
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
法五_1(冒泡排序 - 递增)
private void swap(int[] nums, int i, int j) {
if (i != j) {
nums[i] ^= nums[j];
nums[j] ^= nums[i];
nums[i] ^= nums[j];
}
}
private void bubbleSort_1(int[] nums) {
int len = nums.length;
boolean flag;
for (int i = 0; i < len - 1; i++) {
flag = true;
for (int j = 0; j < len - i - 1; j++) {
if (nums[j] > nums[j + 1]) {
swap(nums, j, j + 1);
flag = false;
}
}
if (flag) {
break;
}
}
}
public int findKthLargest_5_1(int[] nums, int k) {
bubbleSort_1(nums);
return nums[nums.length - k];
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
法五_2(冒泡排序 - 递减)
private void swap(int[] nums, int i, int j) {
if (i != j) {
nums[i] ^= nums[j];
nums[j] ^= nums[i];
nums[i] ^= nums[j];
}
}
private void bubbleSort_2(int[] nums) {
int len = nums.length;
boolean flag;
for (int i = 0; i < len - 1; i++) {
flag = true;
for (int j = 0; j < len - i - 1; j++) {
if (nums[j] < nums[j + 1]) {
swap(nums, j, j + 1);
flag = false;
}
}
if (flag) {
break;
}
}
}
public int findKthLargest_5_2(int[] nums, int k) {
bubbleSort_2(nums);
return nums[k - 1];
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
法六_1(选择排序 - 递增)
private void swap(int[] nums, int i, int j) {
if (i != j) {
nums[i] ^= nums[j];
nums[j] ^= nums[i];
nums[i] ^= nums[j];
}
}
private void selectSort_1(int[] nums) {
int len = nums.length;
for (int i = 0; i < len - 1; i++) {
int min = i;
for (int j = i + 1; j < len; j++) {
if (nums[j] < nums[min]) {
min = j;
}
}
if (i != min) {
swap(nums, i, min);
}
}
}
public int findKthLargest_6_1(int[] nums, int k) {
selectSort_1(nums);
return nums[nums.length - k];
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
法六_2(选择排序 - 递减)
private void swap(int[] nums, int i, int j) {
if (i != j) {
nums[i] ^= nums[j];
nums[j] ^= nums[i];
nums[i] ^= nums[j];
}
}
private void selectSort_2(int[] nums) {
int len = nums.length;
for (int i = 0; i < len - 1; i++) {
int max = i;
for (int j = i + 1; j < len; j++) {
if (nums[j] > nums[max]) {
max = j;
}
}
if (i != max) {
swap(nums, i, max);
}
}
}
public int findKthLargest_6_2(int[] nums, int k) {
selectSort_2(nums);
return nums[k - 1];
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
法七_1(插入排序 - 递增)
private void insertSort_1(int[] nums) {
int len = nums.length;
for (int i = 1; i < len; i++) {
int insert = nums[i];
for (int j = i - 1; j >= 0; j--) {
if (insert < nums[j]) {
nums[j + 1] = nums[j];
nums[j] = insert;
} else {
break;
}
}
}
}
public int findKthLargest_7_1(int[] nums, int k) {
insertSort_1(nums);
return nums[nums.length - k];
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
法七_2(插入排序 - 递减)
private void insertSort_2(int[] nums) {
int len = nums.length;
for (int i = 1; i < len; i++) {
int insert = nums[i];
for (int j = i - 1; j >= 0; j--) {
if (insert > nums[j]) {
nums[j + 1] = nums[j];
nums[j] = insert;
} else {
break;
}
}
}
}
public int findKthLargest_7_2(int[] nums, int k) {
insertSort_2(nums);
return nums[k - 1];
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
法八_1(快速排序 - 递增)
private void swap(int[] nums, int i, int j) {
if (i != j) {
nums[i] ^= nums[j];
nums[j] ^= nums[i];
nums[i] ^= nums[j];
}
}
private void quickSort_1(int[] nums, int left, int right) {
if (left < right) {
int pivot = left;
int index = left + 1;
for (int i = index; i <= right; i++) {
if (nums[i] < nums[pivot]) {
swap(nums, index, i);
index++;
}
}
swap(nums, index - 1, pivot);
pivot = index - 1;
quickSort_1(nums, left, pivot - 1);
quickSort_1(nums, pivot + 1, right);
}
}
public int findKthLargest_8_1(int[] nums, int k) {
quickSort_1(nums, 0, nums.length - 1);
return nums[nums.length - k];
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
法八_2(快速排序 - 递减)
private void swap(int[] nums, int i, int j) {
if (i != j) {
nums[i] ^= nums[j];
nums[j] ^= nums[i];
nums[i] ^= nums[j];
}
}
private void quickSort_2(int[] nums, int left, int right) {
if (left < right) {
int pivot = left;
int index = left + 1;
for (int i = index; i <= right; i++) {
if (nums[i] > nums[pivot]) {
swap(nums, index, i);
index++;
}
}
swap(nums, index - 1, pivot);
pivot = index - 1;
quickSort_2(nums, left, pivot - 1);
quickSort_2(nums, pivot + 1, right);
}
}
public int findKthLargest_8_2(int[] nums, int k) {
quickSort_2(nums, 0, nums.length - 1);
return nums[k - 1];
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
法九_1(堆排序 - 最大堆)
private void swap(int[] nums, int i, int j) {
if (i != j) {
nums[i] ^= nums[j];
nums[j] ^= nums[i];
nums[i] ^= nums[j];
}
}
private void downAdjust_1(int[] nums, int low, int high) {
int dad = low, son = dad * 2 + 1;
while (son <= high) {
if (son + 1 <= high && nums[son + 1] > nums[son]) {
son++;
}
if (nums[son] > nums[dad]) {
swap(nums, son, dad);
dad = son;
son = dad * 2 + 1;
} else {
break;
}
}
}
private void createMaxHeap(int[] nums, int len) {
for (int i = len / 2 - 1; i >= 0; i--) {
downAdjust_1(nums, i, len - 1);
}
}
public int findKthLargest_9_1(int[] nums, int k) {
int len = nums.length;
createMaxHeap(nums, len);
for (int i = len - 1; i > 0; i--) {
swap(nums, i, 0);
downAdjust_1(nums, 0, i - 1);
}
return nums[nums.length - k];
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
法九_2(堆排序 - 最小堆)
private void swap(int[] nums, int i, int j) {
if (i != j) {
nums[i] ^= nums[j];
nums[j] ^= nums[i];
nums[i] ^= nums[j];
}
}
private void downAdjust_2(int[] nums, int low, int high) {
int dad = low, son = dad * 2 + 1;
while (son <= high) {
if (son + 1 <= high && nums[son + 1] < nums[son]) {
son++;
}
if (nums[son] < nums[dad]) {
swap(nums, son, dad);
dad = son;
son = dad * 2 + 1;
} else {
break;
}
}
}
private void createMinHeap(int[] nums, int len) {
for (int i = len / 2 - 1; i >= 0; i--) {
downAdjust_2(nums, i, len - 1);
}
}
public int findKthLargest_9_2(int[] nums, int k) {
int len = nums.length;
createMinHeap(nums, len);
for (int i = len - 1; i > 0; i--) {
swap(nums, i, 0);
downAdjust_2(nums, 0, i - 1);
}
return nums[k - 1];
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
法十_1(归并排序 - 递增)
private void merge_1(int[] nums, int left, int mid, int right) {
int i = left, j = mid + 1, index = 0;
int[] temp = new int[right - left + 1];
while (i <= mid && j <= right) {
temp[index++] = nums[i] < nums[j] ? nums[i++] : nums[j++];
}
while (i <= mid) {
temp[index++] = nums[i++];
}
while (j <= right) {
temp[index++] = nums[j++];
}
for (int num : temp) {
nums[left++] = num;
}
}
private void mergeSort_1(int[] nums, int left, int right) {
if (left < right) {
int mid = left + (right - left) / 2;
mergeSort_1(nums, left, mid);
mergeSort_1(nums, mid + 1, right);
merge_1(nums, left, mid, right);
}
}
public int findKthLargest_10_1(int[] nums, int k) {
mergeSort_1(nums, 0, nums.length - 1);
return nums[nums.length - k];
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
法十_2(归并排序 - 递减)
private void merge_2(int[] nums, int left, int mid, int right) {
int i = left, j = mid + 1, index = 0;
int[] temp = new int[right - left + 1];
while (i <= mid && j <= right) {
temp[index++] = nums[i] > nums[j] ? nums[i++] : nums[j++];
}
while (i <= mid) {
temp[index++] = nums[i++];
}
while (j <= right) {
temp[index++] = nums[j++];
}
for (int num : temp) {
nums[left++] = num;
}
}
private void mergeSort_2(int[] nums, int left, int right) {
if (left < right) {
int mid = left + (right - left) / 2;
mergeSort_2(nums, left, mid);
mergeSort_2(nums, mid + 1, right);
merge_2(nums, left, mid, right);
}
}
public int findKthLargest_10_2(int[] nums, int k) {
mergeSort_2(nums, 0, nums.length - 1);
return nums[k - 1];
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
法十一_1(希尔排序 - 递增)
private void swap(int[] nums, int i, int j) {
if (i != j) {
nums[i] ^= nums[j];
nums[j] ^= nums[i];
nums[i] ^= nums[j];
}
}
private void shellSort_1(int[] nums) {
int len = nums.length;
for (int gap = len / 2; gap > 0; gap /= 2) {
for (int i = gap; i < len; i++) {
for (int j = i - gap; j >= 0; j -= gap) {
if (nums[j + gap] < nums[j]) {
swap(nums, j, j + gap);
}
}
}
}
}
public int findKthLargest_11_1(int[] nums, int k) {
shellSort_1(nums);
return nums[nums.length - k];
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
法十一_2(希尔排序 - 递减)
private void swap(int[] nums, int i, int j) {
if (i != j) {
nums[i] ^= nums[j];
nums[j] ^= nums[i];
nums[i] ^= nums[j];
}
}
private void shellSort_2(int[] nums) {
int len = nums.length;
for (int gap = len / 2; gap > 0; gap /= 2) {
for (int i = gap; i < len; i++) {
for (int j = i - gap; j >= 0; j -= gap) {
if (nums[j + gap] > nums[j]) {
swap(nums, j, j + gap);
}
}
}
}
}
public int findKthLargest_11_2(int[] nums, int k) {
shellSort_2(nums);
return nums[k - 1];
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
法十二_1(计数排序 - 递增)
private void countSort_1(int[] nums) {
int max = nums[0], min = nums[0];
for (int num : nums) {
if (num > max) {
max = num;
}
if (num < min) {
min = num;
}
}
int[] count = new int[max - min + 1];
for (int num : nums) {
count[num - min]++;
}
int k = 0;
for (int i = 0; i < count.length; i++) {
for (int j = 0; j < count[i]; j++) {
nums[k++] = min + i;
}
}
}
public int findKthLargest_12_1(int[] nums, int k) {
countSort_1(nums);
return nums[nums.length - k];
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
法十二_2(计数排序 - 递减)
private void countSort_2(int[] nums) {
int max = nums[0], min = nums[0];
for (int num : nums) {
if (num > max) {
max = num;
}
if (num < min) {
min = num;
}
}
int[] count = new int[max - min + 1];
for (int num : nums) {
count[num - min]++;
}
int k = 0;
for (int i = count.length - 1; i >= 0; i--) {
for (int j = 0; j < count[i]; j++) {
nums[k++] = min + i;
}
}
}
public int findKthLargest_12_2(int[] nums, int k) {
countSort_2(nums);
return nums[k - 1];
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
法十三_1(桶排序 - 递增)
private void bucketSort_1(int[] nums) {
int len = nums.length;
int max = nums[0], min = nums[0];
for (int num : nums) {
if (num > max) {
max = num;
}
if (num < min) {
min = num;
}
}
int gap = (max - min) / len + 1;
int bucketNum = (max - min) / gap + 1;
List<List<Integer>> bucketList = new ArrayList<>();
for (int i = 0; i < bucketNum; i++) {
bucketList.add(new LinkedList<>());
}
for (int i = 0; i < nums.length; i++) {
bucketList.get((nums[i] - min) / gap).add(nums[i]);
}
for (int i = 0; i < bucketList.size(); i++) {
Collections.sort(bucketList.get(i));
}
int k = 0;
for (int i = 0; i < bucketNum; i++) {
for (Integer num : bucketList.get(i)) {
nums[k++] = num;
}
}
}
public int findKthLargest_13_1(int[] nums, int k) {
bucketSort_1(nums);
return nums[nums.length - k];
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
法十三_2(桶排序 - 递减)
private void bucketSort_2(int[] nums) {
int len = nums.length;
int max = nums[0], min = nums[0];
for (int num : nums) {
if (num > max) {
max = num;
}
if (num < min) {
min = num;
}
}
int gap = (max - min) / len + 1;
int bucketNum = (max - min) / gap + 1;
List<List<Integer>> bucketList = new ArrayList<>();
for (int i = 0; i < bucketNum; i++) {
bucketList.add(new LinkedList<>());
}
for (int i = 0; i < nums.length; i++) {
bucketList.get((nums[i] - min) / gap).add(nums[i]);
}
for (int i = 0; i < bucketList.size(); i++) {
Collections.sort(bucketList.get(i), Collections.reverseOrder());
}
int k = 0;
for (int i = bucketNum - 1; i >= 0; i--) {
for (Integer num : bucketList.get(i)) {
nums[k++] = num;
}
}
}
public int findKthLargest_13_2(int[] nums, int k) {
bucketSort_2(nums);
return nums[k - 1];
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
法十四_1(基数排序 - 递增)
private void radixSort_1(int[] nums) {
int len = nums.length;
int max = nums[0];
for (int num : nums) {
if (num > max) {
max = num;
}
}
int digit = 0;
while (max != 0) {
max /= 10;
digit++;
}
int bucketNum = 10;
List<List<Integer>> bucketList = new ArrayList<>();
for (int i = 0; i < bucketNum; i++) {
bucketList.add(new LinkedList<>());
}
for (int i = 0; i < digit; i++) {
for (int j = 0; j < len; j++) {
int radix = (int) (nums[j] / Math.pow(10, i)) % 10;
bucketList.get(radix).add(nums[j]);
}
int k = 0;
for (List<Integer> bucket : bucketList) {
for (Integer num : bucket) {
nums[k++] = num;
}
bucket.clear();
}
}
}
public int findKthLargest_14_1(int[] nums, int k) {
radixSort_1(nums);
return nums[nums.length - k];
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
法十四_2(基数排序 - 递减)
private void radixSort_2(int[] nums) {
int len = nums.length;
int max = nums[0];
for (int num : nums) {
if (num > max) {
max = num;
}
}
int digit = 0;
while (max != 0) {
max /= 10;
digit++;
}
int bucketNum = 10;
List<List<Integer>> bucketList = new ArrayList<>();
for (int i = 0; i < bucketNum; i++) {
bucketList.add(new LinkedList<>());
}
for (int i = 0; i < digit; i++) {
for (int j = 0; j < len; j++) {
int radix = (int) (nums[j] / Math.pow(10, i)) % 10;
bucketList.get(radix).add(nums[j]);
}
int k = 0;
for (int j = bucketNum - 1; j >= 0; j--) {
List<Integer> bucket = bucketList.get(j);
for (Integer num : bucket) {
nums[k++] = num;
}
bucket.clear();
}
}
}
public int findKthLargest_14_2(int[] nums, int k) {
radixSort_2(nums);
return nums[k - 1];
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
本地测试
lay.showTitle(215);
Solution215 sol215 = new Solution215();
int k215 = 2;
int[] nums215_1 = new int[]{3, 2, 1, 5, 6, 4};
System.out.println(k215 + " " + Arrays.toString(nums215_1));
System.out.println(sol215.findKthLargest(nums215_1, k215) + " " + Arrays.toString(nums215_1));
int[] nums215_2 = new int[]{3, 2, 1, 5, 6, 4};
System.out.println(sol215.findKthLargest_2(nums215_2, k215) + " " + Arrays.toString(nums215_2));
int[] nums215_3 = new int[]{3, 2, 1, 5, 6, 4};
System.out.println(sol215.findKthLargest_3(nums215_3, k215) + " " + Arrays.toString(nums215_3));
int[] nums215_4 = new int[]{3, 2, 1, 5, 6, 4};
System.out.println(sol215.findKthLargest_4(nums215_4, k215) + " " + Arrays.toString(nums215_4));
int[] nums215_5_1 = new int[]{3, 2, 1, 5, 6, 4};
System.out.println(sol215.findKthLargest_5_1(nums215_5_1, k215) + " " + Arrays.toString(nums215_5_1));
int[] nums215_5_2 = new int[]{3, 2, 1, 5, 6, 4};
System.out.println(sol215.findKthLargest_5_2(nums215_5_2, k215) + " " + Arrays.toString(nums215_5_2));
int[] nums215_6_1 = new int[]{3, 2, 1, 5, 6, 4};
System.out.println(sol215.findKthLargest_6_1(nums215_6_1, k215) + " " + Arrays.toString(nums215_6_1));
int[] nums215_6_2 = new int[]{3, 2, 1, 5, 6, 4};
System.out.println(sol215.findKthLargest_6_2(nums215_6_2, k215) + " " + Arrays.toString(nums215_6_2));
int[] nums215_7_1 = new int[]{3, 2, 1, 5, 6, 4};
System.out.println(sol215.findKthLargest_7_1(nums215_7_1, k215) + " " + Arrays.toString(nums215_7_1));
int[] nums215_7_2 = new int[]{3, 2, 1, 5, 6, 4};
System.out.println(sol215.findKthLargest_7_2(nums215_7_2, k215) + " " + Arrays.toString(nums215_7_2));
int[] nums215_8_1 = new int[]{3, 2, 1, 5, 6, 4};
System.out.println(sol215.findKthLargest_8_1(nums215_8_1, k215) + " " + Arrays.toString(nums215_8_1));
int[] nums215_8_2 = new int[]{3, 2, 1, 5, 6, 4};
System.out.println(sol215.findKthLargest_8_2(nums215_8_2, k215) + " " + Arrays.toString(nums215_8_2));
int[] nums215_9_1 = new int[]{3, 2, 1, 5, 6, 4};
System.out.println(sol215.findKthLargest_9_1(nums215_9_1, k215) + " " + Arrays.toString(nums215_9_1));
int[] nums215_9_2 = new int[]{3, 2, 1, 5, 6, 4};
System.out.println(sol215.findKthLargest_9_2(nums215_9_2, k215) + " " + Arrays.toString(nums215_9_2));
int[] nums215_10_1 = new int[]{3, 2, 1, 5, 6, 4};
System.out.println(sol215.findKthLargest_10_1(nums215_10_1, k215) + " " + Arrays.toString(nums215_10_1));
int[] nums215_10_2 = new int[]{3, 2, 1, 5, 6, 4};
System.out.println(sol215.findKthLargest_10_2(nums215_10_2, k215) + " " + Arrays.toString(nums215_10_2));
int[] nums215_11_1 = new int[]{3, 2, 1, 5, 6, 4};
System.out.println(sol215.findKthLargest_11_1(nums215_11_1, k215) + " " + Arrays.toString(nums215_11_1));
int[] nums215_11_2 = new int[]{3, 2, 1, 5, 6, 4};
System.out.println(sol215.findKthLargest_11_2(nums215_11_2, k215) + " " + Arrays.toString(nums215_11_2));
int[] nums215_12_1 = new int[]{3, 2, 1, 5, 6, 4};
System.out.println(sol215.findKthLargest_12_1(nums215_12_1, k215) + " " + Arrays.toString(nums215_12_1));
int[] nums215_12_2 = new int[]{3, 2, 1, 5, 6, 4};
System.out.println(sol215.findKthLargest_12_2(nums215_12_2, k215) + " " + Arrays.toString(nums215_12_2));
int[] nums215_13_1 = new int[]{3, 2, 1, 5, 6, 4};
System.out.println(sol215.findKthLargest_13_1(nums215_13_1, k215) + " " + Arrays.toString(nums215_13_1));
int[] nums215_13_2 = new int[]{3, 2, 1, 5, 6, 4};
System.out.println(sol215.findKthLargest_13_2(nums215_13_2, k215) + " " + Arrays.toString(nums215_13_2));
int[] nums215_14_1 = new int[]{3, 2, 1, 5, 6, 4};
System.out.println(sol215.findKthLargest_14_1(nums215_14_1, k215) + " " + Arrays.toString(nums215_14_1));
int[] nums215_14_2 = new int[]{3, 2, 1, 5, 6, 4};
System.out.println(sol215.findKthLargest_14_2(nums215_14_2, k215) + " " + Arrays.toString(nums215_14_2));
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55