Skip to content

Commit 25202d1

Browse files
authored
feat: add Rust solution for lc No.3314 (#4983)
1 parent bb4b52a commit 25202d1

3 files changed

Lines changed: 64 additions & 0 deletions

File tree

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,29 @@ function minBitwiseArray(nums: number[]): number[] {
201201
}
202202
```
203203

204+
#### Rust
205+
206+
```rust
207+
impl Solution {
208+
pub fn min_bitwise_array(nums: Vec<i32>) -> Vec<i32> {
209+
let mut ans = Vec::with_capacity(nums.len());
210+
for x in nums {
211+
if x == 2 {
212+
ans.push(-1);
213+
} else {
214+
for i in 1..32 {
215+
if (((x >> i) & 1) ^ 1) == 1 {
216+
ans.push(x ^ (1 << (i - 1)));
217+
break;
218+
}
219+
}
220+
}
221+
}
222+
ans
223+
}
224+
}
225+
```
226+
204227
<!-- tabs:end -->
205228

206229
<!-- solution:end -->

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,29 @@ function minBitwiseArray(nums: number[]): number[] {
195195
}
196196
```
197197

198+
#### Rust
199+
200+
```rust
201+
impl Solution {
202+
pub fn min_bitwise_array(nums: Vec<i32>) -> Vec<i32> {
203+
let mut ans = Vec::with_capacity(nums.len());
204+
for x in nums {
205+
if x == 2 {
206+
ans.push(-1);
207+
} else {
208+
for i in 1..32 {
209+
if (((x >> i) & 1) ^ 1) == 1 {
210+
ans.push(x ^ (1 << (i - 1)));
211+
break;
212+
}
213+
}
214+
}
215+
}
216+
ans
217+
}
218+
}
219+
```
220+
198221
<!-- tabs:end -->
199222

200223
<!-- solution:end -->
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
impl Solution {
2+
pub fn min_bitwise_array(nums: Vec<i32>) -> Vec<i32> {
3+
let mut ans = Vec::with_capacity(nums.len());
4+
for x in nums {
5+
if x == 2 {
6+
ans.push(-1);
7+
} else {
8+
for i in 1..32 {
9+
if (((x >> i) & 1) ^ 1) == 1 {
10+
ans.push(x ^ (1 << (i - 1)));
11+
break;
12+
}
13+
}
14+
}
15+
}
16+
ans
17+
}
18+
}

0 commit comments

Comments
 (0)