Movatterモバイル変換


[0]ホーム

URL:


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

Commit530f379

Browse files
author
haotf
committed
贪心
1 parentea9fe98 commit530f379

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

‎134.加油站.java‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* @lc app=leetcode.cn id=134 lang=java
3+
*
4+
* [134] 加油站
5+
*/
6+
7+
// @lc code=start
8+
classSolution {
9+
publicintcanCompleteCircuit(int[]gas,int[]cost) {
10+
intsum =0;
11+
inttank =0;
12+
intstart =0;
13+
for (inti =0;i <cost.length;i++) {
14+
sum =sum +gas[i] -cost[i];
15+
tank =tank +gas[i] -cost[i];
16+
if (tank <0) {
17+
tank =0;
18+
start =i +1;
19+
}
20+
}
21+
if (sum <0 ||start ==gas.length)
22+
return -1;
23+
returnstart;
24+
}
25+
}
26+
// @lc code=end

‎45.跳跃游戏-ii.java‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* @lc app=leetcode.cn id=45 lang=java
3+
*
4+
* [45] 跳跃游戏 II
5+
*/
6+
7+
// @lc code=start
8+
classSolution {
9+
publicintjump(int[]nums) {
10+
intlen =nums.length;
11+
intfarthest =0;
12+
intstep =0;
13+
intpoint =0;
14+
15+
for (inti =0;i <len -1;i++) {
16+
farthest =Math.max(farthest,i +nums[i]);
17+
if (point ==i) {
18+
point =farthest;
19+
step++;
20+
}
21+
}
22+
returnstep;
23+
}
24+
}
25+
// @lc code=end

‎55.跳跃游戏.java‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* @lc app=leetcode.cn id=55 lang=java
3+
*
4+
* [55] 跳跃游戏
5+
*/
6+
7+
// @lc code=start
8+
classSolution {
9+
publicbooleancanJump(int[]nums) {
10+
if (nums.length ==0)
11+
returntrue;
12+
intlen =nums.length;
13+
intfarthest =0;
14+
15+
for (inti =0;i <len -1;i++) {
16+
farthest =Math.max(farthest,i +nums[i]);
17+
if (farthest <=i)
18+
returnfalse;
19+
}
20+
21+
returnfarthest >=len -1;
22+
}
23+
}
24+
// @lc code=end

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp