Skip to content

Commit 04dabd9

Browse files
committed
Solve 'Longest Substring Without Repeating Characters'
1 parent 0dd59a7 commit 04dabd9

File tree

15 files changed

+197
-33
lines changed

15 files changed

+197
-33
lines changed

add-two-numbers/main.go renamed to add-two-numbers/add.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package addtwonumbers
22

33
// ListNode represents singly-linked list.
44
type ListNode struct {

add-two-numbers/main_test.go renamed to add-two-numbers/add_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package addtwonumbers
22

33
import (
44
"testing"

hamming_distance/main.go renamed to hamming_distance/hamming.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package hammingdistance
22

33
import (
44
"strconv"

hamming_distance/hamming_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package hammingdistance
22

33
import (
44
"fmt"

hamming_distance/spec.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# 461 Hamming Distance
22

3-
<https://leetcode.com/problems/hamming-distance/description/>
3+
<https://leetcode.com/problems/hamming-distance/>
44

55
The Hamming distance between two integers is the number of positions
66
at which the corresponding bits are different.

jewels_and_stones/main.go renamed to jewels_and_stones/jewels.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package jewelsandstones
22

33
import "fmt"
44

jewels_and_stones/jewels_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package jewelsandstones
22

33
import (
44
"fmt"

jewels_and_stones/spec.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# 771 Jewels and Stones
22

3-
<https://leetcode.com/problems/jewels-and-stones/description/>
3+
<https://leetcode.com/problems/jewels-and-stones>
44

55
You're given strings J representing the types of stones that are jewels, and S representing the stones you have.
66
Each character in S is a type of stone you have. You want to know how many of the stones you have are also jewels.

judge_route_circle/main.go renamed to judge_route_circle/judge.go

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,35 @@
1-
package main
1+
package judgecircle
22

33
import (
4-
"fmt"
54
"strings"
65
)
76

8-
type Robot struct {
9-
position Position
7+
type robot struct {
8+
position position
109
}
1110

12-
type Position struct {
11+
type position struct {
1312
x int
1413
y int
1514
}
1615

17-
func (r *Robot) moveRight() {
16+
func (r *robot) moveRight() {
1817
r.position.y++
1918
}
2019

21-
func (r *Robot) moveLeft() {
20+
func (r *robot) moveLeft() {
2221
r.position.y--
2322
}
2423

25-
func (r *Robot) moveUp() {
24+
func (r *robot) moveUp() {
2625
r.position.x++
2726
}
2827

29-
func (r *Robot) moveDown() {
28+
func (r *robot) moveDown() {
3029
r.position.x--
3130
}
3231

33-
func (r *Robot) Move(moves string) {
32+
func (r *robot) move(moves string) {
3433
for _, step := range moves {
3534
switch strings.ToUpper(string(step)) {
3635
case "R":
@@ -41,23 +40,21 @@ func (r *Robot) Move(moves string) {
4140
r.moveUp()
4241
case "D":
4342
r.moveDown()
44-
default:
45-
fmt.Printf("Wrong movement!")
4643
}
4744
}
4845
}
4946

5047
func judgeCircle(moves string) bool {
51-
robot := Robot{
52-
position: Position{
48+
r := robot{
49+
position: position{
5350
x: 0,
5451
y: 0,
5552
},
5653
}
5754

58-
robot.Move(moves)
55+
r.move(moves)
5956

60-
if robot.position.x == 0 && robot.position.y == 0 {
57+
if r.position.x == 0 && r.position.y == 0 {
6158
return true
6259
}
6360

judge_route_circle/judge_circle_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package judgecircle
22

33
import (
44
"fmt"

judge_route_circle/spec.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
1-
# 657 Judge Route Circle
1+
# 657. Robot Return to Origin
22

3-
<https://leetcode.com/problems/judge-route-circle/description/>
3+
<https://leetcode.com/problems/robot-return-to-origin/>
44

5-
Initially, there is a Robot at position (0, 0). Given a sequence of its moves,
6-
judge if this robot makes a circle, which means it moves back to the original place.
5+
There is a robot starting at position (0, 0), the origin, on a 2D plane. Given a sequence of its moves, judge if this robot ends up at (0, 0) after it completes its moves.
76

8-
The move sequence is represented by a string. And each move is represent by a character.
9-
The valid robot moves are R (Right), L (Left), U (Up) and D (down).
10-
The output should be true or false representing whether the robot makes a circle.
7+
The move sequence is represented by a string, and the character moves[i] represents its ith move. Valid moves are R (right), L (left), U (up), and D (down). If the robot returns to the origin after it finishes all of its moves, return true. Otherwise, return false.
8+
9+
Note: The way that the robot is "facing" is irrelevant. "R" will always make the robot move to the right once, "L" will always make it move left, etc. Also, assume that the magnitude of the robot's movement is the same for each move.
1110

1211
Example 1:
1312

1413
```text
1514
Input: "UD"
1615
Output: true
16+
Explanation: The robot moves up once, and then down once. All moves have the same magnitude, so it ended up at the origin where it started. Therefore, we return true.
1717
```
1818

1919
Example 2:
2020

2121
```text
2222
Input: "LL"
2323
Output: false
24+
Explanation: The robot moves left twice. It ends up two "moves" to the left of the origin. We return false because it is not at the origin at the end of its moves.
2425
```
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# 3. Longest Substring Without Repeating Characters
2+
3+
Given a string, find the length of the longest substring without repeating characters.
4+
5+
Example 1:
6+
7+
``` text
8+
Input: "abcabcbb"
9+
Output: 3
10+
Explanation: The answer is "abc", with the length of 3.
11+
```
12+
13+
Example 2:
14+
15+
```text
16+
Input: "bbbbb"
17+
Output: 1
18+
Explanation: The answer is "b", with the length of 1.
19+
```
20+
21+
Example 3:
22+
23+
```text
24+
Input: "pwwkew"
25+
Output: 3
26+
Explanation: The answer is "wke", with the length of 3.
27+
Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
28+
```
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package longestsubstring
2+
3+
import (
4+
"sort"
5+
)
6+
7+
func lengthOfLongestSubstring(s string) int {
8+
if s == "" {
9+
return 0
10+
}
11+
12+
substrings := split(s)
13+
if len(substrings) == 0 {
14+
return 0
15+
}
16+
17+
sort.Sort(byLen(substrings))
18+
19+
return len(substrings[0])
20+
}
21+
22+
type byLen []string
23+
24+
func (a byLen) Len() int {
25+
return len(a)
26+
}
27+
28+
func (a byLen) Less(i, j int) bool {
29+
return len(a[i]) > len(a[j])
30+
}
31+
32+
func (a byLen) Swap(i, j int) {
33+
a[i], a[j] = a[j], a[i]
34+
}
35+
36+
func split(s string) []string {
37+
var substrings []string
38+
39+
length := len(s)
40+
if length == 1 {
41+
substrings = append(substrings, s)
42+
}
43+
for i := 0; i < length; i++ {
44+
var cur string
45+
46+
seen := make(map[string]struct{})
47+
48+
for j := i; j < length; j++ {
49+
e := string(s[j])
50+
51+
if _, exist := seen[e]; !exist {
52+
cur += e
53+
seen[e] = struct{}{}
54+
} else {
55+
break
56+
}
57+
}
58+
59+
substrings = append(substrings, cur)
60+
}
61+
62+
return substrings
63+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package longestsubstring
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func Test_lengthOfLongestSubstring(t *testing.T) {
10+
type args struct {
11+
s string
12+
}
13+
tests := []struct {
14+
name string
15+
args args
16+
want int
17+
}{
18+
{
19+
name: "abcabcbb",
20+
args: args{
21+
s: "abcabcbb",
22+
},
23+
want: 3,
24+
},
25+
{
26+
name: "bbbbb",
27+
args: args{
28+
s: "bbbbb",
29+
},
30+
want: 1,
31+
},
32+
{
33+
name: "pwwkew",
34+
args: args{
35+
s: "pwwkew",
36+
},
37+
want: 3,
38+
},
39+
{
40+
name: "empty string",
41+
args: args{
42+
s: "",
43+
},
44+
want: 0,
45+
},
46+
{
47+
name: "empty string with spaces",
48+
args: args{
49+
s: " ",
50+
},
51+
want: 1,
52+
},
53+
{
54+
name: "one letter",
55+
args: args{
56+
s: "a",
57+
},
58+
want: 1,
59+
},
60+
{
61+
name: "no repeated",
62+
args: args{
63+
s: "au",
64+
},
65+
want: 2,
66+
},
67+
}
68+
69+
for _, tt := range tests {
70+
t.Run(tt.name, func(t *testing.T) {
71+
got := lengthOfLongestSubstring(tt.args.s)
72+
assert.Equal(t, tt.want, got)
73+
})
74+
}
75+
}

unique_morse_code_words/spec.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# 804 Unique Morse Code Words
22

3-
<https://leetcode.com/problems/unique-morse-code-words/description/>
3+
<https://leetcode.com/problems/unique-morse-code-words/>
44

55
International Morse Code defines a standard encoding where each letter is mapped to a series of dots and dashes,
66
as follows: "a" maps to ".-", "b" maps to "-...", "c" maps to "-.-.", and so on.

0 commit comments

Comments
 (0)