|
50 | 50 | */
|
51 | 51 | public class _403 {
|
52 | 52 |
|
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 |
| - public boolean canCross(int[] stones) { |
56 |
| - if (stones.length == 0) { |
57 |
| - return true; |
58 |
| - } |
59 |
| - Map<Integer, Set<Integer>> map = new HashMap<>(stones.length); |
60 |
| - map.put(0, new HashSet<>()); |
61 |
| - map.get(0).add(1); |
62 |
| - for (int i = 1; i < stones.length; i++) { |
63 |
| - map.put(stones[i], new HashSet<>()); |
64 |
| - } |
| 53 | + public static class Solution1 { |
| 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 | + public boolean canCross(int[] stones) { |
| 59 | + if (stones.length == 0) { |
| 60 | + return true; |
| 61 | + } |
| 62 | + Map<Integer, Set<Integer>> map = new HashMap<>(stones.length); |
| 63 | + map.put(0, new HashSet<>()); |
| 64 | + map.get(0).add(1); |
| 65 | + for (int i = 1; i < stones.length; i++) { |
| 66 | + map.put(stones[i], new HashSet<>()); |
| 67 | + } |
65 | 68 |
|
66 |
| - for (int i = 0; i < stones.length; i++) { |
67 |
| - int stone = stones[i]; |
68 |
| - for (int step : map.get(stone)) { |
69 |
| - int reach = step + stone; |
70 |
| - if (reach == stones[stones.length - 1]) { |
71 |
| - return true; |
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 (int i = 0; i < stones.length; i++) { |
| 70 | + int stone = stones[i]; |
| 71 | + for (int step : map.get(stone)) { |
| 72 | + int reach = step + stone; |
| 73 | + if (reach == stones[stones.length - 1]) { |
| 74 | + return true; |
| 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); |
78 | 83 | }
|
79 |
| - set.add(step + 1); |
80 | 84 | }
|
81 | 85 | }
|
| 86 | + return false; |
82 | 87 | }
|
83 |
| - return false; |
84 | 88 | }
|
85 |
| - |
86 | 89 | }
|
0 commit comments