您的位置:首页 > 其它

Leetcode 403. Frog Jump 青蛙过河 解题报告

2016-09-18 19:18 399 查看

1 解题思想

leetcode上有一种青蛙,不会游泳,但是要过河

从河边出发,位置0,河上有一堆石头。

这只青蛙比较奇怪,若前一次他跳了K步,那么他这次就只能在K-1 K K+1三种步伐中跳

现在请问他能否跳到对岸

恩,这道题我掩面而过,我发现直接暴力搜索是可以通过的。。这就没什么好讲的了,不停的搜索就好

2 原题

A frog is crossing a river. The river is divided into x units and at each unit there may or may not exist a stone. The frog can jump on a stone, but it must not jump into the water.
Given a list of stones' positions (in units) in sorted ascending order, determine if the frog is able to cross the river by landing on the last stone. Initially, the frog is on the first stone and assume the first jump must be 1 unit.
If the frog has just jumped k units, then its next jump must be either k - 1, k, or k + 1 units. Note that the frog can only jump in the forward direction.
Note:
The number of stones is ≥ 2 and is < 1,100.
Each stone's position will be a non-negative integer < 231.
The first stone's position is always 0.

Example 1:
[0,1,3,5,6,8,12,17]

There are a total of 8 stones.
The first stone at the 0th unit, second stone at the 1st unit,
third stone at the 3rd unit, and so on...
The last stone at the 17th unit.

Return true. The frog can jump to the last stone by jumping
1 unit to the 2nd stone, then 2 units to the 3rd stone, then
2 units to the 4th stone, then 3 units to the 6th stone,
4 units to the 7th stone, and 5 units to the 8th stone.

Example 2:
[0,1,2,3,4,8,9,11]

Return false. There is no way to jump to the last stone as
the gap between the 5th and 6th stone is too large.


3 AC解

public class Solution {
public boolean canCross(int[] stones) {
int k = 0;
return helper(stones, 0, k);
}

private boolean helper(int[] stones, int index, int k) {
//目前已经达到了
if (index == stones.length - 1) return true;
//选择k的步伐,范围k-1到k
for (int i = k - 1; i <= k + 1; i++) {
int nextJump = stones[index] + i;
//看接下来有没有合适的石头可以跳过去,从接下来的位置中查找有没有nextJump的位置,有的话返回石头的编号
int nextPosition = Arrays.binarySearch(stones, index + 1, stones.length, nextJump);
if (nextPosition > 0) {
if (helper(stones, nextPosition, i)) return true;
}
}

return false;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  leetcode frog 青蛙 过河