Skip to content

feat: add solutions to lc problem: No.1009 #3843

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 65 additions & 42 deletions solution/1000-1099/1009.Complement of Base 10 Integer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,15 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:位运算

我们首先判断 $n$ 是否为 $0$,如果是,则返回 $1$。

接着我们定义两个变量 $\textit{ans}$ 和 $i$,初始化为 $0$。然后我们遍历 $n$,在每次遍历中,我们将 $\textit{ans}$ 的第 $i$ 位设置为 $n$ 的第 $i$ 位取反,然后将 $i$ 加 $1$,并且$n$ 右移 $1$ 位。

最后返回 $\textit{ans}$ 即可。

时间复杂度 $O(\log n)$,其中 $n$ 为给定的十进制数。空间复杂度 $O(1)$。

<!-- tabs:start -->

Expand All @@ -76,15 +84,11 @@ class Solution:
def bitwiseComplement(self, n: int) -> int:
if n == 0:
return 1
ans = 0
find = False
for i in range(30, -1, -1):
b = n & (1 << i)
if not find and b == 0:
continue
find = True
if b == 0:
ans |= 1 << i
ans = i = 0
while n:
ans |= ((n & 1 ^ 1) << i)
i += 1
n >>= 1
return ans
```

Expand All @@ -96,17 +100,10 @@ class Solution {
if (n == 0) {
return 1;
}
int ans = 0;
boolean find = false;
for (int i = 30; i >= 0; --i) {
int b = n & (1 << i);
if (!find && b == 0) {
continue;
}
find = true;
if (b == 0) {
ans |= (1 << i);
}
int ans = 0, i = 0;
while (n != 0) {
ans |= (n & 1 ^ 1) << (i++);
n >>= 1;
}
return ans;
}
Expand All @@ -119,14 +116,13 @@ class Solution {
class Solution {
public:
int bitwiseComplement(int n) {
if (n == 0) return 1;
int ans = 0;
bool find = false;
for (int i = 30; i >= 0; --i) {
int b = n & (1 << i);
if (!find && b == 0) continue;
find = true;
if (b == 0) ans |= (1 << i);
if (n == 0) {
return 1;
}
int ans = 0, i = 0;
while (n != 0) {
ans |= (n & 1 ^ 1) << (i++);
n >>= 1;
}
return ans;
}
Expand All @@ -136,23 +132,50 @@ public:
#### Go

```go
func bitwiseComplement(n int) int {
func bitwiseComplement(n int) (ans int) {
if n == 0 {
return 1
}
ans := 0
find := false
for i := 30; i >= 0; i-- {
b := n & (1 << i)
if !find && b == 0 {
continue
}
find = true
if b == 0 {
ans |= (1 << i)
}
for i := 0; n != 0; n >>= 1 {
ans |= (n&1 ^ 1) << i
i++
}
return ans
return
}
```

#### TypeScript

```ts
function bitwiseComplement(n: number): number {
if (n === 0) {
return 1;
}
let ans = 0;
for (let i = 0; n; n >>= 1) {
ans |= ((n & 1) ^ 1) << i++;
}
return ans;
}
```

#### Rust

```rust
impl Solution {
pub fn bitwise_complement(mut n: i32) -> i32 {
if n == 0 {
return 1;
}
let mut ans = 0;
let mut i = 0;
while n != 0 {
ans |= ((n & 1) ^ 1) << i;
n >>= 1;
i += 1;
}
ans
}
}
```

Expand Down
107 changes: 65 additions & 42 deletions solution/1000-1099/1009.Complement of Base 10 Integer/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,15 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Bit Manipulation

First, we check if $n$ is $0$. If it is, we return $1$.

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$.

Finally, we return $\textit{ans}$.

The time complexity is $O(\log n)$, where $n$ is the given decimal number. The space complexity is $O(1)$.

<!-- tabs:start -->

Expand All @@ -78,15 +86,11 @@ class Solution:
def bitwiseComplement(self, n: int) -> int:
if n == 0:
return 1
ans = 0
find = False
for i in range(30, -1, -1):
b = n & (1 << i)
if not find and b == 0:
continue
find = True
if b == 0:
ans |= 1 << i
ans = i = 0
while n:
ans |= ((n & 1 ^ 1) << i)
i += 1
n >>= 1
return ans
```

Expand All @@ -98,17 +102,10 @@ class Solution {
if (n == 0) {
return 1;
}
int ans = 0;
boolean find = false;
for (int i = 30; i >= 0; --i) {
int b = n & (1 << i);
if (!find && b == 0) {
continue;
}
find = true;
if (b == 0) {
ans |= (1 << i);
}
int ans = 0, i = 0;
while (n != 0) {
ans |= (n & 1 ^ 1) << (i++);
n >>= 1;
}
return ans;
}
Expand All @@ -121,14 +118,13 @@ class Solution {
class Solution {
public:
int bitwiseComplement(int n) {
if (n == 0) return 1;
int ans = 0;
bool find = false;
for (int i = 30; i >= 0; --i) {
int b = n & (1 << i);
if (!find && b == 0) continue;
find = true;
if (b == 0) ans |= (1 << i);
if (n == 0) {
return 1;
}
int ans = 0, i = 0;
while (n != 0) {
ans |= (n & 1 ^ 1) << (i++);
n >>= 1;
}
return ans;
}
Expand All @@ -138,23 +134,50 @@ public:
#### Go

```go
func bitwiseComplement(n int) int {
func bitwiseComplement(n int) (ans int) {
if n == 0 {
return 1
}
ans := 0
find := false
for i := 30; i >= 0; i-- {
b := n & (1 << i)
if !find && b == 0 {
continue
}
find = true
if b == 0 {
ans |= (1 << i)
}
for i := 0; n != 0; n >>= 1 {
ans |= (n&1 ^ 1) << i
i++
}
return ans
return
}
```

#### TypeScript

```ts
function bitwiseComplement(n: number): number {
if (n === 0) {
return 1;
}
let ans = 0;
for (let i = 0; n; n >>= 1) {
ans |= ((n & 1) ^ 1) << i++;
}
return ans;
}
```

#### Rust

```rust
impl Solution {
pub fn bitwise_complement(mut n: i32) -> i32 {
if n == 0 {
return 1;
}
let mut ans = 0;
let mut i = 0;
while n != 0 {
ans |= ((n & 1) ^ 1) << i;
n >>= 1;
i += 1;
}
ans
}
}
```

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
class Solution {
public:
int bitwiseComplement(int n) {
if (n == 0) return 1;
int ans = 0;
bool find = false;
for (int i = 30; i >= 0; --i) {
int b = n & (1 << i);
if (!find && b == 0) continue;
find = true;
if (b == 0) ans |= (1 << i);
if (n == 0) {
return 1;
}
int ans = 0, i = 0;
while (n != 0) {
ans |= (n & 1 ^ 1) << (i++);
n >>= 1;
}
return ans;
}
};
};
20 changes: 6 additions & 14 deletions solution/1000-1099/1009.Complement of Base 10 Integer/Solution.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
func bitwiseComplement(n int) int {
func bitwiseComplement(n int) (ans int) {
if n == 0 {
return 1
}
ans := 0
find := false
for i := 30; i >= 0; i-- {
b := n & (1 << i)
if !find && b == 0 {
continue
}
find = true
if b == 0 {
ans |= (1 << i)
}
for i := 0; n != 0; n >>= 1 {
ans |= (n&1 ^ 1) << i
i++
}
return ans
}
return
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,11 @@ public int bitwiseComplement(int n) {
if (n == 0) {
return 1;
}
int ans = 0;
boolean find = false;
for (int i = 30; i >= 0; --i) {
int b = n & (1 << i);
if (!find && b == 0) {
continue;
}
find = true;
if (b == 0) {
ans |= (1 << i);
}
int ans = 0, i = 0;
while (n != 0) {
ans |= (n & 1 ^ 1) << (i++);
n >>= 1;
}
return ans;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@ class Solution:
def bitwiseComplement(self, n: int) -> int:
if n == 0:
return 1
ans = 0
find = False
for i in range(30, -1, -1):
b = n & (1 << i)
if not find and b == 0:
continue
find = True
if b == 0:
ans |= 1 << i
ans = i = 0
while n:
ans |= (n & 1 ^ 1) << i
i += 1
n >>= 1
return ans
Loading
Loading