Skip to content

feat: add solutions to lc problem: No.2220 #3977

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
Jan 21, 2025
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
118 changes: 46 additions & 72 deletions solution/2200-2299/2220.Minimum Bit Flips to Convert Number/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,11 @@ tags:

<!-- solution:start -->

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

根据题目描述,我们只需要计算 $\textit{start} \oplus \textit{goal}$ 的二进制表示中有多少个 1 即可。

时间复杂度 $O(\log n)$,其中 $n$ 是题目中整数的大小。空间复杂度 $O(1)$。

<!-- tabs:start -->

Expand All @@ -78,26 +82,15 @@ tags:
```python
class Solution:
def minBitFlips(self, start: int, goal: int) -> int:
t = start ^ goal
ans = 0
while t:
ans += t & 1
t >>= 1
return ans
return (start ^ goal).bit_count()
```

#### Java

```java
class Solution {
public int minBitFlips(int start, int goal) {
int t = start ^ goal;
int ans = 0;
while (t != 0) {
ans += t & 1;
t >>= 1;
}
return ans;
return Integer.bitCount(start ^ goal);
}
}
```
Expand All @@ -108,13 +101,7 @@ class Solution {
class Solution {
public:
int minBitFlips(int start, int goal) {
int t = start ^ goal;
int ans = 0;
while (t) {
ans += t & 1;
t >>= 1;
}
return ans;
return __builtin_popcount(start ^ goal);
}
};
```
Expand All @@ -123,27 +110,24 @@ public:

```go
func minBitFlips(start int, goal int) int {
t := start ^ goal
ans := 0
for t != 0 {
ans += t & 1
t >>= 1
}
return ans
return bits.OnesCount(uint(start ^ goal))
}
```

#### TypeScript

```ts
function minBitFlips(start: number, goal: number): number {
let tmp = start ^ goal;
let ans = 0;
while (tmp !== 0) {
ans += tmp & 1;
tmp >>= 1;
}
return ans;
return bitCount(start ^ goal);
}

function bitCount(i: number): number {
i = i - ((i >>> 1) & 0x55555555);
i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
i = (i + (i >>> 4)) & 0x0f0f0f0f;
i = i + (i >>> 8);
i = i + (i >>> 16);
return i & 0x3f;
}
```

Expand All @@ -152,26 +136,42 @@ function minBitFlips(start: number, goal: number): number {
```rust
impl Solution {
pub fn min_bit_flips(start: i32, goal: i32) -> i32 {
let mut tmp = start ^ goal;
let mut ans = 0;
while tmp != 0 {
ans += tmp & 1;
tmp >>= 1;
}
ans
(start ^ goal).count_ones() as i32
}
}
```

#### JavaScript

```js
/**
* @param {number} start
* @param {number} goal
* @return {number}
*/
var minBitFlips = function (start, goal) {
return bitCount(start ^ goal);
};

function bitCount(i) {
i = i - ((i >>> 1) & 0x55555555);
i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
i = (i + (i >>> 4)) & 0x0f0f0f0f;
i = i + (i >>> 8);
i = i + (i >>> 16);
return i & 0x3f;
}
```

#### C

```c
int minBitFlips(int start, int goal) {
int tmp = start ^ goal;
int x = start ^ goal;
int ans = 0;
while (tmp) {
ans += tmp & 1;
tmp >>= 1;
while (x) {
ans += (x & 1);
x >>= 1;
}
return ans;
}
Expand All @@ -181,30 +181,4 @@ int minBitFlips(int start, int goal) {

<!-- solution:end -->

<!-- solution:start -->

### Solution 2

<!-- tabs:start -->

#### TypeScript

```ts
function minBitFlips(start: number, goal: number): number {
return (start ^ goal).toString(2).replace(/0/g, '').length;
}
```

#### JavaScript

```js
function minBitFlips(start, goal) {
return (start ^ goal).toString(2).replace(/0/g, '').length;
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ It can be shown we cannot convert 3 to 4 in less than 3 steps. Hence, we return

<!-- solution:start -->

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

According to the problem description, we only need to count the number of 1s in the binary representation of $\textit{start} \oplus \textit{goal}$.

The time complexity is $O(\log n)$, where $n$ is the size of the integers in the problem. The space complexity is $O(1)$.

<!-- tabs:start -->

Expand All @@ -75,26 +79,15 @@ It can be shown we cannot convert 3 to 4 in less than 3 steps. Hence, we return
```python
class Solution:
def minBitFlips(self, start: int, goal: int) -> int:
t = start ^ goal
ans = 0
while t:
ans += t & 1
t >>= 1
return ans
return (start ^ goal).bit_count()
```

#### Java

```java
class Solution {
public int minBitFlips(int start, int goal) {
int t = start ^ goal;
int ans = 0;
while (t != 0) {
ans += t & 1;
t >>= 1;
}
return ans;
return Integer.bitCount(start ^ goal);
}
}
```
Expand All @@ -105,13 +98,7 @@ class Solution {
class Solution {
public:
int minBitFlips(int start, int goal) {
int t = start ^ goal;
int ans = 0;
while (t) {
ans += t & 1;
t >>= 1;
}
return ans;
return __builtin_popcount(start ^ goal);
}
};
```
Expand All @@ -120,27 +107,24 @@ public:

```go
func minBitFlips(start int, goal int) int {
t := start ^ goal
ans := 0
for t != 0 {
ans += t & 1
t >>= 1
}
return ans
return bits.OnesCount(uint(start ^ goal))
}
```

#### TypeScript

```ts
function minBitFlips(start: number, goal: number): number {
let tmp = start ^ goal;
let ans = 0;
while (tmp !== 0) {
ans += tmp & 1;
tmp >>= 1;
}
return ans;
return bitCount(start ^ goal);
}

function bitCount(i: number): number {
i = i - ((i >>> 1) & 0x55555555);
i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
i = (i + (i >>> 4)) & 0x0f0f0f0f;
i = i + (i >>> 8);
i = i + (i >>> 16);
return i & 0x3f;
}
```

Expand All @@ -149,26 +133,42 @@ function minBitFlips(start: number, goal: number): number {
```rust
impl Solution {
pub fn min_bit_flips(start: i32, goal: i32) -> i32 {
let mut tmp = start ^ goal;
let mut ans = 0;
while tmp != 0 {
ans += tmp & 1;
tmp >>= 1;
}
ans
(start ^ goal).count_ones() as i32
}
}
```

#### JavaScript

```js
/**
* @param {number} start
* @param {number} goal
* @return {number}
*/
var minBitFlips = function (start, goal) {
return bitCount(start ^ goal);
};

function bitCount(i) {
i = i - ((i >>> 1) & 0x55555555);
i = (i & 0x33333333) + ((i >>> 2) & 0x33333333);
i = (i + (i >>> 4)) & 0x0f0f0f0f;
i = i + (i >>> 8);
i = i + (i >>> 16);
return i & 0x3f;
}
```

#### C

```c
int minBitFlips(int start, int goal) {
int tmp = start ^ goal;
int x = start ^ goal;
int ans = 0;
while (tmp) {
ans += tmp & 1;
tmp >>= 1;
while (x) {
ans += (x & 1);
x >>= 1;
}
return ans;
}
Expand All @@ -178,30 +178,4 @@ int minBitFlips(int start, int goal) {

<!-- solution:end -->

<!-- solution:start -->

### Solution 2

<!-- tabs:start -->

#### TypeScript

```ts
function minBitFlips(start: number, goal: number): number {
return (start ^ goal).toString(2).replace(/0/g, '').length;
}
```

#### JavaScript

```js
function minBitFlips(start, goal) {
return (start ^ goal).toString(2).replace(/0/g, '').length;
}
```

<!-- tabs:end -->

<!-- solution:end -->

<!-- problem:end -->
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
int minBitFlips(int start, int goal) {
int tmp = start ^ goal;
int x = start ^ goal;
int ans = 0;
while (tmp) {
ans += tmp & 1;
tmp >>= 1;
while (x) {
ans += (x & 1);
x >>= 1;
}
return ans;
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
class Solution {
public:
int minBitFlips(int start, int goal) {
int t = start ^ goal;
int ans = 0;
while (t) {
ans += t & 1;
t >>= 1;
}
return ans;
return __builtin_popcount(start ^ goal);
}
};
};
Loading
Loading