LeetCode 351. Android Unlock Patterns(安卓解锁)
来源:互联网 发布:sql团体培训 编辑:程序博客网 时间:2024/06/04 17:58
原题网址: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:
| 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; }}
- LeetCode 351. Android Unlock Patterns(安卓解锁)
- [LeetCode] Android Unlock Patterns 安卓解锁模式
- [leetcode] 351. Android Unlock Patterns 解题报告
- (未完成!)LeetCode 351. Android Unlock Patterns
- [Leetcode] 351. Android Unlock Patterns 解题报告
- 351. Android Unlock Patterns
- Android : 使用 fastboot 解锁安卓手机
- oracle 解锁 unlock
- oracle 解锁 unlock
- PDF 解锁(unlock)
- (求助)安卓指纹解锁功能
- htc 手机解锁 unlock bootloader
- Root与Unlock Bootloader(解锁)
- android 安卓 捕获屏幕变黑,变亮,解锁事件
- Android 高仿 IOS7 IPhone 解锁 Slide To Unlock 附源码
- Android 高仿 IOS7 IPhone 解锁 Slide To Unlock 附源码
- 解锁android设备遇到错误bootloader Check 'Allow OEM Unlock' in Developer Options
- 安卓图案解锁插件
- Stream常用操作min,reduce
- LeetCode 350. Intersection of Two Arrays II(数组交集)
- NLP相关
- 勾股定理每日一证 2
- Stream对数字处理
- LeetCode 351. Android Unlock Patterns(安卓解锁)
- 【设计模式】创建型模式之生成器Builder
- JavaScript概览(一)--基本语法
- [BZOJ1009][HNOI2008]GT考试(AC自动机+dp+矩阵优化)
- linux 学习笔记(三):open、creat、close 函数的使用,文件的创建、打开与关闭
- Karatsuba algorithm C++ implementation
- Laravel 完整开源项目大全
- linux 系统
- vector::erase方法,返回值是更新后的容器的被删除元素的后一个元素的迭代器,如果需要指向删除元素的前面一个元素的迭代器就要-1