From 75b57dc76274c48580d65058548b8f37d20d40a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EC=84=A0=EB=AF=BC?= Date: Fri, 15 Nov 2024 23:27:50 +0900 Subject: [PATCH] Add week 14 solutions: reverse-bits --- reverse-bits/gitsunmin.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 reverse-bits/gitsunmin.ts diff --git a/reverse-bits/gitsunmin.ts b/reverse-bits/gitsunmin.ts new file mode 100644 index 000000000..a218dbde5 --- /dev/null +++ b/reverse-bits/gitsunmin.ts @@ -0,0 +1,15 @@ +/** + * https://leetcode.com/problems/reverse-bits/ + * time complexity : O(1) + * space complexity : O(1) + */ + +function reverseBits(n: number): number { + let result = 0; + for (let i = 0; i < 32; i++) { + result <<= 1; + result |= n & 1; + n >>>= 1; + } + return result >>> 0; +};