Skip to content

Commit 99538cc

Browse files
committed
update
1 parent 6efc0fe commit 99538cc

File tree

6 files changed

+107
-17
lines changed

6 files changed

+107
-17
lines changed

pom.xml

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
<version>1.0-SNAPSHOT</version>
1010

1111
<properties>
12-
<maven.compiler.source>22</maven.compiler.source>
13-
<maven.compiler.target>22</maven.compiler.target>
12+
<maven.compiler.source>21</maven.compiler.source>
13+
<maven.compiler.target>21</maven.compiler.target>
1414
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1515
</properties>
1616
<dependencies>

src/main/java/com/leetcode/binarytree/BSTValidator.java

+9-12
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,20 @@
88
public class BSTValidator {
99

1010
public boolean isValidBST(TreeNode root) {
11-
if (root.left != null) {
12-
if (root.val < root.left.val) {
13-
return false;
14-
}
15-
return isValidBST(root.left);
11+
return validate(root, Long.MIN_VALUE, Long.MAX_VALUE);
12+
}
1613

14+
public boolean validate(TreeNode node, long min, long max) {
15+
if (node == null) {
16+
return true;
1717
}
18-
if (root.right != null) {
19-
20-
if (root.val > root.right.val) {
21-
return false;
22-
}
23-
return isValidBST(root.right);
18+
if (node.val <= min || node.val >= max) {
19+
return false;
2420
}
25-
return true;
21+
return validate(node.left, min, node.val) && validate(node.right, node.val, max);
2622
}
2723

24+
2825
public static void main(String[] args) {
2926
int[] arr = {3, 4, 2, 5, 1, 6};
3027

src/main/java/com/leetcode/dynamicplan/DynPlanOne.java renamed to src/main/java/com/leetcode/dynamicplan/ClimbingStairs.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* name: Climbing Stairs
99
* fibonacci sequence
1010
*/
11-
public class DynPlanOne {
11+
public class ClimbingStairs {
1212

1313
public static int fibonacci(int n) {
1414
if (n == 1) {

src/main/java/com/leetcode/heap/HeapOne.java renamed to src/main/java/com/leetcode/heap/KthLargest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/**
77
* Kth Largest Element in an Array
88
*/
9-
public class HeapOne {
9+
public class KthLargest {
1010

1111
public static int partition(int[] nums, int low, int high) {
1212
int pivot = nums[high];
@@ -49,7 +49,7 @@ public static void main(String[] args) {
4949

5050
int[] nums = new int[]{9,8,7,6,5,4,3,2,1};
5151
int k = 2;
52-
int kthLargest = new HeapOne().findKthLargest(nums, k);
52+
int kthLargest = new KthLargest().findKthLargest(nums, k);
5353
System.out.println(kthLargest);
5454
}
5555
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.leetcode.slidingwindow;
2+
3+
import java.util.ArrayList;
4+
import java.util.function.IntFunction;
5+
6+
public class MaxSlidingWindow {
7+
8+
9+
public int[] maxSlidingWindow(int[] nums, int k) {
10+
ArrayList<Integer> result = new ArrayList<>();
11+
for(int i = 0 , j= k -1; j < nums.length ; i++ , j++){
12+
int max = Integer.MIN_VALUE;
13+
for(int x = i ; x <= j ; x ++){
14+
if(nums[x] > max){
15+
max = nums[x];
16+
}
17+
}
18+
result.add(max);
19+
}
20+
return result.stream().mapToInt(Integer::intValue).toArray();
21+
}
22+
23+
public static void main(String[] args) {
24+
25+
int[] nums = {1,3,-1,-3,5,3,6,7};
26+
int k = 3;
27+
int[] ints = new MaxSlidingWindow().maxSlidingWindow(nums, k);
28+
for(int i : ints){
29+
System.out.println(i);
30+
}
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.leetcode.slidingwindow;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class MinWindow {
7+
8+
9+
public String minWindow(String s, String t) {
10+
Map<Character, Integer> targetMap = new HashMap<>();
11+
for (char c : t.toCharArray()) {
12+
targetMap.put(c, targetMap.getOrDefault(c, 0) + 1);
13+
}
14+
15+
int left = 0, right = 0;
16+
int minLen = Integer.MAX_VALUE;
17+
int minStart = 0;
18+
int count = t.length();
19+
20+
while (right < s.length()) {
21+
char c = s.charAt(right);
22+
if (targetMap.containsKey(c)) {
23+
if (targetMap.get(c) > 0) {
24+
count--;
25+
}
26+
targetMap.put(c, targetMap.get(c) - 1);
27+
}
28+
29+
while (count == 0) {
30+
if (right - left + 1 < minLen) {
31+
minLen = right - left + 1;
32+
minStart = left;
33+
}
34+
35+
char leftChar = s.charAt(left);
36+
if (targetMap.containsKey(leftChar)) {
37+
targetMap.put(leftChar, targetMap.get(leftChar) + 1);
38+
if (targetMap.get(leftChar) > 0) {
39+
count++;
40+
}
41+
}
42+
43+
left++;
44+
}
45+
46+
right++;
47+
}
48+
49+
return minLen == Integer.MAX_VALUE ? "" : s.substring(minStart, minStart + minLen);
50+
}
51+
52+
53+
public static void main(String[] args) {
54+
55+
String str = "ADOBECODEBANC";
56+
String substr = "ABC";
57+
String s = new MinWindow().minWindow(str, substr);
58+
System.out.println(s);
59+
60+
}
61+
}

0 commit comments

Comments
 (0)