一、題目說明
Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest substring is "b", with the length of 1.
查看原題;
查看我的Github項目地址;
題目:求給定字符串中沒有重復(fù)字符的最長子字符串的長度。
二、解題
題目比較簡單,不多說
代碼如下:
public static int lengthOfLongestSubstring(String s) {
if (s == null) {
return 0;
}
if (s.length() == 1) {
return 1;
}
char[] sChars = s.toCharArray();
int sIndex = 0;
int eIndex = 0;
int lenght = 0;
while (eIndex < sChars.length) {
for (int i = sIndex; i < eIndex; i++) {
if (sChars[i] == sChars[eIndex]) {
lenght = eIndex - sIndex > lenght ? eIndex - sIndex : lenght;
sIndex = i + 1;
break;
}
}
eIndex++;
}
lenght = eIndex - sIndex > lenght ? eIndex - sIndex : lenght;
return lenght;
}