力扣 第三题 无重复最长子串(java实现)
解题思想
1.将字符串转换成数组
2.双重for循环构造子串
3.通过String的contains(),判断新加入的字符是否存在,不存在则加入,否则break;
4.和之前子串比较长度
class Solution {
public int lengthOfLongestSubstring(String s) {
int length = 0;
int temp = 0;
String str1 = "";
char[] c = s.toCharArray();
for(int i = 0; i < c.length; i++){
for(int j = i; j < c.length; j++){
if(str1.contains(c[j]+"")){
str1 = "";
break;
}
str1 += c[j] + "";
temp++;
}
if(temp > length){
length = temp;
}
temp = 0;
}
return length;
}
}