author: Blankj blog : http://blankj.com time : 2017/10/11 desc :
| 9 | * </pre> |
| 10 | */ |
| 11 | public class Solution { |
| 12 | public int lengthOfLongestSubstring(String s) { |
| 13 | int len; |
| 14 | if (s == null || (len = s.length()) == 0) return 0; |
| 15 | int preP = 0, max = 0; |
| 16 | int[] hash = new int[128]; |
| 17 | for (int i = 0; i < len; ++i) { |
| 18 | char c = s.charAt(i); |
| 19 | if (hash[c] > preP) { |
| 20 | preP = hash[c]; |
| 21 | } |
| 22 | int l = i - preP + 1; |
| 23 | hash[c] = i + 1; |
| 24 | if (l > max) max = l; |
| 25 | } |
| 26 | return max; |
| 27 | } |
| 28 | |
| 29 | public static void main(String[] args) { |
| 30 | Solution solution = new Solution(); |
| 31 | System.out.println(solution.lengthOfLongestSubstring("abcabcbb")); |
| 32 | System.out.println(solution.lengthOfLongestSubstring("bbbbb")); |
| 33 | System.out.println(solution.lengthOfLongestSubstring("pwwkew")); |
| 34 | System.out.println(solution.lengthOfLongestSubstring("Abcabcbb")); |
| 35 | } |
| 36 | } |
nothing calls this directly
no outgoing calls
no test coverage detected