-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
170 additions
and
0 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
longest-substring-without-repeating-characters/neverlish.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// 시간복잡도: O(n^2) | ||
// 공간복잡도: O(n) | ||
|
||
package main | ||
|
||
import "testing" | ||
|
||
func TestLengthOfLongestSubstring(t *testing.T) { | ||
result1 := lengthOfLongestSubstring("abcabcbb") | ||
|
||
if result1 != 3 { | ||
t.Errorf("Expected 3, but got %v", result1) | ||
} | ||
|
||
result2 := lengthOfLongestSubstring("bbbbb") | ||
if result2 != 1 { | ||
t.Errorf("Expected 1, but got %v", result2) | ||
} | ||
|
||
result3 := lengthOfLongestSubstring("pwwkew") | ||
if result3 != 3 { | ||
t.Errorf("Expected 3, but got %v", result3) | ||
} | ||
|
||
result4 := lengthOfLongestSubstring("") | ||
if result4 != 0 { | ||
t.Errorf("Expected 0, but got %v", result4) | ||
} | ||
} | ||
|
||
func lengthOfLongestSubstring(s string) int { | ||
result := 0 | ||
|
||
for i := 0; i < len(s); i++ { | ||
m := make(map[rune]bool) | ||
for j := i; j < len(s); j++ { | ||
if _, ok := m[rune(s[j])]; ok { | ||
break | ||
} | ||
m[rune(s[j])] = true | ||
} | ||
|
||
if len(m) > result { | ||
result = len(m) | ||
} | ||
} | ||
|
||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// 시간복잡도: O(n^2) | ||
// 공간복잡도: O(n) | ||
|
||
package main | ||
|
||
import "testing" | ||
|
||
func TestNumIslands(t *testing.T) { | ||
result1 := numIslands([][]byte{ | ||
{'1', '1', '1', '1', '0'}, | ||
{'1', '1', '0', '1', '0'}, | ||
{'1', '1', '0', '0', '0'}, | ||
{'0', '0', '0', '0', '0'}, | ||
}) | ||
|
||
if result1 != 1 { | ||
t.Errorf("Expected 1, but got %v", result1) | ||
} | ||
|
||
result2 := numIslands([][]byte{ | ||
{'1', '1', '0', '0', '0'}, | ||
{'1', '1', '0', '0', '0'}, | ||
{'0', '0', '1', '0', '0'}, | ||
{'0', '0', '0', '1', '1'}, | ||
}) | ||
|
||
if result2 != 3 { | ||
t.Errorf("Expected 3, but got %v", result2) | ||
} | ||
} | ||
|
||
func dfs(grid [][]byte, i, j int) { | ||
if i < 0 || j < 0 || i >= len(grid) || j >= len(grid[i]) || grid[i][j] == '0' { | ||
return | ||
} | ||
|
||
grid[i][j] = '0' | ||
|
||
dfs(grid, i-1, j) | ||
dfs(grid, i+1, j) | ||
dfs(grid, i, j-1) | ||
dfs(grid, i, j+1) | ||
} | ||
|
||
func numIslands(grid [][]byte) int { | ||
result := 0 | ||
|
||
for i := 0; i < len(grid); i++ { | ||
for j := 0; j < len(grid[i]); j++ { | ||
if grid[i][j] == '1' { | ||
result++ | ||
dfs(grid, i, j) | ||
} | ||
} | ||
} | ||
|
||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// 시간복잡도: O(n) | ||
// 공간복잡도: O(1) | ||
|
||
package main | ||
|
||
import "testing" | ||
|
||
func TestReverseList(t *testing.T) { | ||
result1 := reverseList(&ListNode{ | ||
Val: 1, | ||
Next: &ListNode{ | ||
Val: 2, | ||
Next: &ListNode{ | ||
Val: 3, | ||
Next: &ListNode{ | ||
Val: 4, | ||
Next: &ListNode{ | ||
Val: 5, | ||
Next: nil, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
|
||
if result1.Val != 5 { | ||
t.Errorf("Expected 5, but got %v", result1.Val) | ||
} | ||
|
||
if result1.Next.Val != 4 { | ||
t.Errorf("Expected 4, but got %v", result1.Next.Val) | ||
} | ||
|
||
if result1.Next.Next.Val != 3 { | ||
t.Errorf("Expected 3, but got %v", result1.Next.Next.Val) | ||
} | ||
|
||
if result1.Next.Next.Next.Val != 2 { | ||
t.Errorf("Expected 2, but got %v", result1.Next.Next.Next.Val) | ||
} | ||
|
||
if result1.Next.Next.Next.Next.Val != 1 { | ||
t.Errorf("Expected 1, but got %v", result1.Next.Next.Next.Next.Val) | ||
} | ||
} | ||
|
||
type ListNode struct { | ||
Val int | ||
Next *ListNode | ||
} | ||
|
||
func reverseList(head *ListNode) *ListNode { | ||
var prev *ListNode | ||
|
||
for head != nil { | ||
next := head.Next | ||
head.Next = prev | ||
prev = head | ||
head = next | ||
} | ||
|
||
return prev | ||
} |