diff --git a/construct-binary-tree-from-preorder-and-inorder-traversal/yeonguchoe.go b/construct-binary-tree-from-preorder-and-inorder-traversal/yeonguchoe.go new file mode 100644 index 000000000..cd6954ec7 --- /dev/null +++ b/construct-binary-tree-from-preorder-and-inorder-traversal/yeonguchoe.go @@ -0,0 +1,36 @@ +func indexOf(slice []int, value int) int { + for i, v := range slice { + if v == value { + return i + } + } + return -1 +} + +func buildTree(preorder []int, inorder []int) *TreeNode { + if len(preorder) == 0 || len(inorder) == 0 { + return nil + } + + // Preorder의 처음 값으로 root을 만듬 + rootVal := preorder[0] + root := &TreeNode{Val: rootVal} + + // inorder에서 root의 위치를 찾음 + mid := indexOf(inorder, rootVal) + + // inorder에서 mid의 왼쪽은 left subtree이고 오른쪽은 right subtree이다. + leftInorder := inorder[:mid] + rightInorder := inorder[mid+1:] + + // left preorder를 계산한다. + leftPreorder := preorder[1 : len(leftInorder)+1] + // right preorder를 계산한다. + rightPreorder := preorder[len(leftInorder)+1:] + + // tree 만들기 + root.Left = buildTree(leftPreorder, leftInorder) + root.Right = buildTree(rightPreorder, rightInorder) + + return root +} diff --git a/counting-bits/yeonguchoe.go b/counting-bits/yeonguchoe.go new file mode 100644 index 000000000..3ef922512 --- /dev/null +++ b/counting-bits/yeonguchoe.go @@ -0,0 +1,11 @@ +func countBits(n int) []int { + var result []int = make([]int, n+1) + var offset int = 1 + for i := 1; i <= n; i++ { + if i == offset*2 { + offset = offset * 2 + } + result[i] = 1 + result[i-offset] + } + return result +} diff --git a/decode-ways/yeonguchoe.go b/decode-ways/yeonguchoe.go new file mode 100644 index 000000000..29c019fa4 --- /dev/null +++ b/decode-ways/yeonguchoe.go @@ -0,0 +1,32 @@ +func dfs(s string, i int, memo map[int]int) int { + if i == len(s) { + return 1 + } + if val, found := memo[i]; found { + return val + } + if s[i] == '0' { + memo[i] = 0 + return 0 + } + + // 1자리 숫자로 디코딩 + result := dfs(s, i+1, memo) + + // 2자리 숫자로 디코딩 + if i+1 < len(s) { + // 10의 자리 숫자 또는 20의 자리 숫자로 디코딩 + if (s[i] == '1') || (s[i] == '2' && s[i+1] >= '1' && s[i+1] <= '6') { + result += dfs(s, i+2, memo) + } + } + // dp에 저장 + memo[i] = result + return result +} + +func numDecodings(s string) int { + // memo 테이블 만들고 dfs 호출 + memo := make(map[int]int) + return dfs(s, 0, memo) +} diff --git a/encode-and-decode-strings/yeonguchoe.go b/encode-and-decode-strings/yeonguchoe.go new file mode 100644 index 000000000..14605886c --- /dev/null +++ b/encode-and-decode-strings/yeonguchoe.go @@ -0,0 +1,33 @@ +type Codec struct { +} + +// Encodes a list of strings to a single string. +func (codec *Codec) Encode(strs []string) string { + var temp []rune + for _, v := range strs { + var current_string = []rune(v) + temp = append(temp, rune(len(current_string))) + temp = append(temp, rune('π')) + temp = append(temp, current_string...) + } + return string(temp) +} + +// Decodes a single string to a list of strings. +func (codec *Codec) Decode(strs string) []string { + var full_string = []rune(strs) + var result []string + + var index = 0 + + for index < len(full_string) { + var word_start int = int(index) + for full_string[word_start] != rune('π') { + word_start += 1 + } + length := int(full_string[word_start-1]) + result = append(result, string(full_string[word_start+1:word_start+1+length])) + index = word_start + 1 + length + } + return result +} diff --git a/valid-anagram/yeonguchoe.go b/valid-anagram/yeonguchoe.go new file mode 100644 index 000000000..9f390cc71 --- /dev/null +++ b/valid-anagram/yeonguchoe.go @@ -0,0 +1,27 @@ +func isAnagram(s string, t string) bool { + + var s_frequency map[rune]int = make(map[rune]int) + var t_frequency map[rune]int = make(map[rune]int) + + for i := 0; i < len(s); i++ { + s_frequency[rune(s[i])] += 1 + } + + for index, key := range t { + t_frequency[key] += 1 + } + + for key, value := range s_frequency { + if v, key_exist := t_frequency[key]; !key_exist || v != value { + return false + } + } + + for key, value := range t_frequency { + if v, key_exist := s_frequency[key]; !key_exist || v != value { + return false + } + } + + return true +}