diff --git a/Pull Here/LeetCode/Jump Game b/Pull Here/LeetCode/Jump Game new file mode 100644 index 0000000..c92e8dd --- /dev/null +++ b/Pull Here/LeetCode/Jump Game @@ -0,0 +1,50 @@ +public class Solution { + + public int jump(int[] A) { + int jumps = 0, curEnd = 0, curFarthest = 0; + for (int i = 0; i < A.length - 1; i++) { // @note: i < Len - 1. 也就是停在倒数第二个 + curFarthest = Math.max(curFarthest, i + A[i]); + if (i == curEnd) { + jumps++; + curEnd = curFarthest; + + if (curEnd >= A.length - 1) { + break; + } + } + } + return jumps; + } +} + + + +public class Jump_Game_II { + + class Solution { + public int jump(int[] nums) { + if (nums == null || nums.length == 0) { + return 0; + } + + int left = 0; // last jump reaching to + int right = 0; // current jump reaching to + + int count = 0; + + for (int i = 0; i < nums.length - 1; i++) { // @note: i < Len - 1. 也就是停在倒数第二个 + right = Math.max(right, i + nums[i]); + if (i == left) { // reaching furthest of last jump + count++; + left = right; + + if (curEnd >= A.length - 1) { + break; + } + } + } + + return count; + } + } +}