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

Commitecd7f7b

Browse files
refactor 403
1 parent2d8d7a8 commitecd7f7b

File tree

1 file changed

+30
-27
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+30
-27
lines changed

‎src/main/java/com/fishercoder/solutions/_403.java

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -50,37 +50,40 @@
5050
*/
5151
publicclass_403 {
5252

53-
/**Reference: https://discuss.leetcode.com/topic/59903/very-easy-to-understand-java-solution-with-explanations/2
54-
* and https://leetcode.com/articles/frog-jump/#approach-5-using-dynamic-programmingaccepted*/
55-
publicbooleancanCross(int[]stones) {
56-
if (stones.length ==0) {
57-
returntrue;
58-
}
59-
Map<Integer,Set<Integer>>map =newHashMap<>(stones.length);
60-
map.put(0,newHashSet<>());
61-
map.get(0).add(1);
62-
for (inti =1;i <stones.length;i++) {
63-
map.put(stones[i],newHashSet<>());
64-
}
53+
publicstaticclassSolution1 {
54+
/**
55+
* Reference: https://discuss.leetcode.com/topic/59903/very-easy-to-understand-java-solution-with-explanations/2
56+
* and https://leetcode.com/articles/frog-jump/#approach-5-using-dynamic-programmingaccepted
57+
*/
58+
publicbooleancanCross(int[]stones) {
59+
if (stones.length ==0) {
60+
returntrue;
61+
}
62+
Map<Integer,Set<Integer>>map =newHashMap<>(stones.length);
63+
map.put(0,newHashSet<>());
64+
map.get(0).add(1);
65+
for (inti =1;i <stones.length;i++) {
66+
map.put(stones[i],newHashSet<>());
67+
}
6568

66-
for (inti =0;i <stones.length;i++) {
67-
intstone =stones[i];
68-
for (intstep :map.get(stone)) {
69-
intreach =step +stone;
70-
if (reach ==stones[stones.length -1]) {
71-
returntrue;
72-
}
73-
Set<Integer>set =map.get(reach);
74-
if (set !=null) {
75-
set.add(step);
76-
if (step -1 >0) {
77-
set.add(step -1);
69+
for (inti =0;i <stones.length;i++) {
70+
intstone =stones[i];
71+
for (intstep :map.get(stone)) {
72+
intreach =step +stone;
73+
if (reach ==stones[stones.length -1]) {
74+
returntrue;
75+
}
76+
Set<Integer>set =map.get(reach);
77+
if (set !=null) {
78+
set.add(step);
79+
if (step -1 >0) {
80+
set.add(step -1);
81+
}
82+
set.add(step +1);
7883
}
79-
set.add(step +1);
8084
}
8185
}
86+
returnfalse;
8287
}
83-
returnfalse;
8488
}
85-
8689
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp