You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solution/1000-1099/1009.Complement of Base 10 Integer/README_EN.md
+65-42
Original file line number
Diff line number
Diff line change
@@ -67,7 +67,15 @@ tags:
67
67
68
68
<!-- solution:start -->
69
69
70
-
### Solution 1
70
+
### Solution 1: Bit Manipulation
71
+
72
+
First, we check if $n$ is $0$. If it is, we return $1$.
73
+
74
+
Next, we define two variables $\textit{ans}$ and $i$, both initialized to $0$. Then we iterate through $n$. In each iteration, we set the $i$-th bit of $\textit{ans}$ to the inverse of the $i$-th bit of $n$, increment $i$ by $1$, and right shift $n$ by $1$.
75
+
76
+
Finally, we return $\textit{ans}$.
77
+
78
+
The time complexity is $O(\log n)$, where $n$ is the given decimal number. The space complexity is $O(1)$.
0 commit comments