Skip to content

Commit 67b327f

Browse files
committed
update
1 parent 99538cc commit 67b327f

File tree

5 files changed

+81
-4
lines changed

5 files changed

+81
-4
lines changed

Diff for: README.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
## practice in leetcode
22
https://leetcode.cn/studyplan/top-100-liked/
33

4+
5+
| 算法名称 | 算法关键字 | Path |
6+
|------ |---------------|------ |
7+
|快速排序| pivot , split , recursion |/com/leetcode/recursion/QuickSort.java|
8+
|寻找第K大的元素| pivot , split |com/leetcode/heap/KthLargest.java|
9+

Diff for: src/main/java/com/leetcode/heap/HeapSort.java

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.leetcode.heap;
2+
3+
public class HeapSort {
4+
5+
public void heapSort(int[] arr) {
6+
int n = arr.length;
7+
8+
// Build heap (rearrange array)
9+
for (int i = n / 2 - 1; i >= 0; i--)
10+
heapify(arr, n, i);
11+
12+
// One by one extract an element from heap
13+
for (int i = n - 1; i > 0; i--) {
14+
// Move current root to end
15+
int temp = arr[0];
16+
arr[0] = arr[i];
17+
arr[i] = temp;
18+
19+
// call max heapify on the reduced heap
20+
heapify(arr, i, 0);
21+
}
22+
}
23+
24+
void heapify(int arr[], int n, int i) {
25+
int largest = i; // Initialize largest as root
26+
int l = 2 * i + 1; // left = 2*i + 1
27+
int r = 2 * i + 2; // right = 2*i + 2
28+
29+
// If left child is larger than root
30+
if (l < n && arr[l] > arr[largest])
31+
largest = l;
32+
33+
// If right child is larger than largest so far
34+
if (r < n && arr[r] > arr[largest])
35+
largest = r;
36+
37+
// If largest is not root
38+
if (largest != i) {
39+
int swap = arr[i];
40+
arr[i] = arr[largest];
41+
arr[largest] = swap;
42+
43+
// Recursively heapify the affected sub-tree
44+
heapify(arr, n, largest);
45+
}
46+
}
47+
48+
public static void main(String[] args) {
49+
int[] arr = {12, 11, 13, 5, 6, 7};
50+
HeapSort heapSort = new HeapSort();
51+
heapSort.heapSort(arr);
52+
53+
System.out.println("Sorted array:");
54+
for (int i : arr) {
55+
System.out.print(i + " ");
56+
}
57+
}
58+
}

Diff for: src/main/java/com/leetcode/heap/KthLargest.java

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
/**
77
* Kth Largest Element in an Array
8+
*
89
*/
910
public class KthLargest {
1011

Diff for: src/main/java/com/leetcode/slidingwindow/MinWindow.java

+14-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,18 @@
66
public class MinWindow {
77

88

9+
/**
10+
* 算法介绍:
11+
* 设置一个targetmap来存放 目标字符串t的 字符和个数。
12+
* 设置左右两个指针,开始都指向s字符数组的左端。
13+
* 首先,right指针从左向右移动,直到发现了所有的目标字符,记录一个minlen, minstart。
14+
* 关键步骤-A) 此时,right停止,而left开始从左向右移动,直到读到第一个目标字符中的任意一个字符, 比如字符a,停止下来。
15+
* 关键步骤-B) 此时,right开始继续走,1)直到读到left读到的那个字符,比如字符a, 停止下来;
16+
* 2)如果没有读到该字符,right指针到达了字符串长度,从而停止循环,返回结果;
17+
* 若1)继续执行关键步骤A->关键步骤B,直到退出循环。
18+
*
19+
*/
20+
921
public String minWindow(String s, String t) {
1022
Map<Character, Integer> targetMap = new HashMap<>();
1123
for (char c : t.toCharArray()) {
@@ -52,8 +64,8 @@ public String minWindow(String s, String t) {
5264

5365
public static void main(String[] args) {
5466

55-
String str = "ADOBECODEBANC";
56-
String substr = "ABC";
67+
String str = "axxxxxbcxbxxxxbc";
68+
String substr = "abc";
5769
String s = new MinWindow().minWindow(str, substr);
5870
System.out.println(s);
5971

Diff for: src/main/java/com/leetcode/stack/StackOne.java renamed to src/main/java/com/leetcode/stack/CheckPerentheses.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
* using stack data structure
1010
*/
11-
public class StackOne {
11+
public class CheckPerentheses {
1212

1313

1414
public boolean isValid(String s) {
@@ -31,7 +31,7 @@ public boolean isValid(String s) {
3131
}
3232
public static void main(String[] args) {
3333
String s = "{(abc+bcd)*[cc+dd]}";
34-
boolean isvalid = new StackOne().isValid(s);
34+
boolean isvalid = new CheckPerentheses().isValid(s);
3535
System.out.println(isvalid);
3636
}
3737
}

0 commit comments

Comments
 (0)