添加 0053.最大子序和、0135分发糖果和0455分发饼干的 Rust版本

This commit is contained in:
wang
2022-05-01 21:08:57 +08:00
parent ce8e2019db
commit dd514eb087
3 changed files with 69 additions and 16 deletions

View File

@ -140,7 +140,7 @@ public:
## 其他语言版本 ## 其他语言版本
### Java ### Java
```java ```java
class Solution { class Solution {
public int maxSubArray(int[] nums) { public int maxSubArray(int[] nums) {
@ -180,7 +180,7 @@ class Solution {
} }
``` ```
### Python ### Python
```python ```python
class Solution: class Solution:
def maxSubArray(self, nums: List[int]) -> int: def maxSubArray(self, nums: List[int]) -> int:
@ -195,7 +195,7 @@ class Solution:
return result return result
``` ```
### Go ### Go
```go ```go
func maxSubArray(nums []int) int { func maxSubArray(nums []int) int {
@ -212,6 +212,20 @@ func maxSubArray(nums []int) int {
} }
``` ```
### Rust
```rust
pub fn max_sub_array(nums: Vec<i32>) -> i32 {
let mut max_sum = i32::MIN;
let mut curr = 0;
for n in nums.iter() {
curr += n;
max_sum = max_sum.max(curr);
curr = curr.max(0);
}
max_sum
}
```
### Javascript: ### Javascript:
```Javascript ```Javascript
var maxSubArray = function(nums) { var maxSubArray = function(nums) {

View File

@ -126,11 +126,11 @@ public:
## 其他语言版本 ## 其他语言版本
### Java ### Java
```java ```java
class Solution { class Solution {
/** /**
分两个阶段 分两个阶段
1、起点下标1 从左往右,只要 右边 比 左边 大,右边的糖果=左边 + 1 1、起点下标1 从左往右,只要 右边 比 左边 大,右边的糖果=左边 + 1
2、起点下标 ratings.length - 2 从右往左, 只要左边 比 右边 大,此时 左边的糖果应该 取本身的糖果数(符合比它左边大) 和 右边糖果数 + 1 二者的最大值,这样才符合 它比它左边的大,也比它右边大 2、起点下标 ratings.length - 2 从右往左, 只要左边 比 右边 大,此时 左边的糖果应该 取本身的糖果数(符合比它左边大) 和 右边糖果数 + 1 二者的最大值,这样才符合 它比它左边的大,也比它右边大
*/ */
@ -160,7 +160,7 @@ class Solution {
} }
``` ```
### Python ### Python
```python ```python
class Solution: class Solution:
def candy(self, ratings: List[int]) -> int: def candy(self, ratings: List[int]) -> int:
@ -213,6 +213,25 @@ func findMax(num1 int ,num2 int) int{
} }
``` ```
### Rust
```rust
pub fn candy(ratings: Vec<i32>) -> i32 {
let mut candies = vec![1i32; ratings.len()];
for i in 1..ratings.len() {
if ratings[i - 1] < ratings[i] {
candies[i] = candies[i - 1] + 1;
}
}
for i in (0..ratings.len()-1).rev() {
if ratings[i] > ratings[i + 1] {
candies[i] = candies[i].max(candies[i + 1] + 1);
}
}
candies.iter().sum()
}
```
### Javascript: ### Javascript:
```Javascript ```Javascript
var candy = function(ratings) { var candy = function(ratings) {
@ -229,7 +248,7 @@ var candy = function(ratings) {
candys[i] = Math.max(candys[i], candys[i + 1] + 1) candys[i] = Math.max(candys[i], candys[i + 1] + 1)
} }
} }
let count = candys.reduce((a, b) => { let count = candys.reduce((a, b) => {
return a + b return a + b
}) })

View File

@ -106,7 +106,7 @@ public:
## 其他语言版本 ## 其他语言版本
### Java ### Java
```java ```java
class Solution { class Solution {
// 思路1优先考虑饼干小饼干先喂饱小胃口 // 思路1优先考虑饼干小饼干先喂饱小胃口
@ -145,7 +145,7 @@ class Solution {
} }
``` ```
### Python ### Python
```python ```python
class Solution: class Solution:
# 思路1优先考虑胃饼干 # 思路1优先考虑胃饼干
@ -166,13 +166,13 @@ class Solution:
s.sort() s.sort()
start, count = len(s) - 1, 0 start, count = len(s) - 1, 0
for index in range(len(g) - 1, -1, -1): # 先喂饱大胃口 for index in range(len(g) - 1, -1, -1): # 先喂饱大胃口
if start >= 0 and g[index] <= s[start]: if start >= 0 and g[index] <= s[start]:
start -= 1 start -= 1
count += 1 count += 1
return count return count
``` ```
### Go ### Go
```golang ```golang
//排序后,局部最优 //排序后,局部最优
func findContentChildren(g []int, s []int) int { func findContentChildren(g []int, s []int) int {
@ -191,7 +191,27 @@ func findContentChildren(g []int, s []int) int {
} }
``` ```
### Javascript ### Rust
```rust
pub fn find_content_children(children: Vec<i32>, cookie: Vec<i32>) -> i32 {
let mut children = children;
let mut cookies = cookie;
children.sort();
cookies.sort();
let (mut child, mut cookie) = (0usize, 0usize);
while child < children.len() && cookie < cookies.len() {
// 优先选择最小饼干喂饱孩子
if children[child] <= cookies[cookie] {
child += 1;
}
cookie += 1
}
child as i32
}
```
### Javascript
```js ```js
var findContentChildren = function(g, s) { var findContentChildren = function(g, s) {
g = g.sort((a, b) => a - b) g = g.sort((a, b) => a - b)
@ -203,7 +223,7 @@ var findContentChildren = function(g, s) {
result++ result++
index-- index--
} }
} }
return result return result
}; };
@ -251,7 +271,7 @@ function findContentChildren(g: number[], s: number[]): number {
}; };
``` ```
### C ### C
```c ```c
int cmp(int* a, int* b) { int cmp(int* a, int* b) {
@ -261,7 +281,7 @@ int cmp(int* a, int* b) {
int findContentChildren(int* g, int gSize, int* s, int sSize){ int findContentChildren(int* g, int gSize, int* s, int sSize){
if(sSize == 0) if(sSize == 0)
return 0; return 0;
//将两个数组排序为升序 //将两个数组排序为升序
qsort(g, gSize, sizeof(int), cmp); qsort(g, gSize, sizeof(int), cmp);
qsort(s, sSize, sizeof(int), cmp); qsort(s, sSize, sizeof(int), cmp);