mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 08:50:15 +08:00
Merge branch 'youngyangyang04:master' into master
This commit is contained in:
@ -214,19 +214,17 @@ end
|
|||||||
```
|
```
|
||||||
Rust:
|
Rust:
|
||||||
```rust
|
```rust
|
||||||
pub fn remove_element(nums: &mut Vec<i32>, val: i32) -> &mut Vec<i32> {
|
impl Solution {
|
||||||
let mut start: usize = 0;
|
pub fn remove_element(nums: &mut Vec<i32>, val: i32) -> i32 {
|
||||||
while start < nums.len() {
|
let mut slowIdx = 0;
|
||||||
if nums[start] == val {
|
for pos in (0..nums.len()) {
|
||||||
nums.remove(start);
|
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));
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -139,17 +139,11 @@ Java:
|
|||||||
// 贪心思路
|
// 贪心思路
|
||||||
class Solution {
|
class Solution {
|
||||||
public int maxProfit(int[] prices) {
|
public int maxProfit(int[] prices) {
|
||||||
int sum = 0;
|
int result = 0;
|
||||||
int profit = 0;
|
|
||||||
int buy = prices[0];
|
|
||||||
for (int i = 1; i < prices.length; i++) {
|
for (int i = 1; i < prices.length; i++) {
|
||||||
profit = prices[i] - buy;
|
result += Math.max(prices[i] - prices[i - 1], 0);
|
||||||
if (profit > 0) {
|
|
||||||
sum += profit;
|
|
||||||
}
|
|
||||||
buy = prices[i];
|
|
||||||
}
|
}
|
||||||
return sum;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
|
@ -117,6 +117,7 @@ public:
|
|||||||
Java:
|
Java:
|
||||||
```java
|
```java
|
||||||
class Solution {
|
class Solution {
|
||||||
|
// 思路1:优先考虑饼干,小饼干先喂饱小胃口
|
||||||
public int findContentChildren(int[] g, int[] s) {
|
public int findContentChildren(int[] g, int[] s) {
|
||||||
Arrays.sort(g);
|
Arrays.sort(g);
|
||||||
Arrays.sort(s);
|
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:
|
Python:
|
||||||
```python3
|
```python3
|
||||||
class Solution:
|
class Solution:
|
||||||
|
# 思路1:优先考虑胃饼干
|
||||||
def findContentChildren(self, g: List[int], s: List[int]) -> int:
|
def findContentChildren(self, g: List[int], s: List[int]) -> int:
|
||||||
g.sort()
|
g.sort()
|
||||||
s.sort()
|
s.sort()
|
||||||
@ -145,6 +166,20 @@ class Solution:
|
|||||||
res += 1
|
res += 1
|
||||||
return res
|
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:
|
Go:
|
||||||
```golang
|
```golang
|
||||||
//排序后,局部最优
|
//排序后,局部最优
|
||||||
|
@ -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:
|
||||||
```python
|
```python
|
||||||
class Solution:
|
class Solution:
|
||||||
|
Reference in New Issue
Block a user