diff --git a/solution/1300-1399/1318.Minimum Flips to Make a OR b Equal to c/README.md b/solution/1300-1399/1318.Minimum Flips to Make a OR b Equal to c/README.md
index 7350d45a2edf4..a3d82732885a0 100644
--- a/solution/1300-1399/1318.Minimum Flips to Make a OR b Equal to c/README.md
+++ b/solution/1300-1399/1318.Minimum Flips to Make a OR b Equal to c/README.md
@@ -52,7 +52,7 @@
我们可以枚举 $a$, $b$, $c$ 的二进制表示的每一位,分别记为 $x$, $y$, $z$。如果 $x$ 和 $y$ 的按位或运算结果与 $z$ 不同,此时我们判断 $x$ 和 $y$ 是否都是 $1$,如果是,则需要翻转两次,否则只需要翻转一次。我们将所有需要翻转的次数累加即可。
-时间复杂度 $O(\log M)$,空间复杂度 $O(1)$。其中 $M$ 是题目中数字的最大值。
+时间复杂度 $O(\log M)$,其中 $M$ 是题目中数字的最大值。空间复杂度 $O(1)$。
@@ -60,10 +60,9 @@
class Solution:
def minFlips(self, a: int, b: int, c: int) -> int:
ans = 0
- for i in range(30):
+ for i in range(32):
x, y, z = a >> i & 1, b >> i & 1, c >> i & 1
- if x | y != z:
- ans += 2 if x == 1 and y == 1 else 1
+ ans += x + y if z == 0 else int(x == 0 and y == 0)
return ans
```
@@ -71,11 +70,9 @@ class Solution:
class Solution {
public int minFlips(int a, int b, int c) {
int ans = 0;
- for (int i = 0; i < 30; ++i) {
+ for (int i = 0; i < 32; ++i) {
int x = a >> i & 1, y = b >> i & 1, z = c >> i & 1;
- if ((x | y) != z) {
- ans += x == 1 && y == 1 ? 2 : 1;
- }
+ ans += z == 0 ? x + y : (x == 0 && y == 0 ? 1 : 0);
}
return ans;
}
@@ -87,11 +84,9 @@ class Solution {
public:
int minFlips(int a, int b, int c) {
int ans = 0;
- for (int i = 0; i < 30; ++i) {
+ for (int i = 0; i < 32; ++i) {
int x = a >> i & 1, y = b >> i & 1, z = c >> i & 1;
- if ((x | y) != z) {
- ans += x == 1 && y == 1 ? 2 : 1;
- }
+ ans += z == 0 ? x + y : (x == 0 && y == 0 ? 1 : 0);
}
return ans;
}
@@ -100,20 +95,29 @@ public:
```go
func minFlips(a int, b int, c int) (ans int) {
- for i := 0; i < 30; i++ {
+ for i := 0; i < 32; i++ {
x, y, z := a>>i&1, b>>i&1, c>>i&1
- if (x | y) != z {
- if x == 1 && y == 1 {
- ans += 2
- } else {
- ans++
- }
+ if z == 0 {
+ ans += x + y
+ } else if x == 0 && y == 0 {
+ ans++
}
}
return
}
```
+```ts
+function minFlips(a: number, b: number, c: number): number {
+ let ans = 0;
+ for (let i = 0; i < 32; ++i) {
+ const [x, y, z] = [(a >> i) & 1, (b >> i) & 1, (c >> i) & 1];
+ ans += z === 0 ? x + y : x + y === 0 ? 1 : 0;
+ }
+ return ans;
+}
+```
+
diff --git a/solution/1300-1399/1318.Minimum Flips to Make a OR b Equal to c/README_EN.md b/solution/1300-1399/1318.Minimum Flips to Make a OR b Equal to c/README_EN.md
index 4911252081cb8..e1d1272099318 100644
--- a/solution/1300-1399/1318.Minimum Flips to Make a OR b Equal to c/README_EN.md
+++ b/solution/1300-1399/1318.Minimum Flips to Make a OR b Equal to c/README_EN.md
@@ -60,7 +60,11 @@ Flip operation consists of change any single bit
## Solutions
-### Solution 1
+### Solution 1: Bit Manipulation
+
+We can enumerate each bit of the binary representation of $a$, $b$, and $c$, denoted as $x$, $y$, and $z$ respectively. If the bitwise OR operation result of $x$ and $y$ is different from $z$, we then check if both $x$ and $y$ are $1$. If so, we need to flip twice, otherwise, we only need to flip once. We accumulate all the required flip times.
+
+The time complexity is $O(\log M)$, where $M$ is the maximum value of the numbers in the problem. The space complexity is $O(1)$.
@@ -68,10 +72,9 @@ Flip operation consists of change any single bit
class Solution:
def minFlips(self, a: int, b: int, c: int) -> int:
ans = 0
- for i in range(30):
+ for i in range(32):
x, y, z = a >> i & 1, b >> i & 1, c >> i & 1
- if x | y != z:
- ans += 2 if x == 1 and y == 1 else 1
+ ans += x + y if z == 0 else int(x == 0 and y == 0)
return ans
```
@@ -79,11 +82,9 @@ class Solution:
class Solution {
public int minFlips(int a, int b, int c) {
int ans = 0;
- for (int i = 0; i < 30; ++i) {
+ for (int i = 0; i < 32; ++i) {
int x = a >> i & 1, y = b >> i & 1, z = c >> i & 1;
- if ((x | y) != z) {
- ans += x == 1 && y == 1 ? 2 : 1;
- }
+ ans += z == 0 ? x + y : (x == 0 && y == 0 ? 1 : 0);
}
return ans;
}
@@ -95,11 +96,9 @@ class Solution {
public:
int minFlips(int a, int b, int c) {
int ans = 0;
- for (int i = 0; i < 30; ++i) {
+ for (int i = 0; i < 32; ++i) {
int x = a >> i & 1, y = b >> i & 1, z = c >> i & 1;
- if ((x | y) != z) {
- ans += x == 1 && y == 1 ? 2 : 1;
- }
+ ans += z == 0 ? x + y : (x == 0 && y == 0 ? 1 : 0);
}
return ans;
}
@@ -108,20 +107,29 @@ public:
```go
func minFlips(a int, b int, c int) (ans int) {
- for i := 0; i < 30; i++ {
+ for i := 0; i < 32; i++ {
x, y, z := a>>i&1, b>>i&1, c>>i&1
- if (x | y) != z {
- if x == 1 && y == 1 {
- ans += 2
- } else {
- ans++
- }
+ if z == 0 {
+ ans += x + y
+ } else if x == 0 && y == 0 {
+ ans++
}
}
return
}
```
+```ts
+function minFlips(a: number, b: number, c: number): number {
+ let ans = 0;
+ for (let i = 0; i < 32; ++i) {
+ const [x, y, z] = [(a >> i) & 1, (b >> i) & 1, (c >> i) & 1];
+ ans += z === 0 ? x + y : x + y === 0 ? 1 : 0;
+ }
+ return ans;
+}
+```
+
diff --git a/solution/1300-1399/1318.Minimum Flips to Make a OR b Equal to c/Solution.cpp b/solution/1300-1399/1318.Minimum Flips to Make a OR b Equal to c/Solution.cpp
index 80219fbbdcca3..6ecb5c0d55e21 100644
--- a/solution/1300-1399/1318.Minimum Flips to Make a OR b Equal to c/Solution.cpp
+++ b/solution/1300-1399/1318.Minimum Flips to Make a OR b Equal to c/Solution.cpp
@@ -2,11 +2,9 @@ class Solution {
public:
int minFlips(int a, int b, int c) {
int ans = 0;
- for (int i = 0; i < 30; ++i) {
+ for (int i = 0; i < 32; ++i) {
int x = a >> i & 1, y = b >> i & 1, z = c >> i & 1;
- if ((x | y) != z) {
- ans += x == 1 && y == 1 ? 2 : 1;
- }
+ ans += z == 0 ? x + y : (x == 0 && y == 0 ? 1 : 0);
}
return ans;
}
diff --git a/solution/1300-1399/1318.Minimum Flips to Make a OR b Equal to c/Solution.go b/solution/1300-1399/1318.Minimum Flips to Make a OR b Equal to c/Solution.go
index 174ec0ee427bd..fd1434825c1d8 100644
--- a/solution/1300-1399/1318.Minimum Flips to Make a OR b Equal to c/Solution.go
+++ b/solution/1300-1399/1318.Minimum Flips to Make a OR b Equal to c/Solution.go
@@ -1,12 +1,10 @@
func minFlips(a int, b int, c int) (ans int) {
- for i := 0; i < 30; i++ {
+ for i := 0; i < 32; i++ {
x, y, z := a>>i&1, b>>i&1, c>>i&1
- if (x | y) != z {
- if x == 1 && y == 1 {
- ans += 2
- } else {
- ans++
- }
+ if z == 0 {
+ ans += x + y
+ } else if x == 0 && y == 0 {
+ ans++
}
}
return
diff --git a/solution/1300-1399/1318.Minimum Flips to Make a OR b Equal to c/Solution.java b/solution/1300-1399/1318.Minimum Flips to Make a OR b Equal to c/Solution.java
index 9c0c502381d8c..9b6569178c651 100644
--- a/solution/1300-1399/1318.Minimum Flips to Make a OR b Equal to c/Solution.java
+++ b/solution/1300-1399/1318.Minimum Flips to Make a OR b Equal to c/Solution.java
@@ -1,11 +1,9 @@
class Solution {
public int minFlips(int a, int b, int c) {
int ans = 0;
- for (int i = 0; i < 30; ++i) {
+ for (int i = 0; i < 32; ++i) {
int x = a >> i & 1, y = b >> i & 1, z = c >> i & 1;
- if ((x | y) != z) {
- ans += x == 1 && y == 1 ? 2 : 1;
- }
+ ans += z == 0 ? x + y : (x == 0 && y == 0 ? 1 : 0);
}
return ans;
}
diff --git a/solution/1300-1399/1318.Minimum Flips to Make a OR b Equal to c/Solution.py b/solution/1300-1399/1318.Minimum Flips to Make a OR b Equal to c/Solution.py
index f35bc9038fbd5..adcc114f80459 100644
--- a/solution/1300-1399/1318.Minimum Flips to Make a OR b Equal to c/Solution.py
+++ b/solution/1300-1399/1318.Minimum Flips to Make a OR b Equal to c/Solution.py
@@ -1,8 +1,7 @@
class Solution:
def minFlips(self, a: int, b: int, c: int) -> int:
ans = 0
- for i in range(30):
+ for i in range(32):
x, y, z = a >> i & 1, b >> i & 1, c >> i & 1
- if x | y != z:
- ans += 2 if x == 1 and y == 1 else 1
+ ans += x + y if z == 0 else int(x == 0 and y == 0)
return ans
diff --git a/solution/1300-1399/1318.Minimum Flips to Make a OR b Equal to c/Solution.ts b/solution/1300-1399/1318.Minimum Flips to Make a OR b Equal to c/Solution.ts
new file mode 100644
index 0000000000000..bcb3c62baccaa
--- /dev/null
+++ b/solution/1300-1399/1318.Minimum Flips to Make a OR b Equal to c/Solution.ts
@@ -0,0 +1,8 @@
+function minFlips(a: number, b: number, c: number): number {
+ let ans = 0;
+ for (let i = 0; i < 32; ++i) {
+ const [x, y, z] = [(a >> i) & 1, (b >> i) & 1, (c >> i) & 1];
+ ans += z === 0 ? x + y : x + y === 0 ? 1 : 0;
+ }
+ return ans;
+}