mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-10 20:40:39 +08:00
Merge branch 'youngyangyang04:master' into master
This commit is contained in:
@ -305,6 +305,56 @@ object Solution {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Rust
|
||||||
|
|
||||||
|
```Rust
|
||||||
|
//版本一
|
||||||
|
impl Solution {
|
||||||
|
fn max(a: i32, b:i32) -> i32 {
|
||||||
|
if a > b { a } else { b }
|
||||||
|
}
|
||||||
|
pub fn jump(nums: Vec<i32>) -> i32 {
|
||||||
|
if nums.len() == 0 { return 0; }
|
||||||
|
let mut cur_distance: i32 = 0;
|
||||||
|
let mut ans: i32 = 0;
|
||||||
|
let mut next_distance: i32 = 0;
|
||||||
|
for i in 0..nums.len() {
|
||||||
|
next_distance = Self::max(nums[i] + i as i32, next_distance);
|
||||||
|
if i as i32 == cur_distance {
|
||||||
|
if cur_distance != (nums.len() - 1) as i32 {
|
||||||
|
ans += 1;
|
||||||
|
cur_distance = next_distance;
|
||||||
|
if next_distance == (nums.len() - 1) as i32 { break; }
|
||||||
|
}
|
||||||
|
else { break; }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ans
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
```Rust
|
||||||
|
//版本二
|
||||||
|
impl Solution {
|
||||||
|
fn max(a: i32, b:i32) -> i32 {
|
||||||
|
if a > b { a } else { b }
|
||||||
|
}
|
||||||
|
pub fn jump(nums: Vec<i32>) -> i32 {
|
||||||
|
let mut cur_distance: i32 = 0;
|
||||||
|
let mut ans: i32 = 0;
|
||||||
|
let mut next_distance: i32 = 0;
|
||||||
|
for i in 0..nums.len() - 1 {
|
||||||
|
next_distance = Self::max(nums[i] + i as i32, next_distance);
|
||||||
|
if i as i32 == cur_distance {
|
||||||
|
cur_distance = next_distance;
|
||||||
|
ans += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ans
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
|
@ -154,6 +154,26 @@ var canJump = function(nums) {
|
|||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Rust
|
||||||
|
|
||||||
|
```Rust
|
||||||
|
impl Solution {
|
||||||
|
fn max(a: usize, b: usize) -> usize {
|
||||||
|
if a > b { a } else { b }
|
||||||
|
}
|
||||||
|
pub fn can_jump(nums: Vec<i32>) -> bool {
|
||||||
|
let mut cover = 0;
|
||||||
|
if (nums.len() == 1) { return true; }
|
||||||
|
let mut i = 0;
|
||||||
|
while i <= cover {
|
||||||
|
cover = Self::max(i + nums[i] as usize, cover);
|
||||||
|
if cover >= nums.len() - 1 { return true; }
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
### C
|
### C
|
||||||
```c
|
```c
|
||||||
|
@ -263,6 +263,13 @@ function reverseLeftWords(s: string, n: number): string {
|
|||||||
return strArr.join('');
|
return strArr.join('');
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
方法二:
|
||||||
|
```typescript
|
||||||
|
// 拼接两个字符串,截取符合要求的部分
|
||||||
|
function reverseLeftWords(s: string, n: number): string {
|
||||||
|
return (s+s).slice(n,s.length+n);
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
Swift:
|
Swift:
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user