Leetcode - Frog Jump

My code:

public class Solution {
    public boolean canCross(int[] stones) {
        if (stones == null || stones.length == 0) {
            return false;
        }
        
        Map<Integer, Set<Integer>> map = new HashMap<Integer, Set<Integer>>();
        map.put(0, new HashSet<Integer>());
        map.get(0).add(1);
        for (int i = 1; i < stones.length; i++) {
            map.put(stones[i], new HashSet<Integer>());
        }
        
        for (int i = 0; i < stones.length; i++) {
            int base = stones[i];
            for (Integer step : map.get(stones[i])) {
                int reach = base + step;
                if (reach == stones[stones.length - 1]) {
                    return true;
                }
                if (map.containsKey(reach)) {
                    Set<Integer> set = map.get(reach);
                    set.add(step);
                    set.add(step + 1);
                    if (step - 1 > 0) {
                        set.add(step - 1);
                    }
                }
            }
        }
        
        return false;
    }
}

reference:
https://discuss.leetcode.com/topic/59903/very-easy-to-understand-java-solution-with-explanations/2

没想出来。
感觉已经没有思考新题的能力了。。。
悲哀

Anyway, Good luck, Richardo! -- 10/21/2016

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 9,968评论 0 23
  • 背景 一年多以前我在知乎上答了有关LeetCode的问题, 分享了一些自己做题目的经验。 张土汪:刷leetcod...
    土汪阅读 12,779评论 0 33
  • 文笔不好,大家见谅,此文只是自己对现在生活的一时感慨而已。 我现在的生活过的平淡顺遂,虽有些沟沟坎坎,有时亦闹闹腾...
    JulyH阅读 423评论 0 0
  • 路线:家-若尔盖-西宁-青海湖-甘肃张掖-祁连县-宁夏中卫-银川-延安-西安-阆中古城- 家 景点:花湖 青海湖 ...
    她与他梦阅读 262评论 0 0
  • 11月21日,天空阴沉,有北风。 马小河背着帆布背包,穿着一件有些过长的海军衫,大步走在一片丛林石径上。 如果沿着...
    Kris阅读 577评论 0 2