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; }