parent
c0e9fe57f2
commit
f488b8dbb6
@ -0,0 +1,27 @@
|
||||
package 第03期.mca_09;
|
||||
|
||||
// 本题测试链接 : https://leetcode.com/problems/longest-substring-without-repeating-characters/
|
||||
public class Code02_LongestSubstringWithoutRepeatingCharacters {
|
||||
|
||||
public static int lengthOfLongestSubstring(String s) {
|
||||
if (s == null || s.equals("")) {
|
||||
return 0;
|
||||
}
|
||||
char[] str = s.toCharArray();
|
||||
int[] map = new int[256];
|
||||
for (int i = 0; i < 256; i++) {
|
||||
map[i] = -1;
|
||||
}
|
||||
map[str[0]] = 0;
|
||||
int N = str.length;
|
||||
int ans = 1;
|
||||
int pre = 1;
|
||||
for (int i = 1; i < N; i++) {
|
||||
pre = Math.min(i - map[str[i]], pre + 1);
|
||||
ans = Math.max(ans, pre);
|
||||
map[str[i]] = i;
|
||||
}
|
||||
return ans;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
package 第03期.mca_09;
|
||||
|
||||
// 测试链接 : https://leetcode.com/problems/minimum-window-substring/
|
||||
public class Code03_MinWindowLength {
|
||||
|
||||
public static String minWindow(String s, String t) {
|
||||
if (s.length() < t.length()) {
|
||||
return "";
|
||||
}
|
||||
char[] str = s.toCharArray();
|
||||
char[] target = t.toCharArray();
|
||||
int[] map = new int[256];
|
||||
for (char cha : target) {
|
||||
map[cha]++;
|
||||
}
|
||||
int all = target.length;
|
||||
int L = 0;
|
||||
int R = 0;
|
||||
int minLen = Integer.MAX_VALUE;
|
||||
int ansl = -1;
|
||||
int ansr = -1;
|
||||
while (R != str.length) {
|
||||
map[str[R]]--;
|
||||
if (map[str[R]] >= 0) {
|
||||
all--;
|
||||
}
|
||||
if (all == 0) {
|
||||
while (map[str[L]] < 0) {
|
||||
map[str[L++]]++;
|
||||
}
|
||||
if (minLen > R - L + 1) {
|
||||
minLen = R - L + 1;
|
||||
ansl = L;
|
||||
ansr = R;
|
||||
}
|
||||
all++;
|
||||
map[str[L++]]++;
|
||||
}
|
||||
R++;
|
||||
}
|
||||
return minLen == Integer.MAX_VALUE ? "" : s.substring(ansl, ansr + 1);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
package 第03期.mca_09;
|
||||
|
||||
// 测试链接 : https://leetcode.cn/problems/coin-change-ii/
|
||||
public class Code05_CoinChange {
|
||||
|
||||
public static int change(int aim, int[] arr) {
|
||||
if (arr == null || arr.length == 0 || aim < 0) {
|
||||
return 0;
|
||||
}
|
||||
int N = arr.length;
|
||||
int[][] dp = new int[N + 1][aim + 1];
|
||||
dp[N][0] = 1;
|
||||
for (int index = N - 1; index >= 0; index--) {
|
||||
for (int rest = 0; rest <= aim; rest++) {
|
||||
dp[index][rest] = dp[index + 1][rest];
|
||||
if (rest - arr[index] >= 0) {
|
||||
dp[index][rest] += dp[index][rest - arr[index]];
|
||||
}
|
||||
}
|
||||
}
|
||||
return dp[0][aim];
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue