Skip to content

Commit 66515bd

Browse files
author
vesper
committed
feat: bytedance.longest_substring_without_repeating_characters
1 parent 4bed6ae commit 66515bd

File tree

3 files changed

+53
-0
lines changed

3 files changed

+53
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@
113113
- [225. 用队列实现栈](./src/leetcode/design/leet_zh_225/MyStack.java)
114114
- [232. 用栈实现队列](./src/leetcode/design/leet_zh_232/MyQueue.java)
115115

116+
- 【专项】
117+
- 【ByteDance】
118+
- [无重复字符的最长子串](./src/leetcode/specialized/bytedance/longest_substring_without_repeating_characters/Solution.java)
119+
116120
#### LeetCode 周赛
117121

118122
- 【第 129 周】[题目链接](https://leetcode-cn.com/contest/weekly-contest-129)

src/leetcode/specialized/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
## 专项
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package leetcode.specialized.bytedance.longest_substring_without_repeating_characters;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
public class Solution {
7+
/*
8+
* 无重复字符的最长子串
9+
* link: https://leetcode-cn.com/explore/interview/card/bytedance/242/string/1012
10+
* Label: 滑动窗口、hashMap、双指针
11+
*
12+
* 执行用时 : 8 ms
13+
* 内存消耗 : 40.4 MB
14+
15+
* */
16+
public int lengthOfLongestSubstring(String s) {
17+
int maxLength = 0;
18+
if (s.length() == 0) {
19+
return maxLength;
20+
}
21+
22+
char a;
23+
int start, index;
24+
start = 0;
25+
Map<Character, Integer> tmp = new HashMap<>();
26+
for (int i = 0; i < s.length(); i++) {
27+
a = s.charAt(i);
28+
if (tmp.containsKey(a)) {
29+
index = tmp.get(a);
30+
// >= 非 >
31+
if (index >= start) {
32+
start = tmp.get(a) + 1;
33+
}
34+
}
35+
maxLength = Math.max(maxLength, i - start + 1);
36+
tmp.put(a, i);
37+
}
38+
return maxLength;
39+
}
40+
41+
public static void main(String[] args) {
42+
Solution ss = new Solution();
43+
System.out.println(ss.lengthOfLongestSubstring("abcabcbb"));
44+
System.out.println(ss.lengthOfLongestSubstring("bbbbb"));
45+
System.out.println(ss.lengthOfLongestSubstring("pwwkew"));
46+
System.out.println(ss.lengthOfLongestSubstring("abba"));
47+
}
48+
}

0 commit comments

Comments
 (0)