Given a string, find the length of the longest substring without repeating characters.題目要求輸入一個字符串,咱們要找出其中不含重複字符的最長子字符串,返回這個最長子字符串的長度。code
Examples:
輸入"abcabcbb",最長不含重複字符子字符串"abc",返回3.
輸入"bbbbb",最長不含重複字符子字符串"b",返回1.
輸入"pwwkew",最長不含重複字符子字符串"wke",返回3.字符串
public int lengthOfLongestSubstring(String s) { int max = 0; String temp = ""; for(char c : s.toCharArray()){ int index = temp.indexOf(c); if(index < 0) temp += c; else{ max = (temp.length() > max) ? temp.length() : max ; temp = temp.substring(index+1); temp += c; } } max = (temp.length() > max) ? temp.length() : max; return max; }