From 63043b76d232910f936ec6386f55472f9c636678 Mon Sep 17 00:00:00 2001 From: changchanghwang Date: Mon, 23 Dec 2024 12:17:32 +0900 Subject: [PATCH 1/4] feat: twoSum --- two-sum/changchanghwang.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 two-sum/changchanghwang.go diff --git a/two-sum/changchanghwang.go b/two-sum/changchanghwang.go new file mode 100644 index 000000000..7a4abf675 --- /dev/null +++ b/two-sum/changchanghwang.go @@ -0,0 +1,15 @@ +// Time: O(n) +// Space: O(n) +func twoSum(nums []int, target int) []int { + m := make(map[int]int) + + // O(n) + for i, num := range nums { + // O(1) + if j, ok := m[target-num]; ok && j != i { // target = num2 + num1 -> num2 = target - num1 을 이용하여 두 수를 찾는다. + return []int{j, i} + } + m[num] = i // 없다면 현재 수를 키로 하여 인덱스를 저장한다. + } + return nil +} From fb485d94d420a03a6265597bc5b5e8566b86fe1a Mon Sep 17 00:00:00 2001 From: changchanghwang Date: Thu, 26 Dec 2024 17:19:05 +0900 Subject: [PATCH 2/4] feat: reverse bits --- reverse-bits/changchanghwang.go | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 reverse-bits/changchanghwang.go diff --git a/reverse-bits/changchanghwang.go b/reverse-bits/changchanghwang.go new file mode 100644 index 000000000..9f604dae2 --- /dev/null +++ b/reverse-bits/changchanghwang.go @@ -0,0 +1,6 @@ +// Time Complexity: O(1) +// Space Complexity: O(1) +func reverseBits(num uint32) uint32 { + reversedBits := bits.Reverse32(num) + return reversedBits +} From a3dbcb25f4ea7c4f687565929e91f73001265f53 Mon Sep 17 00:00:00 2001 From: changchanghwang Date: Thu, 26 Dec 2024 17:25:13 +0900 Subject: [PATCH 3/4] feat: product except self --- .../changchanghwang.go | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 product-of-array-except-self/changchanghwang.go diff --git a/product-of-array-except-self/changchanghwang.go b/product-of-array-except-self/changchanghwang.go new file mode 100644 index 000000000..5ffcbd92e --- /dev/null +++ b/product-of-array-except-self/changchanghwang.go @@ -0,0 +1,26 @@ +// time complexity: O(n) +// space complexity: O(1) +// prefix와 postfix를 이용하여 계산 +// 예를 들어, [1, 2, 3, 4] 일 때, +// prefix는 [1, 1, 2, 6] 이고 postfix는 [24, 12, 4, 1] 이다. +// 그리고 서로 곱하면 [24, 12, 8, 6] 이 된다. +func productExceptSelf(nums []int) []int { + res := make([]int, len(nums)) + for i := range res { + res[i] = 1 + } + + prefix := 1 + for i := 0; i < len(nums); i++ { + res[i] = prefix + prefix *= nums[i] + } + + postfix := 1 + for i := len(nums) - 1; i >= 0; i-- { + res[i] *= postfix + postfix *= nums[i] + } + + return res +} \ No newline at end of file From 4d19802e27294249d1c0206e69e5814858afbfda Mon Sep 17 00:00:00 2001 From: changchanghwang Date: Thu, 26 Dec 2024 17:46:16 +0900 Subject: [PATCH 4/4] fix: lint --- product-of-array-except-self/changchanghwang.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/product-of-array-except-self/changchanghwang.go b/product-of-array-except-self/changchanghwang.go index 5ffcbd92e..e6aaa51ac 100644 --- a/product-of-array-except-self/changchanghwang.go +++ b/product-of-array-except-self/changchanghwang.go @@ -23,4 +23,4 @@ func productExceptSelf(nums []int) []int { } return res -} \ No newline at end of file +}