MCPcopy Create free account
hub / github.com/Blankj/awesome-java-leetcode / Solution

Class Solution

src/com/blankj/medium/_0003/Solution.java:11–36  ·  view source on GitHub ↗

author: Blankj blog : http://blankj.com time : 2017/10/11 desc :

Source from the content-addressed store, hash-verified

9 * </pre>
10 */
11public 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}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected