From 531b9704510521c2a1815a693ce5b0cf837aa93a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nguy=E1=BB=85n=20=C4=90=C3=ACnh=20Qu=C3=BD?= <127675330+dinhquy-nguyen-1704@users.noreply.github.com> Date: Fri, 16 Feb 2024 11:55:36 +0700 Subject: [PATCH] Time: 41 ms (26.41%), Space: 16.5 MB (65.91%) - LeetHub --- .../0476-number-complement.py | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 0476-number-complement/0476-number-complement.py diff --git a/0476-number-complement/0476-number-complement.py b/0476-number-complement/0476-number-complement.py new file mode 100644 index 0000000..81ab2e2 --- /dev/null +++ b/0476-number-complement/0476-number-complement.py @@ -0,0 +1,22 @@ +class Solution: + def findComplement(self, num: int) -> int: + st = "" + while num != 0: + st += str(num%2) + num = num//2 + + st2 = "" + for i in st: + if i == "0": + st2 += "1" + else: + st2 += "0" + + dec = 0 + for j in range(len(st2)): + if st2[j] == "1": + dec += 2**(j) + + return dec + +