Skip to content

Commit 9f3a109

Browse files
authored
feat: add solutions to lc problems: No.3314~3316 (#3662)
* No.3314.Construct the Minimum Bitwise Array I * No.3315.Construct the Minimum Bitwise Array II * No.3316.Find Maximum Removals From Source String
1 parent 9eda464 commit 9f3a109

File tree

21 files changed

+903
-24
lines changed

21 files changed

+903
-24
lines changed

solution/3300-3399/3314.Construct the Minimum Bitwise Array I/README.md

+95-4
Original file line numberDiff line numberDiff line change
@@ -80,32 +80,123 @@ tags:
8080

8181
<!-- solution:start -->
8282

83-
### 方法一
83+
### 方法一:位运算
84+
85+
对于一个整数 $a$,满足 $a \lor (a + 1)$ 的结果一定为奇数,因此,如果 $\text{nums[i]}$ 是偶数,那么 $\text{ans}[i]$ 一定不存在,直接返回 $-1$。本题中 $\textit{nums}[i]$ 是质数,判断是否是偶数,只需要判断是否等于 $2$ 即可。
86+
87+
如果 $\text{nums[i]}$ 是奇数,假设 $\text{nums[i]} = \text{0b1101101}$,由于 $a \lor (a + 1) = \text{nums[i]}$,等价于将 $a$ 的最后一个为 $0$ 的二进制位变为 $1$。那么求解 $a$,就等价于将 $\text{nums[i]}$ 的最后一个 $0$ 的下一位 $1$ 变为 $0$。我们只需要从低位(下标为 $1$)开始遍历,找到第一个为 $0$ 的二进制位,如果是第 $i$ 位,那么我们就将 $\text{nums[i]}$ 的第 $i - 1$ 位变为 $1$,即 $\text{ans}[i] = \text{nums[i]} \oplus 2^{i - 1}$。
88+
89+
遍历所有的 $\text{nums[i]}$,即可得到答案。
90+
91+
时间复杂度 $O(n \times \log M)$,其中 $n$ 和 $M$ 分别是数组 $\text{nums}$ 的长度和数组中的最大值。忽略答案数组的空间消耗,空间复杂度 $O(1)$。
8492

8593
<!-- tabs:start -->
8694

8795
#### Python3
8896

8997
```python
90-
98+
class Solution:
99+
def minBitwiseArray(self, nums: List[int]) -> List[int]:
100+
ans = []
101+
for x in nums:
102+
if x == 2:
103+
ans.append(-1)
104+
else:
105+
for i in range(1, 32):
106+
if x >> i & 1 ^ 1:
107+
ans.append(x ^ 1 << (i - 1))
108+
break
109+
return ans
91110
```
92111

93112
#### Java
94113

95114
```java
96-
115+
class Solution {
116+
public int[] minBitwiseArray(List<Integer> nums) {
117+
int n = nums.size();
118+
int[] ans = new int[n];
119+
for (int i = 0; i < n; ++i) {
120+
int x = nums.get(i);
121+
if (x == 2) {
122+
ans[i] = -1;
123+
} else {
124+
for (int j = 1; j < 32; ++j) {
125+
if ((x >> j & 1) == 0) {
126+
ans[i] = x ^ 1 << (j - 1);
127+
break;
128+
}
129+
}
130+
}
131+
}
132+
return ans;
133+
}
134+
}
97135
```
98136

99137
#### C++
100138

101139
```cpp
102-
140+
class Solution {
141+
public:
142+
vector<int> minBitwiseArray(vector<int>& nums) {
143+
vector<int> ans;
144+
for (int x : nums) {
145+
if (x == 2) {
146+
ans.push_back(-1);
147+
} else {
148+
for (int i = 1; i < 32; ++i) {
149+
if (x >> i & 1 ^ 1) {
150+
ans.push_back(x ^ 1 << (i - 1));
151+
break;
152+
}
153+
}
154+
}
155+
}
156+
return ans;
157+
}
158+
};
103159
```
104160
105161
#### Go
106162
107163
```go
164+
func minBitwiseArray(nums []int) (ans []int) {
165+
for _, x := range nums {
166+
if x == 2 {
167+
ans = append(ans, -1)
168+
} else {
169+
for i := 1; i < 32; i++ {
170+
if x>>i&1 == 0 {
171+
ans = append(ans, x^1<<(i-1))
172+
break
173+
}
174+
}
175+
}
176+
}
177+
return
178+
}
179+
```
108180

181+
#### TypeScript
182+
183+
```ts
184+
function minBitwiseArray(nums: number[]): number[] {
185+
const ans: number[] = [];
186+
for (const x of nums) {
187+
if (x === 2) {
188+
ans.push(-1);
189+
} else {
190+
for (let i = 1; i < 32; ++i) {
191+
if (((x >> i) & 1) ^ 1) {
192+
ans.push(x ^ (1 << (i - 1)));
193+
break;
194+
}
195+
}
196+
}
197+
}
198+
return ans;
199+
}
109200
```
110201

111202
<!-- tabs:end -->

solution/3300-3399/3314.Construct the Minimum Bitwise Array I/README_EN.md

+95-4
Original file line numberDiff line numberDiff line change
@@ -74,32 +74,123 @@ tags:
7474

7575
<!-- solution:start -->
7676

77-
### Solution 1
77+
### Solution 1: Bit Manipulation
78+
79+
For an integer $a$, the result of $a \lor (a + 1)$ is always odd. Therefore, if $\text{nums[i]}$ is even, then $\text{ans}[i]$ does not exist, and we directly return $-1$. In this problem, $\textit{nums}[i]$ is a prime number, so to check if it is even, we only need to check if it equals $2$.
80+
81+
If $\text{nums[i]}$ is odd, suppose $\text{nums[i]} = \text{0b1101101}$. Since $a \lor (a + 1) = \text{nums[i]}$, this is equivalent to changing the last $0$ bit of $a$ to $1$. To solve for $a$, we need to change the bit after the last $0$ in $\text{nums[i]}$ to $0$. We start traversing from the least significant bit (index $1$) and find the first $0$ bit. If it is at position $i$, we change the $(i - 1)$-th bit of $\text{nums[i]}$ to $1$, i.e., $\text{ans}[i] = \text{nums[i]} \oplus 2^{i - 1}$.
82+
83+
By traversing all elements in $\text{nums}$, we can obtain the answer.
84+
85+
The time complexity is $O(n \times \log M)$, where $n$ and $M$ are the length of the array $\text{nums}$ and the maximum value in the array, respectively. Ignoring the space consumption of the answer array, the space complexity is $O(1)$.
7886

7987
<!-- tabs:start -->
8088

8189
#### Python3
8290

8391
```python
84-
92+
class Solution:
93+
def minBitwiseArray(self, nums: List[int]) -> List[int]:
94+
ans = []
95+
for x in nums:
96+
if x == 2:
97+
ans.append(-1)
98+
else:
99+
for i in range(1, 32):
100+
if x >> i & 1 ^ 1:
101+
ans.append(x ^ 1 << (i - 1))
102+
break
103+
return ans
85104
```
86105

87106
#### Java
88107

89108
```java
90-
109+
class Solution {
110+
public int[] minBitwiseArray(List<Integer> nums) {
111+
int n = nums.size();
112+
int[] ans = new int[n];
113+
for (int i = 0; i < n; ++i) {
114+
int x = nums.get(i);
115+
if (x == 2) {
116+
ans[i] = -1;
117+
} else {
118+
for (int j = 1; j < 32; ++j) {
119+
if ((x >> j & 1) == 0) {
120+
ans[i] = x ^ 1 << (j - 1);
121+
break;
122+
}
123+
}
124+
}
125+
}
126+
return ans;
127+
}
128+
}
91129
```
92130

93131
#### C++
94132

95133
```cpp
96-
134+
class Solution {
135+
public:
136+
vector<int> minBitwiseArray(vector<int>& nums) {
137+
vector<int> ans;
138+
for (int x : nums) {
139+
if (x == 2) {
140+
ans.push_back(-1);
141+
} else {
142+
for (int i = 1; i < 32; ++i) {
143+
if (x >> i & 1 ^ 1) {
144+
ans.push_back(x ^ 1 << (i - 1));
145+
break;
146+
}
147+
}
148+
}
149+
}
150+
return ans;
151+
}
152+
};
97153
```
98154
99155
#### Go
100156
101157
```go
158+
func minBitwiseArray(nums []int) (ans []int) {
159+
for _, x := range nums {
160+
if x == 2 {
161+
ans = append(ans, -1)
162+
} else {
163+
for i := 1; i < 32; i++ {
164+
if x>>i&1 == 0 {
165+
ans = append(ans, x^1<<(i-1))
166+
break
167+
}
168+
}
169+
}
170+
}
171+
return
172+
}
173+
```
102174

175+
#### TypeScript
176+
177+
```ts
178+
function minBitwiseArray(nums: number[]): number[] {
179+
const ans: number[] = [];
180+
for (const x of nums) {
181+
if (x === 2) {
182+
ans.push(-1);
183+
} else {
184+
for (let i = 1; i < 32; ++i) {
185+
if (((x >> i) & 1) ^ 1) {
186+
ans.push(x ^ (1 << (i - 1)));
187+
break;
188+
}
189+
}
190+
}
191+
}
192+
return ans;
193+
}
103194
```
104195

105196
<!-- tabs:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
vector<int> minBitwiseArray(vector<int>& nums) {
4+
vector<int> ans;
5+
for (int x : nums) {
6+
if (x == 2) {
7+
ans.push_back(-1);
8+
} else {
9+
for (int i = 1; i < 32; ++i) {
10+
if (x >> i & 1 ^ 1) {
11+
ans.push_back(x ^ 1 << (i - 1));
12+
break;
13+
}
14+
}
15+
}
16+
}
17+
return ans;
18+
}
19+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
func minBitwiseArray(nums []int) (ans []int) {
2+
for _, x := range nums {
3+
if x == 2 {
4+
ans = append(ans, -1)
5+
} else {
6+
for i := 1; i < 32; i++ {
7+
if x>>i&1 == 0 {
8+
ans = append(ans, x^1<<(i-1))
9+
break
10+
}
11+
}
12+
}
13+
}
14+
return
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public int[] minBitwiseArray(List<Integer> nums) {
3+
int n = nums.size();
4+
int[] ans = new int[n];
5+
for (int i = 0; i < n; ++i) {
6+
int x = nums.get(i);
7+
if (x == 2) {
8+
ans[i] = -1;
9+
} else {
10+
for (int j = 1; j < 32; ++j) {
11+
if ((x >> j & 1) == 0) {
12+
ans[i] = x ^ 1 << (j - 1);
13+
break;
14+
}
15+
}
16+
}
17+
}
18+
return ans;
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def minBitwiseArray(self, nums: List[int]) -> List[int]:
3+
ans = []
4+
for x in nums:
5+
if x == 2:
6+
ans.append(-1)
7+
else:
8+
for i in range(1, 32):
9+
if x >> i & 1 ^ 1:
10+
ans.append(x ^ 1 << (i - 1))
11+
break
12+
return ans
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
function minBitwiseArray(nums: number[]): number[] {
2+
const ans: number[] = [];
3+
for (const x of nums) {
4+
if (x === 2) {
5+
ans.push(-1);
6+
} else {
7+
for (let i = 1; i < 32; ++i) {
8+
if (((x >> i) & 1) ^ 1) {
9+
ans.push(x ^ (1 << (i - 1)));
10+
break;
11+
}
12+
}
13+
}
14+
}
15+
return ans;
16+
}

0 commit comments

Comments
 (0)