LeetCode 351. Android Unlock Patterns(安卓解锁)
2016-05-25 01:34
525 查看
原题网址: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:
Each pattern must connect at least m keys and at most n keys.
All the keys must be distinct.
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.
The order of keys used matters.
Explanation:
Invalid move:
Line 1 - 3 passes through key 2 which had not been selected in the pattern.
Invalid move:
Line 1 - 9 passes through key 5 which had not been selected in the pattern.
Valid move:
Line 1 - 3 is valid because it passes through key 2, which had been selected in the pattern
Valid move:
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.
方法:深度优先搜索。
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:
Each pattern must connect at least m keys and at most n keys.
All the keys must be distinct.
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.
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.
方法:深度优先搜索。
public class Solution { private int patterns; private boolean valid(boolean[] keypad, int from, int to) { if (from==to) return false; int i=Math.min(from, to), j=Math.max(from,to); if ((i==1 && j==9) || (i==3 && j==7)) return keypad[5] && !keypad[to]; if ((i==1 || i==4 || i==7) && i+2==j) return keypad[i+1] && !keypad[to]; if (i<=3 && i+6==j) return keypad[i+3] && !keypad[to]; return !keypad[to]; } private void find(boolean[] keypad, int from, int step, int m, int n) { if (step == n) { patterns ++; return; } if (step >= m) patterns ++; for(int i=1; i<=9; i++) { if (valid(keypad, from, i)) { keypad[i] = true; find(keypad, i, step+1, m, n); keypad[i] = false; } } } public int numberOfPatterns(int m, int n) { boolean[] keypad = new boolean[10]; for(int i=1; i<=9; i++) { keypad[i] = true; find(keypad, i, 1, m, n); keypad[i] = false; } return patterns; } }
相关文章推荐
- Android 简单的条形颜色选择器
- This Handler class should be static or leaks may occur (anonymous android.os.Handler)
- mac开发android 安卓真机调试解决方案
- mac开发android 安卓真机调试解决方案
- Android Drawable - Scale Drawable使用详解(附图)
- Android防止多次点击
- Android之手势翻页
- 小技巧:如何知道当前界面为哪个Activity
- <Android 基础(二)> BroadcastReceiver
- <Android 基础(二)> BroadcastReceiver
- Android打造属于自己的数据库操作类。
- 【自用】Android 关于surfaceview调用宿主Activity中的方法
- Ubuntu Android 开发(二) Android SDK 安装
- Ubuntu Android 开发(三) 使用SDK更新SDK 镜像
- Android缓存超过有效时间,则重新请求数据
- Android Fragment的使用
- Android 颜色设置
- Intellij IDEA + Android SDK + Genymotion Emulator
- “哎哟!蛮吊的” 之 Android GestureDetector详解
- Android 入门项目NoteBook