原题链接在这里:https://leetcode.com/problems/knight-dialer/

题目:

A chess knight can move as indicated in the chess diagram below:

 .           

This time, we place our chess knight on any numbered key of a phone pad (indicated above), and the knight makes N-1 hops.  Each hop must be from one key to another numbered key.

Each time it lands on a key (including the initial placement of the knight), it presses the number of that key, pressing N digits total.

How many distinct numbers can you dial in this manner?

Since the answer may be large, output the answer modulo 10^9 + 7.

Example 1:

Input: 1
Output: 10

Example 2:

Input: 2
Output: 20

Example 3:

Input: 3
Output: 46

Note:

  • 1 <= N <= 5000

题解:

The question asks distinct numbers could dial.

It is actually the sum of ways jump ending at each cell.

Cell 1 could jump to cell 6 and 8. Thus accumlate the current ways count to next ways at 6 and 8.

Eventually, get all the sum.

Time Complexity: O(N).

Space: O(1).

AC Java:

 class Solution {
public int knightDialer(int N) {
if(N == 0){
return 0;
} if(N == 1){
return 10;
} int M = 1000000007;
long [] cur = new long[10];
Arrays.fill(cur, 1);
for(int k = 2; k<=N; k++){
long [] next = new long[10]; next[1] = (cur[6]+cur[8])%M;
next[2] = (cur[7]+cur[9])%M;
next[3] = (cur[4]+cur[8])%M;
next[4] = (cur[3]+cur[9]+cur[0])%M;
next[5] = 0;
next[6] = (cur[1]+cur[7]+cur[0])%M;
next[7] = (cur[2]+cur[6])%M;
next[8] = (cur[1]+cur[3])%M;
next[9] = (cur[2]+cur[4])%M;
next[0] = (cur[4]+cur[6])%M; cur = next;
} long res = 0;
for(int i = 0; i<10; i++){
res = (res + cur[i]) % M;
}
return (int)res;
}
}

类似Number of Ways to Stay in the Same Place After Some Steps.

LeetCode 935. Knight Dialer的更多相关文章

  1. [LeetCode] 935. Knight Dialer 骑士拨号器

    A chess knight can move as indicated in the chess diagram below:  .            This time, we place o ...

  2. 【LeetCode】935. Knight Dialer 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 动态规划TLE 空间换时间,利用对称性 优化空间复杂 ...

  3. 【leetcode】935. Knight Dialer

    题目如下: A chess knight can move as indicated in the chess diagram below:  .            This time, we p ...

  4. 935. Knight Dialer

    A chess knight can move as indicated in the chess diagram below:  .            This time, we place o ...

  5. 109th LeetCode Weekly Contest Knight Dialer

    A chess knight can move as indicated in the chess diagram below:  .            This time, we place o ...

  6. [Swift]LeetCode935. 骑士拨号器 | Knight Dialer

    A chess knight can move as indicated in the chess diagram below:  .            This time, we place o ...

  7. LeetCode 688. Knight Probability in Chessboard

    原题链接在这里:https://leetcode.com/problems/knight-probability-in-chessboard/description/ 题目: On an NxN ch ...

  8. LeetCode——688. Knight Probability in Chessboard

    一.题目链接:https://leetcode.com/problems/knight-probability-in-chessboard/ 二.题目大意: 给定一个N*N的棋盘和一个初始坐标值(r, ...

  9. leetcode动态规划题目总结

    Hello everyone, I am a Chinese noob programmer. I have practiced questions on leetcode.com for 2 yea ...

随机推荐

  1. Flask源码之:路由加载

    路由加载整体思路: 1. 将 url = /index  和  methods = [GET,POST]  和 endpoint = "index"封装到Rule对象 2. 将Ru ...

  2. 54 容器(九)——HashSet

    HashSet的特点: 无序,不可重复. HashSet实现自Set,而Set继承自Collection,在日常使用中,我们都是以Set引用指向HashSet对象的方式. 所以,Set中的方法是我们主 ...

  3. golang 之 jwt-go

    主要针对jwt-go快速生成token.和如何取进行介绍,具体详情还请查看 github.com/dgrijalva/jwt-go 生成token package main import ( &quo ...

  4. pandas-06 Series和Dataframe的排序操作

    pandas-06 Series和Dataframe的排序操作 对pandas中的Series和Dataframe进行排序,主要使用sort_values()和sort_index(). DataFr ...

  5. 【强烈推荐】ok-admin 一个好看又好用的后台模版!!!

    ok-admin 一个很赞的,扁平化风格的,响应式布局的后台管理模版,旨为后端程序员减压! 目前一共有两个版本:ok-admin v1.0和ok-admin v2.0可自由选择! 源码地址:https ...

  6. JQuery+Bootstrap 自定义全屏Loading插件

    /** * 自定义Loading插件 * @param {Object} config * { * content[加载显示文本], * time[自动关闭等待时间(ms)] * } * @param ...

  7. State Design Pattern

    注: 转载自 https://www.geeksforgeeks.org/state-design-pattern/  [以便查阅,非原创] State Design Pattern State pa ...

  8. Eclipse不支持tomcat8_compiler编译级别选不到1.8

    -------------------------------------------------------------- Eclipse不支持tomcat8 如果你要使用tomcat8.0+版本的 ...

  9. Spring 重定向(Redirect)指南

    原文:Hacking the IntegerCache in Java 9? 链接:https://dzone.com/articles/hacking-the-integercache-in-jav ...

  10. HashMap不足性分析

    不足性: 1.缺陷就在于其高度依赖hash算法,如果key是自定义类,你得自己重写hashcode方法,写hash算法. 而且hashmap要求,存入时的hashcode什么样,之后就不能在变更,如果 ...