[LeetCode] 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:
Input: m = 1, n = 1
Output: 9
Credits:
Special thanks to @elmirap for adding this problem and creating all test cases.
这道题乍一看题目这么长以为是一个设计题,其实不是,这道题还是比较有意思的,起码跟实际结合的比较紧密。这道题说的是安卓机子的解锁方法,有9个数字键,如果密码的长度范围在 [m, n] 之间,问所有的解锁模式共有多少种,注意题目中给出的一些非法的滑动模式。那么先来看一下哪些是非法的,首先1不能直接到3,必须经过2,同理的有4到6,7到9,1到7,2到8,3到9,还有就是对角线必须经过5,例如1到9,3到7等。建立一个二维数组 jumps,用来记录两个数字键之间是否有中间键,然后再用一个一位数组 visited 来记录某个键是否被访问过,然后用递归来解,先对1调用递归函数,在递归函数中,遍历1到9每个数字 next,然后找他们之间是否有 jump 数字,如果 next 没被访问过,并且 jump 为0,或者 jump 被访问过,对 next 调用递归函数。数字1的模式个数算出来后,由于 1,3,7,9 是对称的,所以乘4即可,然后再对数字2调用递归函数,2,4,6,9 也是对称的,再乘4,最后单独对5调用一次,然后把所有的加起来就是最终结果了,参见代码如下:
解法一:
class Solution {
public:
int numberOfPatterns(int m, int n) {
int res = ;
vector<bool> visited(, false);
vector<vector<int>> jumps(, vector<int>(, ));
jumps[][] = jumps[][] = ;
jumps[][] = jumps[][] = ;
jumps[][] = jumps[][] = ;
jumps[][] = jumps[][] = ;
jumps[][] = jumps[][] = ;
jumps[][] = jumps[][] = ;
jumps[][] = jumps[][] = jumps[][] = jumps[][] = ;
res += helper(, , m, n, jumps, visited, ) * ;
res += helper(, , m, n, jumps, visited, ) * ;
res += helper(, , m, n, jumps, visited, );
return res;
}
int helper(int num, int len, int m, int n, vector<vector<int>>& jumps, vector<bool>& visited, int res) {
if (len >= m) ++res;
++len;
if (len > n) return res;
visited[num] = true;
for (int next = ; next <= ; ++next) {
int jump = jumps[num][next];
if (!visited[next] && (jump == || visited[jump])) {
res = helper(next, len, m, n, jumps, visited, res);
}
}
visited[num] = false;
return res;
}
};
下面这种方法很简洁,但是不容易理解,讲解请看这个帖子。其中 used 是一个9位的 mask,每位对应一个数字,如果为1表示存在,0表示不存在,(i1, j1) 是之前的位置,(i, j) 是当前的位置,所以滑动是从 (i1, j1) 到 (i, j),中间点为 ((i1+i)/2, (j1+j)/2),这里的I和J分别为 i1+i 和 j1+j,还没有除以2,所以I和J都是整数。如果 I%2 或者 J%2 不为0,说明中间点的坐标不是整数,即中间点不存在,如果中间点存在,如果中间点被使用了,则这条线也是成立的,可以调用递归,参见代码如下:
解法二:
class Solution {
public:
int numberOfPatterns(int m, int n) {
return count(m, n, , , );
}
int count(int m, int n, int used, int i1, int j1) {
int res = m <= ;
if (!n) return ;
for (int i = ; i < ; ++i) {
for (int j = ; j < ; ++j) {
int I = i1 + i, J = j1 + j, used2 = used | ( << (i * + j));
if (used2 > used && (I % || J % || used2 & ( << (I / * + J / )))) {
res += count(m - , n - , used2, i, j);
}
}
}
return res;
}
};
Github 同步地址:
https://github.com/grandyang/leetcode/issues/351
参考资料:
https://leetcode.com/problems/android-unlock-patterns/
LeetCode All in One 题目讲解汇总(持续更新中...)
[LeetCode] Android Unlock Patterns 安卓解锁模式的更多相关文章
- [LeetCode] 351. Android Unlock Patterns 安卓解锁模式
Given an Android 3x3 key lock screen and two integers m and n, where 1 ≤ m ≤ n ≤ 9, count the total ...
- Leetcode: Android Unlock Patterns
Given an Android 3x3 key ≤ m ≤ n ≤ , count the total number of unlock patterns of the Android lock s ...
- [Swift]LeetCode351. 安卓解锁模式 $ Android Unlock Patterns
Given an Android 3x3 key lock screen and two integers m and n, where 1 ≤ m ≤ n ≤ 9, count the total ...
- Android Unlock Patterns
Given an Android 3x3 key lock screen and two integers m and n, where 1 ≤ m ≤ n ≤ 9, count the total ...
- LC 351. Android Unlock Patterns
Given an Android 3x3 key lock screen and two integers m and n, where 1 ≤ m ≤ n ≤ 9, count the total ...
- 351. Android Unlock Patterns
这个题我真是做得想打人了卧槽. 题目不难,就是算组合,但是因为是3乘3的键盘,所以只需要从1和2分别开始DFS,结果乘以4,再加上5开始的DFS就行了. 问题是这个傻逼题目的设定是,从1到8不需要经过 ...
- 白底黑字!Android浅色状态栏黑色字体模式(另)
小彬什么都想做任重致远 关注 2016.06.30 10:16* 字数 489 阅读 3234评论 3喜欢 12 前言 由于该死不死的设计湿,设计了一套白色状态栏的UI.当然在iOS上可以实现自适应, ...
- Eclipse+ADT+Android SDK 搭建安卓开发环境
Eclipse+ADT+Android SDK 搭建安卓开发环境 要求 必备知识 windows 7 基本操作. 运行环境 windows 7(64位); eclipse-jee-luna-SR2 ...
- Android中的创建型模式总结
共5种,单例模式.工厂方法模式.抽象工厂模式.建造者模式.原型模式 单例模式 定义:确保某一个类的实例只有一个,而且向其他类提供这个实例. 单例模式的使用场景:某个类的创建需要消耗大量资源,new一个 ...
随机推荐
- C#开发微信门户及应用(22)-微信小店的开发和使用
在做企业电子商务方面,微信小店虽然较淘宝天猫等起步较晚,但是作为一个电商平台,这个影响力不容忽视,结合微信的特点和便利,微信小店具有很好的粘合性和广泛的用户基础,因此花费一定的时间,在这方面做深入的研 ...
- 从Google工程师到创业CTO,他的8项理念也许可以帮到你
Lan Langworth是前Google软件工程师.O'Reily作者,现在他是Artillery的co-founder/CTO,致力于把游戏机质量的游戏带进网页浏览器.下文是他从Google离职到 ...
- 了解java注解
类似于下面这样的就是注解 注解可以在类上,成员变量上,方法上等 假如有2个注解是这样的:(其中的Author和Date) 那么这2个注解的定义就是这样的: Author注解: Date注解: 可以看到 ...
- Django模型类Meta元数据详解
转自:https://my.oschina.net/liuyuantao/blog/751337 简介 使用内部的class Meta 定义模型的元数据,例如: from django.db impo ...
- 使用SSH上传安装eclipse
安装eclipse 步骤: 1.上传eclipse tar文件 2.[root@localhost soft]# tar -zxvf eclipse-jee-galileo-SR2-linux-gtk ...
- 三个linux系统共存,修改默认启动
一个mint,一个ubuntu,想要默认启动ubuntu,那么咱们这么来:修改启动顺序,我们需要修改Ubuntu的GRUB配置文件.使用常见的编辑程序如"gedit"就可以很方便 ...
- 《连载 | 物联网框架ServerSuperIO教程》- 3.设备驱动介绍
1.C#跨平台物联网通讯框架ServerSuperIO(SSIO)介绍 <连载 | 物联网框架ServerSuperIO教程>1.4种通讯模式机制. <连载 | 物联网框架Serve ...
- 在一个项目各个子模块中使用Maven的一些通用的准则
1.各个子模块都应该使用相同的groupId(如:com.mvnbook.account); 2.各个子模块如果一起开发和发布,还应该使用相同的版本:version: 3.各个子模块还应该使用一致的前 ...
- ASP.NET中后台数据和前台控件的绑定
关于ASP.NET中后台数据库和前台的数据控件的绑定问题 最近一直在学习个知识点,自己创建了SQL Server数据库表,想在ASP.NET中连接数据库,并把数据库中的数据显示在前台,注意,这里的数据 ...
- ArcGIS Engine开发前基础知识(2)
ArcGIS基本控件简介 ArcGIS Engine控件是一组可视化的开发组件,每个ArcGIS Engine控件都是一个COM组件.这些组件包括MapControl,PageLayoutContro ...