Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c7049e3

Browse files
求一个字符串的最大不重复子串
1 parent a67d740 commit c7049e3

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

‎src/main/java/com/algorithm/study/demo/algorithm/Solution.java‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package com.algorithm.study.demo.algorithm;
22

3+
import java.util.HashSet;
4+
import java.util.LinkedHashSet;
5+
import java.util.Set;
6+
import java.util.TreeSet;
7+
38
/**
49
* @Author: liuxun
510
* @CreateDate: 2019年2月13日 下午3:11
@@ -23,4 +28,30 @@ public static int reverse(int x) {
2328
result = 0;
2429
return (int)result;
2530
}
31+
32+
/**
33+
* 求一个字符串的最大不重复子串
34+
*/
35+
public static int lengthOfLongestSubstring(String s) {
36+
int n = s.length();
37+
Set<Character> set = new HashSet<>();
38+
int ans = 0, i = 0, j = 0;
39+
while (i < n && j < n) {
40+
// try to extend the range [i, j]
41+
if (!set.contains(s.charAt(j))){
42+
set.add(s.charAt(j++));
43+
ans = Math.max(ans, j - i);
44+
}
45+
else {
46+
//删除重复的字符
47+
set.remove(s.charAt(i++));
48+
}
49+
}
50+
return ans;
51+
}
52+
53+
public static void main(String[] args) {
54+
System.out.println(lengthOfLongestSubstring("abaa"));
55+
}
56+
2657
}

‎src/main/java/com/algorithm/study/demo/datastructure/linear/Solution.java‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.algorithm.study.demo.datastructure.linear;
22

33
/**
4-
* 求一个字符串的最大不重复子串
54
* 单链表链表算法题
65
* @Author: liuxun
76
* @CreateDate: 2019年2月12日 下午3:43

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /