[leetcode] 351. Android Unlock Patterns 解题报告

59 篇文章 0 订阅

题目链接: https://leetcode.com/problems/android-unlock-patterns/

Given an Android 3x3 key lock screen and two integers m and n, where 1 ≤ m ≤ n ≤ 9, count the total number of unlock patterns of the Android lock screen, which consist of minimum of m keys and maximum n keys.

Rules for a valid pattern:

  1. Each pattern must connect at least m keys and at most n keys.
  2. All the keys must be distinct.
  3. If the line connecting two consecutive keys in the pattern passes through any other keys, the other keys must have previously selected in the pattern. No jumps through non selected key is allowed.
  4. The order of keys used matters.

Explanation:

| 1 | 2 | 3 |
| 4 | 5 | 6 |
| 7 | 8 | 9 |

Invalid move: 4 - 1 - 3 - 6 
Line 1 - 3 passes through key 2 which had not been selected in the pattern.

Invalid move: 4 - 1 - 9 - 2
Line 1 - 9 passes through key 5 which had not been selected in the pattern.

Valid move: 2 - 4 - 1 - 3 - 6
Line 1 - 3 is valid because it passes through key 2, which had been selected in the pattern

Valid move: 6 - 5 - 4 - 1 - 9 - 2
Line 1 - 9 is valid because it passes through key 5, which had been selected in the pattern.

Example:
Given m = 1, n = 1, return 9.


思路: 看到这题思路一片混乱, 这他妈什么东西! google都问些什么乱七八糟的问题! 水太深啊! 不过归根揭底这题还是DFS的题目, 而我最擅长的就是DFS(不包括这题, 我懵了!).

这题是给定了一个最小的key个数和最大key个数, 问有多少种合法的手势, 可以看到9个数字其中1, 3, 7, 9处在角上, 是对称的. 而2, 4, 6, 8处在边中间, 也是对称的. 5处在最中间, 只有这一个. 也就是说9个数字只要求出三个位置的数量就行了. 其中还有一个条件就是如果两个数字经过了一个数字, 那么这个数字必须之前访问过, 因此我们可以设置一个hash表来存储两个数字的中间数字. 这样当从一个数字去访问下一个数字的时候还要看中间数字是不是被访问过了. 然后剩下的就是正常的DFS了.

代码如下: 

class Solution {
public:
    int DFS(int m, int n, int len, int num)
    {
        int cnt = 0;
        if(len >= m) cnt++;
        if(++len > n) return cnt;
        visited[num] = true;
        for(int i = 1; i<= 9; i++)
            if(!visited[i] && visited[hash[num][i]])
                cnt += DFS(m, n, len, i);
        visited[num] = false;
        return cnt;
    }
    
    int numberOfPatterns(int m, int n) {
        if(m < 1 || n < 1) return 0;
        visited.resize(10, false);
        visited[0] = true;
        hash.resize(10, vector<int>(10, 0));
        hash[1][3] = hash[3][1] = 2;
        hash[1][7] = hash[7][1] = 4;
        hash[3][9] = hash[9][3] = 6;
        hash[7][9] = hash[9][7] = 8;
        hash[2][8] = hash[8][2] = hash[4][6] = hash[6][4] = 5;
        hash[1][9] = hash[9][1] = hash[3][7] = hash[7][3] = 5;
        return DFS(m, n, 1, 1)*4 + DFS(m, n, 1, 2)*4 + DFS(m, n, 1, 5);
    }
private:
    vector<bool> visited;
    vector<vector<int>> hash;
};
参考: https://leetcode.com/discuss/104688/simple-and-concise-java-solution-in-69ms

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值