Merge branch 'youngyangyang04:master' into master

This commit is contained in:
XuBoLun
2021-08-28 12:56:49 +08:00
committed by GitHub
5 changed files with 101 additions and 20 deletions

View File

@ -214,19 +214,17 @@ end
```
Rust:
```rust
pub fn remove_element(nums: &mut Vec<i32>, val: i32) -> &mut Vec<i32> {
let mut start: usize = 0;
while start < nums.len() {
if nums[start] == val {
nums.remove(start);
impl Solution {
pub fn remove_element(nums: &mut Vec<i32>, val: i32) -> i32 {
let mut slowIdx = 0;
for pos in (0..nums.len()) {
if nums[pos]!=val {
nums[slowIdx] = nums[pos];
slowIdx += 1;
}
}
start += 1;
return (slowIdx) as i32;
}
nums
}
fn main() {
let mut nums = vec![5,1,3,5,2,3,4,1];
println!("{:?}",remove_element(&mut nums, 5));
}
```

View File

@ -139,17 +139,11 @@ Java
// 贪心思路
class Solution {
public int maxProfit(int[] prices) {
int sum = 0;
int profit = 0;
int buy = prices[0];
int result = 0;
for (int i = 1; i < prices.length; i++) {
profit = prices[i] - buy;
if (profit > 0) {
sum += profit;
}
buy = prices[i];
result += Math.max(prices[i] - prices[i - 1], 0);
}
return sum;
return result;
}
}
```

View File

@ -237,6 +237,32 @@ func minSubArrayLen(_ target: Int, _ nums: [Int]) -> Int {
}
```
Rust:
```rust
impl Solution {
pub fn min_sub_array_len(target: i32, nums: Vec<i32>) -> i32 {
let (mut result, mut subLength): (i32, i32) = (i32::MAX, 0);
let (mut sum, mut i) = (0, 0);
for (pos, val) in nums.iter().enumerate() {
sum += val;
while sum >= target {
subLength = (pos - i + 1) as i32;
if result > subLength {
result = subLength;
}
sum -= nums[i];
i += 1;
}
}
if result == i32::MAX {
return 0;
}
result
}
}
```
-----------------------

View File

@ -117,6 +117,7 @@ public:
Java
```java
class Solution {
// 思路1优先考虑饼干小饼干先喂饱小胃口
public int findContentChildren(int[] g, int[] s) {
Arrays.sort(g);
Arrays.sort(s);
@ -132,10 +133,30 @@ class Solution {
}
}
```
```java
class Solution {
// 思路2优先考虑胃口先喂饱大胃口
public int findContentChildren(int[] g, int[] s) {
Arrays.sort(g);
Arrays.sort(s);
int count = 0;
int start = s.length - 1;
// 遍历胃口
for (int index = g.length - 1; index >= 0; index--) {
if(start >= 0 && g[index] <= s[start]) {
start--;
count++;
}
}
return count;
}
}
```
Python
```python3
class Solution:
# 思路1优先考虑胃饼干
def findContentChildren(self, g: List[int], s: List[int]) -> int:
g.sort()
s.sort()
@ -145,6 +166,20 @@ class Solution:
res += 1
return res
```
```python
class Solution:
# 思路2优先考虑胃口
def findContentChildren(self, g: List[int], s: List[int]) -> int:
g.sort()
s.sort()
start, count = len(s) - 1, 0
for index in range(len(g) - 1, -1, -1): # 先喂饱大胃口
if start >= 0 and g[index] <= s[start]:
start -= 1
count += 1
return count
```
Go
```golang
//排序后,局部最优

View File

@ -152,7 +152,35 @@ class Solution {
}
}
```
```java
// 解法3
class Solution {
public String reverseStr(String s, int k) {
char[] ch = s.toCharArray();
// 1. 每隔 2k 个字符的前 k 个字符进行反转
for (int i = 0; i< ch.length; i += 2 * k) {
// 2. 剩余字符小于 2k 但大于或等于 k 个,则反转前 k 个字符
if (i + k <= ch.length) {
reverse(ch, i, i + k -1);
continue;
}
// 3. 剩余字符少于 k 个,则将剩余字符全部反转
reverse(ch, i, ch.length - 1);
}
return new String(ch);
}
// 定义翻转函数
public void reverse(char[] ch, int i, int j) {
for (; i < j; i++, j--) {
char temp = ch[i];
ch[i] = ch[j];
ch[j] = temp;
}
}
}
```
Python
```python
class Solution: