Skip to content

Commit b3b67ce

Browse files
committed
pass problem 3 - longest substring without repeating characters
1 parent 887aee3 commit b3b67ce

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package double_pointers.longest_substring_without_repeating_characters;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* @author Bota5ky
8+
* @link <a href="https://leetcode.cn/problems/longest-substring-without-repeating-characters/">3. 无重复字符的最长子串</a>
9+
* @since 2023-11-03 21:00
10+
*/
11+
class Solution {
12+
public int lengthOfLongestSubstring(String s) {
13+
if (s.isEmpty()) {
14+
return 0;
15+
}
16+
Map<Character, Integer> map = new HashMap<>();
17+
int maxLen = 1;
18+
for (int i = 0, j = 0; j < s.length(); j++) {
19+
if (map.containsKey(s.charAt(j))) {
20+
i = Math.max(i, 1 + map.get(s.charAt(j)));
21+
}
22+
map.put(s.charAt(j), j);
23+
maxLen = Math.max(maxLen, j - i + 1);
24+
}
25+
return maxLen;
26+
}
27+
}

double_pointers/maximum_points_you_can_obtain_from_cards/Solution.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ public int maxScore(int[] cardPoints, int k) {
1818
}
1919
return max;
2020
}
21-
}
21+
}

0 commit comments

Comments
 (0)