diff --git a/0190.Reverse-Bits/memo.md b/0190.Reverse-Bits/memo.md new file mode 100644 index 0000000..69602a0 --- /dev/null +++ b/0190.Reverse-Bits/memo.md @@ -0,0 +1,52 @@ +# 190. Reverse Bits + +## step1 +strに直してreverseする。f-stringのフォーマットが分からず、調べた。 + +https://docs.python.org/ja/3/tutorial/inputoutput.html#formatted-string-literals +https://docs.python.org/ja/3/library/string.html#formatspec + +bが2進数、x (Xで大文字)で16進数、#を付けるとプレフィックスが付く + +0(桁数)で0埋め。他を書くときには><^などを書く + +```python +>>> num = 42 +>>> print(f"{num:b}") +101010 +>>> print(f"{num:x}") +2a +>>> print(f"{num:#x}") +0x2a +>>> print(f"{num:#X}") +0X2A +>>> print(f"{num:03b}") +101010 +>>> print(f"{num:03x}") +02a +>>> print(f"{num:-3x}") + 2a +>>> print(f"{num:,3x}") +Traceback (most recent call last): + File "", line 1, in +ValueError: Invalid format specifier ',3x' for object of type 'int' +>>> print(f"{num:,>3x}") +,2a +>>> print(f"{num:,^6x}") +,,2a,, +``` + +bit演算でも書く。 + +## step2 +https://github.com/Kitaken0107/GrindEasy/pull/23 + +> 文字列に変換しないでやってみてください。ビット操作の問題だと思います。 + +> 32 bit 整数って、シフト演算をして、最下位ビットを見ると、スタックみたいなものじゃないですか。 + +--- +以下を追加 +- builtin (c++) +- divide and conquer +- 配列を前計算しておく diff --git a/0190.Reverse-Bits/step1.py b/0190.Reverse-Bits/step1.py new file mode 100644 index 0000000..98df0c1 --- /dev/null +++ b/0190.Reverse-Bits/step1.py @@ -0,0 +1,5 @@ +class Solution: + def reverseBits(self, n: int) -> int: + bit_expression = f"{n:032b}" + return int(bit_expression[::-1], 2) + diff --git a/0190.Reverse-Bits/step1_bit.py b/0190.Reverse-Bits/step1_bit.py new file mode 100644 index 0000000..f87d6cd --- /dev/null +++ b/0190.Reverse-Bits/step1_bit.py @@ -0,0 +1,12 @@ +class Solution: + def reverseBits(self, n: int) -> int: + reversed_bits = 0 + num_bits = 0 + while num_bits < 32: + reversed_bits <<= 1 + reversed_bits |= n & 1 + n >>= 1 + num_bits += 1 + return reversed_bits + + diff --git a/0190.Reverse-Bits/step2_builtin.cpp b/0190.Reverse-Bits/step2_builtin.cpp new file mode 100644 index 0000000..a0adb35 --- /dev/null +++ b/0190.Reverse-Bits/step2_builtin.cpp @@ -0,0 +1,8 @@ +#include + +class Solution { +public: + uint32_t reverseBits(uint32_t n) { + return __builtin_bitreverse32(n); + } +}; diff --git a/0190.Reverse-Bits/step2_divide_and_conquer.py b/0190.Reverse-Bits/step2_divide_and_conquer.py new file mode 100644 index 0000000..8f69a76 --- /dev/null +++ b/0190.Reverse-Bits/step2_divide_and_conquer.py @@ -0,0 +1,10 @@ +class Solution: + def reverseBits(self, n: int) -> int: + n = n & 0xffffffff + n = ((n & 0x55555555) << 1) | ((n & 0xaaaaaaaa) >> 1) + n = ((n & 0x33333333) << 2) | ((n & 0xcccccccc) >> 2) + n = ((n & 0x0f0f0f0f) << 4) | ((n & 0xf0f0f0f0) >> 4) + n = ((n & 0x00ff00ff) << 8) | ((n & 0xff00ff00) >> 8) + n = ((n & 0x0000ffff) << 16) | ((n & 0xffff0000) >> 16) + + return n & 0xffffffff diff --git a/0190.Reverse-Bits/step2_memory.py b/0190.Reverse-Bits/step2_memory.py new file mode 100644 index 0000000..ee58750 --- /dev/null +++ b/0190.Reverse-Bits/step2_memory.py @@ -0,0 +1,21 @@ +class Solution: + REVERSE_TABLE = [0] * 256 + + @classmethod + def init_table(cls): + for i in range(1, 256): + cls.REVERSE_TABLE[i] = (cls.REVERSE_TABLE[i >> 1] >> 1) | ((i & 1) << 7) + + def __init__(self): + if self.REVERSE_TABLE[1] == 0: + self.init_table() + + def reverseBits(self, n: int) -> int: + n = n & 0xffffffff + + byte0 = self.REVERSE_TABLE[n & 0xff] + byte1 = self.REVERSE_TABLE[(n >> 8) & 0xff] + byte2 = self.REVERSE_TABLE[(n >> 16) & 0xff] + byte3 = self.REVERSE_TABLE[(n >> 24) & 0xff] + + return (byte0 << 24) | (byte1 << 16) | (byte2 << 8) | byte3 \ No newline at end of file