Skip to content

Commit 38656df

Browse files
authored
feat: add rust solution to lc problem: No.1708 (#1248)
1 parent 5a05d29 commit 38656df

File tree

3 files changed

+67
-0
lines changed

3 files changed

+67
-0
lines changed

solution/1700-1799/1708.Largest Subarray Length K/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,30 @@ public:
113113
};
114114
```
115115
116+
### **Rust**
117+
118+
```rust
119+
impl Solution {
120+
#[allow(dead_code)]
121+
pub fn largest_subarray(nums: Vec<i32>, k: i32) -> Vec<i32> {
122+
let mut ret_vec = vec![i32::MIN];
123+
let n = nums.len();
124+
125+
if n == k as usize {
126+
return nums;
127+
}
128+
129+
for i in 0..=n - k as usize {
130+
if nums[i] > ret_vec[0] {
131+
ret_vec = nums[i..i + k as usize].to_vec();
132+
}
133+
}
134+
135+
ret_vec
136+
}
137+
}
138+
```
139+
116140
### **Go**
117141

118142
```go

solution/1700-1799/1708.Largest Subarray Length K/README_EN.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,30 @@ public:
100100
};
101101
```
102102
103+
### **Rust**
104+
105+
```rust
106+
impl Solution {
107+
#[allow(dead_code)]
108+
pub fn largest_subarray(nums: Vec<i32>, k: i32) -> Vec<i32> {
109+
let mut ret_vec = vec![i32::MIN];
110+
let n = nums.len();
111+
112+
if n == k as usize {
113+
return nums;
114+
}
115+
116+
for i in 0..=n - k as usize {
117+
if nums[i] > ret_vec[0] {
118+
ret_vec = nums[i..i + k as usize].to_vec();
119+
}
120+
}
121+
122+
ret_vec
123+
}
124+
}
125+
```
126+
103127
### **Go**
104128

105129
```go
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
impl Solution {
2+
#[allow(dead_code)]
3+
pub fn largest_subarray(nums: Vec<i32>, k: i32) -> Vec<i32> {
4+
let mut ret_vec = vec![i32::MIN];
5+
let n = nums.len();
6+
7+
if n == k as usize {
8+
return nums;
9+
}
10+
11+
for i in 0..=n - k as usize {
12+
if nums[i] > ret_vec[0] {
13+
ret_vec = nums[i..i + k as usize].to_vec();
14+
}
15+
}
16+
17+
ret_vec
18+
}
19+
}

0 commit comments

Comments
 (0)