Java 使用HashSet的字符串的最长子字符串的长度
我已经得到了最长子字符串长度的代码,但我不确定下面代码的“else”部分是什么。 另外,这里开始的变量表示什么Java 使用HashSet的字符串的最长子字符串的长度,java,string,collections,set,hashset,Java,String,Collections,Set,Hashset,我已经得到了最长子字符串长度的代码,但我不确定下面代码的“else”部分是什么。 另外,这里开始的变量表示什么 while(start < i && s.charAt(start)!=c){ set.remove(s.charAt(start)); start++; } start++; while(开始
while(start < i && s.charAt(start)!=c){
set.remove(s.charAt(start));
start++;
}
start++;
while(开始
方法的完整代码:
public static int lengthOfLongestSubstring(String s) {
if(s==null || s.length()==0)
return 0;
HashSet<Character> set = new HashSet<Character>();
int max=0;
int i=0;
int start=0;
while(i < s.length()){
char c = s.charAt(i);
if(!set.contains(c)){
set.add(c);
}else{
max = Math.max(max, set.size());
while(start < i && s.charAt(start)!=c){
set.remove(s.charAt(start));
start++;
}
start++;
}
i++;
}
max = Math.max(max, set.size());
return max;
}
public static int lengthOfLongestSubstring(字符串s){
如果(s==null | | s.length()==0)
返回0;
HashSet=newhashset();
int max=0;
int i=0;
int start=0;
而(i
该问题要求使用最长的子字符串,其中子字符串中的每个字符都是唯一的。这个问题要求描述else块做什么,以及描述start。要描述,请考虑字符串<强> ABCDBFG
开始=0;//指向唯一子字符串中第一个字符的指针
“最长子字符串”的定义是什么?最长子字符串可以是任何要验证其最长子字符串长度的字符串:给定“abcabcbb”,答案是“abc”,长度为3。给定“bbbbb”,答案是“b”,长度为1。根据这些例子,“最长的子字符串”实际上是“字符串中唯一字符的计数”,这是真的。具有所有唯一字符的最长子字符串。“abcabcdab”的“最长子字符串”应该是什么?为什么?