leetcode-935-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:
1 | Input: 1 |
Example 2:
1 | Input: 2 |
Example 3:
1 | Input: 3 |
Note:
1 <= N <= 5000
分析
思路1: 动态规划
思路2: 使用无向图的性质 https://leetcode.com/problems/knight-dialer/discuss/189252/O(logN)
代码
1 | class Solution { |
Author: Hatton.Liu
Link: http://hattonl.github.io/2020/03/28/leetcode-935/
License: 知识共享署名-非商业性使用 4.0 国际许可协议