public class Solution {
public static void main(String[] args){
String str = "abcabcbb";
System.out.println(lengthOfLongestSubstring(str));
}
public static int lengthOfLongestSubstring(String s) {
int n = s.length();
// Set<Character> set = new HashSet<>();
HashSet<Character> set = new HashSet<>();
int ans = 0, i = 0, j = 0;
while (i < n && j < n) {
// try to extend the range [i, j]
if (!set.contains(s.charAt(j))){
set.add(s.charAt(j++));
ans = Math.max(ans, j - i);
}
else {
set.remove(s.charAt(i++));
}
}
return ans;
}
}
javac 编译时,提示
Solution.java:15: 错误: 找不到符号
HashSet<Character> set = new HashSet<>();
^
符号: 类 HashSet
位置: 类 Solution
Solution.java:15: 错误: 找不到符号
HashSet<Character> set = new HashSet<>();
^
符号: 类 HashSet
位置: 类 Solution
2 个错误
在顶部引入相关类库就能正常编译
import java.util.HashSet;
在java使用中,使用java的一些函数则需要引入相应类库