From 5a1ee896d691f9fad6220f3634520abf8937b017 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EC=9D=80=EB=B9=88=20Eunbeen=20Kang?= <145640625+river20s@users.noreply.github.com> Date: Thu, 26 Dec 2024 17:59:32 +0900 Subject: [PATCH 1/2] solution two sum --- two-sum/river20s.java | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 two-sum/river20s.java diff --git a/two-sum/river20s.java b/two-sum/river20s.java new file mode 100644 index 000000000..621660e25 --- /dev/null +++ b/two-sum/river20s.java @@ -0,0 +1,24 @@ +/* + * T.C: O(n) -> 배열 nums를 한 번 순회 + * S.C: O(n) → 최대 n개의 요소가 저장됨 + */ +import java.util.HashMap; + +class Solution { + public int[] twoSum(int[] nums, int target) { + + HashMap map = new HashMap<>(); + for (int i = 0; i < nums.length; i++) { + int k = target - nums[i]; + + if (map.containsKey(k)) { + return new int[] { map.get(k), i }; + } + + map.put(nums[i], i); + } + throw new IllegalArgumentException("exception handling for java compilation"); + + } +} + From f4a56839f4e2bcd92a3e85aaede49f84e176cc33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B0=95=EC=9D=80=EB=B9=88=20Eunbeen=20Kang?= <145640625+river20s@users.noreply.github.com> Date: Fri, 27 Dec 2024 18:25:32 +0900 Subject: [PATCH 2/2] solution reverse bits --- reverse-bits/river20s.java | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 reverse-bits/river20s.java diff --git a/reverse-bits/river20s.java b/reverse-bits/river20s.java new file mode 100644 index 000000000..76a976fe9 --- /dev/null +++ b/reverse-bits/river20s.java @@ -0,0 +1,22 @@ +public class Solution { + /* T.C = O(1) + * S.C = O(1) + */ + public int reverseBits(int n) { + // Set the output to 0 + int output = 0; + // Repeat 32 times + for (int i = 0; i < 32; i++) { + // Shift the output value one space to the left to make room for the new bit + output <<= 1; + // '&' operation to get the rightmost bit and add it to the output + output = output | (n & 1); + // Discard the rightmost bit of the 'n' + n = n >> 1; + } + + return output; + + } +} +