Skip to content

Commit c3ff33f

Browse files
authored
Merge branch 'youngyangyang04:master' into master
2 parents 54e4f43 + 1b1ccb1 commit c3ff33f

File tree

5 files changed

+101
-20
lines changed

5 files changed

+101
-20
lines changed

problems/0027.移除元素.md

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -214,19 +214,17 @@ end
214214
```
215215
Rust:
216216
```rust
217-
pub fn remove_element(nums: &mut Vec<i32>, val: i32) -> &mut Vec<i32> {
218-
let mut start: usize = 0;
219-
while start < nums.len() {
220-
if nums[start] == val {
221-
nums.remove(start);
217+
impl Solution {
218+
pub fn remove_element(nums: &mut Vec<i32>, val: i32) -> i32 {
219+
let mut slowIdx = 0;
220+
for pos in (0..nums.len()) {
221+
if nums[pos]!=val {
222+
nums[slowIdx] = nums[pos];
223+
slowIdx += 1;
224+
}
222225
}
223-
start += 1;
226+
return (slowIdx) as i32;
224227
}
225-
nums
226-
}
227-
fn main() {
228-
let mut nums = vec![5,1,3,5,2,3,4,1];
229-
println!("{:?}",remove_element(&mut nums, 5));
230228
}
231229
```
232230

problems/0122.买卖股票的最佳时机II.md

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -139,17 +139,11 @@ Java:
139139
// 贪心思路
140140
class Solution {
141141
public int maxProfit(int[] prices) {
142-
int sum = 0;
143-
int profit = 0;
144-
int buy = prices[0];
142+
int result = 0;
145143
for (int i = 1; i < prices.length; i++) {
146-
profit = prices[i] - buy;
147-
if (profit > 0) {
148-
sum += profit;
149-
}
150-
buy = prices[i];
144+
result += Math.max(prices[i] - prices[i - 1], 0);
151145
}
152-
return sum;
146+
return result;
153147
}
154148
}
155149
```

problems/0209.长度最小的子数组.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,32 @@ func minSubArrayLen(_ target: Int, _ nums: [Int]) -> Int {
237237
}
238238
```
239239

240+
Rust:
241+
242+
```rust
243+
impl Solution {
244+
pub fn min_sub_array_len(target: i32, nums: Vec<i32>) -> i32 {
245+
let (mut result, mut subLength): (i32, i32) = (i32::MAX, 0);
246+
let (mut sum, mut i) = (0, 0);
247+
248+
for (pos, val) in nums.iter().enumerate() {
249+
sum += val;
250+
while sum >= target {
251+
subLength = (pos - i + 1) as i32;
252+
if result > subLength {
253+
result = subLength;
254+
}
255+
sum -= nums[i];
256+
i += 1;
257+
}
258+
}
259+
if result == i32::MAX {
260+
return 0;
261+
}
262+
result
263+
}
264+
}
265+
```
240266

241267

242268
-----------------------

problems/0455.分发饼干.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ public:
117117
Java:
118118
```java
119119
class Solution {
120+
// 思路1:优先考虑饼干,小饼干先喂饱小胃口
120121
public int findContentChildren(int[] g, int[] s) {
121122
Arrays.sort(g);
122123
Arrays.sort(s);
@@ -132,10 +133,30 @@ class Solution {
132133
}
133134
}
134135
```
136+
```java
137+
class Solution {
138+
// 思路2:优先考虑胃口,先喂饱大胃口
139+
public int findContentChildren(int[] g, int[] s) {
140+
Arrays.sort(g);
141+
Arrays.sort(s);
142+
int count = 0;
143+
int start = s.length - 1;
144+
// 遍历胃口
145+
for (int index = g.length - 1; index >= 0; index--) {
146+
if(start >= 0 && g[index] <= s[start]) {
147+
start--;
148+
count++;
149+
}
150+
}
151+
return count;
152+
}
153+
}
154+
```
135155

136156
Python:
137157
```python3
138158
class Solution:
159+
# 思路1:优先考虑胃饼干
139160
def findContentChildren(self, g: List[int], s: List[int]) -> int:
140161
g.sort()
141162
s.sort()
@@ -145,6 +166,20 @@ class Solution:
145166
res += 1
146167
return res
147168
```
169+
```python
170+
class Solution:
171+
# 思路2:优先考虑胃口
172+
def findContentChildren(self, g: List[int], s: List[int]) -> int:
173+
g.sort()
174+
s.sort()
175+
start, count = len(s) - 1, 0
176+
for index in range(len(g) - 1, -1, -1): # 先喂饱大胃口
177+
if start >= 0 and g[index] <= s[start]:
178+
start -= 1
179+
count += 1
180+
return count
181+
```
182+
148183
Go:
149184
```golang
150185
//排序后,局部最优

problems/0541.反转字符串II.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,35 @@ class Solution {
152152
}
153153
}
154154
```
155+
```java
156+
// 解法3
157+
class Solution {
158+
public String reverseStr(String s, int k) {
159+
char[] ch = s.toCharArray();
160+
// 1. 每隔 2k 个字符的前 k 个字符进行反转
161+
for (int i = 0; i< ch.length; i += 2 * k) {
162+
// 2. 剩余字符小于 2k 但大于或等于 k 个,则反转前 k 个字符
163+
if (i + k <= ch.length) {
164+
reverse(ch, i, i + k -1);
165+
continue;
166+
}
167+
// 3. 剩余字符少于 k 个,则将剩余字符全部反转
168+
reverse(ch, i, ch.length - 1);
169+
}
170+
return new String(ch);
155171

172+
}
173+
// 定义翻转函数
174+
public void reverse(char[] ch, int i, int j) {
175+
for (; i < j; i++, j--) {
176+
char temp = ch[i];
177+
ch[i] = ch[j];
178+
ch[j] = temp;
179+
}
180+
181+
}
182+
}
183+
```
156184
Python:
157185
```python
158186
class Solution:

0 commit comments

Comments
 (0)