首页 > 代码库 > 3 Longest Substring Without Repeating Characters

3 Longest Substring Without Repeating Characters

 1     public int lengthOfLongestSubstring(String s) {
 2         long length = s.length();
 3         String tmp = "";
 4         int substringLength = 0;
 5         for (int i = 0; i < length; i ++) {
 6             if (tmp.contains(("" + s.charAt(i)))) {
 7                 if (tmp.length() > substringLength)
 8                     substringLength = tmp.length();
 9                 int idx = tmp.indexOf(s.charAt(i) + "");
10                 tmp = tmp.substring(idx + 1) + s.charAt(i);
11             }
12             else {
13                 tmp = tmp + s.charAt(i);
14                 if (substringLength < tmp.length())
15                     substringLength = tmp.length();
16             }
17         }
18         return substringLength;
19     }

 

3 Longest Substring Without Repeating Characters