有谁能告诉我哪里出错了。以下是我的代码。 这里我使用左右滑动窗口解决方案作为String的指针。 “dist_count”包含字符串中唯一字符的计数。
public class Solution {
public String findSubstring(String s) {
if (s == null || s.length() == 0)
return "";
int dist_count = 0;
int[] hash = new int[256]; // character hash
boolean[] visited = new boolean[256];
// record each character in s to hash
for (char c : s.toCharArray()) {
hash[c]++;
}
for (char c : s.toCharArray()) {
if (visited[c] == false) {
dist_count++;
visited[c] = true;
}
}
System.out.println(dist_count);
int left = 0, right = 0, count = 0;
int min_window = Integer.MAX_VALUE;
int startIndex = -1;
while (right < s.length()) {
if (hash[s.charAt(right)] >= 1) {
count++;
}
hash[s.charAt(right)]--;
right++;
if (count == dist_count) {
while (hash[s.charAt(left)] > 1) {
if (hash[s.charAt(left)] > 1)
hash[s.charAt(left)]--;
left++;
}
int window = right - left + 1;
if (min_window > window) {
min_window = window;
startIndex = left;
}
}
}
return s.substring(startIndex, startIndex + min_window);
}
public static void main(String args[]) {
Solution ob = new Solution();
String str = ob.findSubstring("abdacbfcabegbdfcadcefa");
System.out.println(str);
}
}
答案 0 :(得分:0)