Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] master from youngyangyang04:master #102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 11 commits into AlgorithmAndLeetCode:master from youngyangyang04:master
Sep 30, 2022
Merged
Changes from 1 commit
Commits
Show all changes
11 commits
Select commit Hold shift + click to select a range
8309641
add 0055 go tanxin
Galaxies2580 Sep 26, 2022
37d518f
添加0222.完全二叉树的节点个数Go版本.md
Galaxies2580 Sep 26, 2022
f109f92
添加559.n叉树的最大深度Go版本
Galaxies2580 Sep 26, 2022
626f985
Update 链表理论基础 about rust
fwqaaq Sep 26, 2022
9b7cf50
增加0045跳跃游戏的go贪心版本和python贪心版本二
Galaxies2580 Sep 27, 2022
2846071
Update 0019.删除链表的倒数第N个节点.md
Flow-sandyu Sep 27, 2022
87c5053
Update 0019.删除链表的倒数第N个节点.md
Flow-sandyu Sep 27, 2022
5252cfc
Update 链表理论基础.md
fwqaaq Sep 28, 2022
f6cfaf1
Merge pull request #1662 from Galaxies2580/master
youngyangyang04 Sep 30, 2022
a8d946c
Merge pull request #1664 from Jack-Zhang-1314/patch-3
youngyangyang04 Sep 30, 2022
180c08f
Merge pull request #1665 from Flow-sandyu/master
youngyangyang04 Sep 30, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
增加0045跳跃游戏的go贪心版本和python贪心版本二
  • Loading branch information
Galaxies2580 committed Sep 27, 2022
commit 9b7cf50c5a67596c50b9f720d1c845bff83ebc49
83 changes: 83 additions & 0 deletions problems/0045.跳跃游戏II.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,26 @@ class Solution:
return ans
```

```python
# 贪心版本二
class Solution:
def jump(self, nums: List[int]) -> int:
if len(nums) == 1:
return 0
curDistance, nextDistance = 0, 0
step = 0
for i in range(len(nums)-1):
nextDistance = max(nextDistance, nums[i]+i)
if i == curDistance:
curDistance = nextDistance
step += 1
return step
```



### Go

```Go
func jump(nums []int) int {
dp := make([]int, len(nums))
Expand All @@ -240,7 +259,71 @@ func min(a, b int) int {
}
```

```go
// 贪心版本一
func jump(nums []int) int {
n := len(nums)
if n == 1 {
return 0
}
cur, next := 0, 0
step := 0
for i := 0; i < n; i++ {
next = max(nums[i]+i, next)
if i == cur {
if cur != n-1 {
step++
cur = next
if cur >= n-1 {
return step
}
} else {
return step
}
}
}
return step
}

func max(a, b int) int {
if a > b {
return a
}
return b
}
```

```go
// 贪心版本二
func jump(nums []int) int {
n := len(nums)
if n == 1 {
return 0
}
cur, next := 0, 0
step := 0
for i := 0; i < n-1; i++ {
next = max(nums[i]+i, next)
if i == cur {
cur = next
step++
}
}
return step
}

func max(a, b int) int {
if a > b {
return a
}
return b
}
```



### Javascript

```Javascript
var jump = function(nums) {
let curIndex = 0
Expand Down

AltStyle によって変換されたページ (->オリジナル) /