Update A76.java

This commit is contained in:
Mrhongxing committed 2026-10-15 01:14:56 +08:00
1 parent d424ed1891
commit 5cefd58c80
1 file changed
+48 -10
+48 -10
View File
@@ -3,7 +3,8 @@ package com.hong.www;
import java.util.*; import java.util.*;
public class A76 { public class A76 {
public int temp(char[] chars, HashMap<Character, Integer> temp, HashMap<Character, Integer> temp2, HashMap<Character, Integer> temp3, ArrayList<String> finalList, int left, int right, CharSequence s) { public int temp(char[] chars, HashMap<Character, Integer> temp, HashMap<Character, Integer> temp2,
HashMap<Character, Integer> temp3, ArrayList<String> finalList, int left, int right, CharSequence s) {
if (temp.containsKey(chars[right])) { if (temp.containsKey(chars[right])) {
temp2.put(chars[right], temp2.getOrDefault(chars[right], 0) + 1); temp2.put(chars[right], temp2.getOrDefault(chars[right], 0) + 1);
if (temp2.get(chars[right]) - temp.get(chars[right]) >= 0) { if (temp2.get(chars[right]) - temp.get(chars[right]) >= 0) {
@@ -19,29 +20,31 @@ public class A76 {
temp2.put(chars[left], temp2.get(chars[left]) - 1); temp2.put(chars[left], temp2.get(chars[left]) - 1);
if (temp2.get(chars[left]) - temp.get(chars[left]) >= 0) { if (temp2.get(chars[left]) - temp.get(chars[left]) >= 0) {
temp3.put(chars[left], 0); temp3.put(chars[left], 0);
}else{ } else {
temp3.put(chars[left], 1); temp3.put(chars[left], 1);
} }
for (int j = left + 1; j <= right; j++) { for (int j = left + 1; j <= right; j++) {
if (temp.containsKey(chars[j])) { if (temp.containsKey(chars[j])) {
left = j; left = j;
break; break;
} }
} }
left=temp(chars, temp, temp2, temp3, finalList, left, right, s); left = temp(chars, temp, temp2, temp3, finalList, left, right, s);
} }
return left; return left;
} }
public String minWindow(String s, String t) { public String minWindow(String s, String t) {
ArrayList<String> list = new ArrayList<>(); // ArrayList<String> list = new ArrayList<>();
char[] chars = s.toCharArray(); char[] chars = s.toCharArray();
char[] chart = t.toCharArray(); char[] chart = t.toCharArray();
HashMap<Character, Integer> temp = new HashMap<>(); HashMap<Character, Integer> temp = new HashMap<>();
HashMap<Character, Integer> temp2 = new HashMap<>(); HashMap<Character, Integer> temp2 = new HashMap<>();
HashMap<Character, Integer> temp3 = new HashMap<>(); HashMap<Character, Integer> temp3 = new HashMap<>();
ArrayList<String> finalList = new ArrayList<>(); // ArrayList<String> finalList = new ArrayList<>();
int finallong = Integer.MAX_VALUE;
String finalString = "";
for (char x : chart) { for (char x : chart) {
temp.put(x, temp.getOrDefault(x, 0) + 1); temp.put(x, temp.getOrDefault(x, 0) + 1);
temp3.put(x, 1); temp3.put(x, 1);
@@ -49,16 +52,51 @@ public class A76 {
int left = 0; int left = 0;
int right = 0; int right = 0;
while (right < chars.length) { while (right < chars.length) {
left=temp(chars, temp, temp2, temp3, finalList, left, right, s); // left=temp(chars, temp, temp2, temp3, finalList, left, right, s);
if (temp.containsKey(chars[right])) {
temp2.put(chars[right], temp2.getOrDefault(chars[right], 0) + 1);
if (temp2.get(chars[right]) - temp.get(chars[right]) >= 0) {
temp3.put(chars[right], 0);
}
}
int xx = 0;
for (int x : temp3.values()) {
xx += x;
}
while (xx == 0) {
if (temp2.containsKey(chars[left])) {
temp2.put(chars[left], temp2.get(chars[left]) - 1);
if (temp2.get(chars[left]) < temp.get(chars[left])) {
temp3.put(chars[left], 1);
if(finallong > right - left + 1) {
finallong = right - left + 1;
finalString = s.substring(left, right + 1);
}
left++;
if (left >= chars.length) {
break;
}
while (left < chars.length && !temp.containsKey(chars[left])) {
left++;
}
break;
}
}
left++;
xx = 0;
for (int x : temp3.values()) {
xx += x;
}
}
right++; right++;
} }
return "xx"; return finalString;
} }
public static void main(String[] args) { public static void main(String[] args) {
A76 a76 = new A76(); A76 a76 = new A76();
String s = "ADOBECODEBANC"; String s = "a";
String t = "ABC"; String t = "a";
System.out.println(a76.minWindow(s, t)); System.out.println(a76.minWindow(s, t));
} }
} }