Skip to content

Commit 148e371

Browse files
committed
feat: add 003
1 parent 6773610 commit 148e371

File tree

8 files changed

+180
-54
lines changed

8 files changed

+180
-54
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
|#|Title|Tag|
4848
|:------------- |:------------- |:------------- |
4949
|2|[Add Two Numbers][002]|Linked List, Math|
50+
|3|[Longest Substring Without Repeating Characters][003]|Hash Table, Two Pointers, String|
5051
|8|[String to Integer (atoi)][008]|Math, String|
5152
|19|[Remove Nth Node From End of List][019]|Linked List, Two Pointers|
5253

@@ -97,6 +98,7 @@
9798
[122]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/122/README.md
9899

99100
[002]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/002/README.md
101+
[003]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/003/README.md
100102
[008]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/008/README.md
101103
[019]: https://github.com/Blankj/awesome-java-leetcode/blob/master/note/019/README.md
102104

note/003/README.md

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# [Longest Substring Without Repeating Characters][title]
2+
3+
## Description
4+
5+
Given a string, find the length of the **longest substring** without repeating characters.
6+
7+
**Examples:**
8+
9+
Given `"abcabcbb"`, the answer is `"abc"`, which the length is 3.
10+
11+
Given `"bbbbb"`, the answer is `"b"`, with the length of 1.
12+
13+
Given `"pwwkew"`, the answer is `"wke"`, with the length of 3. Note that the answer must be a **substring**, `"pwke"` is a *subsequence*and not a substring.
14+
15+
**Tags:** Hash Table, Two Pointers, String
16+
17+
18+
## 思路
19+
20+
题意是计算不带重复字符的最长子字符串的长度,开辟一个hash数组来存储该字符上次出现的位置,比如`hash[a] = 3`就是代表`a`字符前一次出现的索引在3,遍历该字符串,获取到上次出现的最大索引(只能向前,不能退后),与当前的索引做差获取的就是本次所需长度,从中迭代出最大值就是最终答案。
21+
22+
``` java
23+
class Solution {
24+
public int lengthOfLongestSubstring(String s) {
25+
int len;
26+
if (s == null || (len = s.length()) == 0) return 0;
27+
int preP = 0, max = 0;
28+
int[] hash = new int[128];
29+
for (int i = 0; i < len; ++i) {
30+
char c = s.charAt(i);
31+
if (hash[c] > preP) {
32+
preP = hash[c];
33+
}
34+
int l = i - preP + 1;
35+
hash[c] = i + 1;
36+
if (l > max) max = l;
37+
}
38+
return max;
39+
}
40+
}
41+
```
42+
43+
44+
## 结语
45+
46+
如果你同我一样热爱数据结构、算法、LeetCode,可以关注我GitHub上的LeetCode题解:[awesome-java-leetcode][ajl]
47+
48+
49+
50+
[title]: https://leetcode.com/problems/longest-substring-without-repeating-characters
51+
[ajl]: https://github.com/Blankj/awesome-java-leetcode

project/leetcode/.idea/workspace.xml

+69-47
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Binary file not shown.
Binary file not shown.
Binary file not shown.

project/leetcode/src/com/blankj/medium/_002/Solution.java

+22-7
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
import com.blankj.structure.ListNode;
44

5-
import java.util.Arrays;
6-
75
/**
86
* <pre>
97
* author: Blankj
@@ -13,15 +11,32 @@
1311
* </pre>
1412
*/
1513
public class Solution {
16-
1714
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
18-
15+
ListNode node = new ListNode(0);
16+
ListNode n1 = l1, n2 = l2, t = node;
17+
int sum = 0;
18+
while (n1 != null || n2 != null) {
19+
sum /= 10;
20+
if (n1 != null) {
21+
sum += n1.val;
22+
n1 = n1.next;
23+
}
24+
if (n2 != null) {
25+
sum += n2.val;
26+
n2 = n2.next;
27+
}
28+
t.next = new ListNode(sum % 10);
29+
t = t.next;
30+
}
31+
if (sum / 10 != 0) t.next = new ListNode(1);
32+
return node.next;
1933
}
2034

21-
2235
public static void main(String[] args) {
2336
Solution solution = new Solution();
24-
25-
System.out.println(Arrays.toString(solution.addTwoNumbers(nums, target)));
37+
ListNode.print(solution.addTwoNumbers(
38+
ListNode.createTestData("[2,4,3]"),
39+
ListNode.createTestData("[5,6,4]")
40+
));
2641
}
2742
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.blankj.medium._003;
2+
3+
/**
4+
* <pre>
5+
* author: Blankj
6+
* blog : http://blankj.com
7+
* time : 2017/10/11
8+
* 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+
}

0 commit comments

Comments
 (0)