Skip to content

Commit 2496829

Browse files
authored
week 07 (#957)
1 parent f4393df commit 2496829

File tree

3 files changed

+170
-0
lines changed

3 files changed

+170
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// 시간복잡도: O(n^2)
2+
// 공간복잡도: O(n)
3+
4+
package main
5+
6+
import "testing"
7+
8+
func TestLengthOfLongestSubstring(t *testing.T) {
9+
result1 := lengthOfLongestSubstring("abcabcbb")
10+
11+
if result1 != 3 {
12+
t.Errorf("Expected 3, but got %v", result1)
13+
}
14+
15+
result2 := lengthOfLongestSubstring("bbbbb")
16+
if result2 != 1 {
17+
t.Errorf("Expected 1, but got %v", result2)
18+
}
19+
20+
result3 := lengthOfLongestSubstring("pwwkew")
21+
if result3 != 3 {
22+
t.Errorf("Expected 3, but got %v", result3)
23+
}
24+
25+
result4 := lengthOfLongestSubstring("")
26+
if result4 != 0 {
27+
t.Errorf("Expected 0, but got %v", result4)
28+
}
29+
}
30+
31+
func lengthOfLongestSubstring(s string) int {
32+
result := 0
33+
34+
for i := 0; i < len(s); i++ {
35+
m := make(map[rune]bool)
36+
for j := i; j < len(s); j++ {
37+
if _, ok := m[rune(s[j])]; ok {
38+
break
39+
}
40+
m[rune(s[j])] = true
41+
}
42+
43+
if len(m) > result {
44+
result = len(m)
45+
}
46+
}
47+
48+
return result
49+
}

number-of-islands/neverlish.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// 시간복잡도: O(n^2)
2+
// 공간복잡도: O(n)
3+
4+
package main
5+
6+
import "testing"
7+
8+
func TestNumIslands(t *testing.T) {
9+
result1 := numIslands([][]byte{
10+
{'1', '1', '1', '1', '0'},
11+
{'1', '1', '0', '1', '0'},
12+
{'1', '1', '0', '0', '0'},
13+
{'0', '0', '0', '0', '0'},
14+
})
15+
16+
if result1 != 1 {
17+
t.Errorf("Expected 1, but got %v", result1)
18+
}
19+
20+
result2 := numIslands([][]byte{
21+
{'1', '1', '0', '0', '0'},
22+
{'1', '1', '0', '0', '0'},
23+
{'0', '0', '1', '0', '0'},
24+
{'0', '0', '0', '1', '1'},
25+
})
26+
27+
if result2 != 3 {
28+
t.Errorf("Expected 3, but got %v", result2)
29+
}
30+
}
31+
32+
func dfs(grid [][]byte, i, j int) {
33+
if i < 0 || j < 0 || i >= len(grid) || j >= len(grid[i]) || grid[i][j] == '0' {
34+
return
35+
}
36+
37+
grid[i][j] = '0'
38+
39+
dfs(grid, i-1, j)
40+
dfs(grid, i+1, j)
41+
dfs(grid, i, j-1)
42+
dfs(grid, i, j+1)
43+
}
44+
45+
func numIslands(grid [][]byte) int {
46+
result := 0
47+
48+
for i := 0; i < len(grid); i++ {
49+
for j := 0; j < len(grid[i]); j++ {
50+
if grid[i][j] == '1' {
51+
result++
52+
dfs(grid, i, j)
53+
}
54+
}
55+
}
56+
57+
return result
58+
}

reverse-linked-list/neverlish.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// 시간복잡도: O(n)
2+
// 공간복잡도: O(1)
3+
4+
package main
5+
6+
import "testing"
7+
8+
func TestReverseList(t *testing.T) {
9+
result1 := reverseList(&ListNode{
10+
Val: 1,
11+
Next: &ListNode{
12+
Val: 2,
13+
Next: &ListNode{
14+
Val: 3,
15+
Next: &ListNode{
16+
Val: 4,
17+
Next: &ListNode{
18+
Val: 5,
19+
Next: nil,
20+
},
21+
},
22+
},
23+
},
24+
})
25+
26+
if result1.Val != 5 {
27+
t.Errorf("Expected 5, but got %v", result1.Val)
28+
}
29+
30+
if result1.Next.Val != 4 {
31+
t.Errorf("Expected 4, but got %v", result1.Next.Val)
32+
}
33+
34+
if result1.Next.Next.Val != 3 {
35+
t.Errorf("Expected 3, but got %v", result1.Next.Next.Val)
36+
}
37+
38+
if result1.Next.Next.Next.Val != 2 {
39+
t.Errorf("Expected 2, but got %v", result1.Next.Next.Next.Val)
40+
}
41+
42+
if result1.Next.Next.Next.Next.Val != 1 {
43+
t.Errorf("Expected 1, but got %v", result1.Next.Next.Next.Next.Val)
44+
}
45+
}
46+
47+
type ListNode struct {
48+
Val int
49+
Next *ListNode
50+
}
51+
52+
func reverseList(head *ListNode) *ListNode {
53+
var prev *ListNode
54+
55+
for head != nil {
56+
next := head.Next
57+
head.Next = prev
58+
prev = head
59+
head = next
60+
}
61+
62+
return prev
63+
}

0 commit comments

Comments
 (0)