本文共 1092 字,大约阅读时间需要 3 分钟。
3.Longest Substring Without Repeating Characters
Given a string, find the length of the longest substring without repeating characters.
Examples: Given "abcabcbb", the answer is "abc", which the length is 3. Given "bbbbb", the answer is "b", with the length of 1. Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.public int lengthOfLongestSubstring(String s) { int max = 0;int start = 0; Mapmap = new HashMap(); for(int i = 0;i < s.length();i++){ if(map.containsKey(s.charAt(i))){ start = Math.max(map.get(s.charAt(i)) + 1,start); } max = Math.max(max,i-start+1); map.put(s.charAt(i),i); } return max; }
转载地址:http://dedvi.baihongyu.com/