diff --git a/longest-substring-without-repeating-characters/neverlish.go b/longest-substring-without-repeating-characters/neverlish.go new file mode 100644 index 000000000..98bbccc82 --- /dev/null +++ b/longest-substring-without-repeating-characters/neverlish.go @@ -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 +} diff --git a/number-of-islands/neverlish.go b/number-of-islands/neverlish.go new file mode 100644 index 000000000..b1459c265 --- /dev/null +++ b/number-of-islands/neverlish.go @@ -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 +} diff --git a/reverse-linked-list/neverlish.go b/reverse-linked-list/neverlish.go new file mode 100644 index 000000000..4fd989ae3 --- /dev/null +++ b/reverse-linked-list/neverlish.go @@ -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 +}