mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
添加 0053.最大子序和、0135分发糖果和0455分发饼干的 Rust版本
This commit is contained in:
@ -140,7 +140,7 @@ public:
|
||||
## 其他语言版本
|
||||
|
||||
|
||||
### Java
|
||||
### Java
|
||||
```java
|
||||
class Solution {
|
||||
public int maxSubArray(int[] nums) {
|
||||
@ -180,7 +180,7 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
### Python
|
||||
### Python
|
||||
```python
|
||||
class Solution:
|
||||
def maxSubArray(self, nums: List[int]) -> int:
|
||||
@ -195,7 +195,7 @@ class Solution:
|
||||
return result
|
||||
```
|
||||
|
||||
### Go
|
||||
### Go
|
||||
|
||||
```go
|
||||
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
|
||||
var maxSubArray = function(nums) {
|
||||
|
@ -126,11 +126,11 @@ public:
|
||||
## 其他语言版本
|
||||
|
||||
|
||||
### Java
|
||||
### Java
|
||||
```java
|
||||
class Solution {
|
||||
/**
|
||||
分两个阶段
|
||||
/**
|
||||
分两个阶段
|
||||
1、起点下标1 从左往右,只要 右边 比 左边 大,右边的糖果=左边 + 1
|
||||
2、起点下标 ratings.length - 2 从右往左, 只要左边 比 右边 大,此时 左边的糖果应该 取本身的糖果数(符合比它左边大) 和 右边糖果数 + 1 二者的最大值,这样才符合 它比它左边的大,也比它右边大
|
||||
*/
|
||||
@ -160,7 +160,7 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
### Python
|
||||
### Python
|
||||
```python
|
||||
class Solution:
|
||||
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
|
||||
var candy = function(ratings) {
|
||||
@ -229,7 +248,7 @@ var candy = function(ratings) {
|
||||
candys[i] = Math.max(candys[i], candys[i + 1] + 1)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
let count = candys.reduce((a, b) => {
|
||||
return a + b
|
||||
})
|
||||
|
@ -106,7 +106,7 @@ public:
|
||||
## 其他语言版本
|
||||
|
||||
|
||||
### Java
|
||||
### Java
|
||||
```java
|
||||
class Solution {
|
||||
// 思路1:优先考虑饼干,小饼干先喂饱小胃口
|
||||
@ -145,7 +145,7 @@ class Solution {
|
||||
}
|
||||
```
|
||||
|
||||
### Python
|
||||
### Python
|
||||
```python
|
||||
class Solution:
|
||||
# 思路1:优先考虑胃饼干
|
||||
@ -166,13 +166,13 @@ class Solution:
|
||||
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]:
|
||||
if start >= 0 and g[index] <= s[start]:
|
||||
start -= 1
|
||||
count += 1
|
||||
return count
|
||||
```
|
||||
|
||||
### Go
|
||||
### Go
|
||||
```golang
|
||||
//排序后,局部最优
|
||||
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
|
||||
var findContentChildren = function(g, s) {
|
||||
g = g.sort((a, b) => a - b)
|
||||
@ -203,7 +223,7 @@ var findContentChildren = function(g, s) {
|
||||
result++
|
||||
index--
|
||||
}
|
||||
}
|
||||
}
|
||||
return result
|
||||
};
|
||||
|
||||
@ -251,7 +271,7 @@ function findContentChildren(g: number[], s: number[]): number {
|
||||
};
|
||||
```
|
||||
|
||||
### C
|
||||
### C
|
||||
|
||||
```c
|
||||
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){
|
||||
if(sSize == 0)
|
||||
return 0;
|
||||
|
||||
|
||||
//将两个数组排序为升序
|
||||
qsort(g, gSize, sizeof(int), cmp);
|
||||
qsort(s, sSize, sizeof(int), cmp);
|
||||
|
Reference in New Issue
Block a user