Leetcode: Android Unlock Patterns
Given an Android 3x3 key lock screen and two integers m and n, where ≤ m ≤ n ≤ , 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 - passes through key which had not been selected in the pattern. Invalid move: - - -
Line - passes through key which had not been selected in the pattern. Valid move: - - - -
Line - is valid because it passes through key , which had been selected in the pattern Valid move: - - - - -
Line - is valid because it passes through key , which had been selected in the pattern. Example:
Given m = , n = , return .
我自己的backtracking做法
最开始把cur设置为一个dummy value 0
public class Solution {
int num = 0;
public int numberOfPatterns(int m, int n) {
for (int len=m; len<=n; len++) {
HashSet<Integer> visited = new HashSet<Integer>();
count(visited, 0, 0, len);
}
return num;
} public void count(HashSet<Integer> visited, int cur, int pos, int len) {
if (pos == len) {
num++;
return;
}
for (int elem=1; elem<=9; elem++) {
if (visited.contains(elem)) continue;
if (cur == 1) {
if (elem==3 && !visited.contains(2)) continue;
if (elem==7 && !visited.contains(4)) continue;
if (elem==9 && !visited.contains(5)) continue;
}
else if (cur == 2) {
if (elem == 8 && !visited.contains(5)) continue;
}
else if (cur == 3) {
if (elem==1 && !visited.contains(2)) continue;
if (elem==7 && !visited.contains(5)) continue;
if (elem==9 && !visited.contains(6)) continue;
}
else if (cur == 4) {
if (elem == 6 && !visited.contains(5)) continue;
}
else if (cur == 6) {
if (elem == 4 && !visited.contains(5)) continue;
}
else if (cur == 7) {
if (elem==1 && !visited.contains(4)) continue;
if (elem==3 && !visited.contains(5)) continue;
if (elem==9 && !visited.contains(8)) continue;
}
else if (cur == 8) {
if (elem==2 && !visited.contains(5)) continue;
}
else if (cur == 9) {
if (elem==1 && !visited.contains(5)) continue;
if (elem==3 && !visited.contains(6)) continue;
if (elem==7 && !visited.contains(8)) continue;
}
visited.add(elem);
count(visited, elem, pos+1, len);
visited.remove(elem);
}
}
}
最好的DFS with Optimization beat 97%: https://discuss.leetcode.com/topic/46260/java-dfs-solution-with-clear-explanations-and-optimization-beats-97-61-12ms
Use an matrix to store the corssed number for each possible move and use DFS to find out all patterns.
The optimization idea is that 1,3,7,9 are symmetric, 2,4,6,8 are also symmetric. Hence we only calculate one among each group and multiply by 4.
public class Solution {
// cur: the current position
// remain: the steps remaining
int DFS(boolean vis[], int[][] skip, int cur, int remain) {
if(remain < 0) return 0;
if(remain == 0) return 1;
vis[cur] = true;
int rst = 0;
for(int i = 1; i <= 9; ++i) {
// If vis[i] is not visited and (two numbers are adjacent or skip number is already visited)
if(!vis[i] && (skip[cur][i] == 0 || (vis[skip[cur][i]]))) {
rst += DFS(vis, skip, i, remain - 1);
}
}
vis[cur] = false;
return rst;
} public int numberOfPatterns(int m, int n) {
// Skip array represents number to skip between two pairs
int skip[][] = new int[10][10];
skip[1][3] = skip[3][1] = 2;
skip[1][7] = skip[7][1] = 4;
skip[3][9] = skip[9][3] = 6;
skip[7][9] = skip[9][7] = 8;
skip[1][9] = skip[9][1] = skip[2][8] = skip[8][2] = skip[3][7] = skip[7][3] = skip[4][6] = skip[6][4] = 5;
boolean vis[] = new boolean[10];
int rst = 0;
// DFS search each length from m to n
for(int i = m; i <= n; ++i) {
rst += DFS(vis, skip, 1, i - 1) * 4; // 1, 3, 7, 9 are symmetric
rst += DFS(vis, skip, 2, i - 1) * 4; // 2, 4, 6, 8 are symmetric
rst += DFS(vis, skip, 5, i - 1); //
}
return rst;
}
}
Leetcode: Android Unlock Patterns的更多相关文章
- [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 ...
- [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 ...
- [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不需要经过 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
随机推荐
- Codeforces 307 div2 E.GukiZ and GukiZiana 分块
time limit per test 10 seconds memory limit per test 256 megabytes input standard input output stand ...
- TTrayIcon用法
TTrayIcon用法 self.trycn1.Icon:=Application.Icon; Self.trycn1.Hint:=self.Caption; self.trycn1.Visible: ...
- inline-block 兼容性
inline-block 兼容性 通常网页模板都需要动态添加或删除内容,在做网页导航的时候,需要nav中的ul能够居中并实现自适应拓展,如果ul是固定的长度很好实现居中,只需要设置margin:0 a ...
- 安装dede显示dir的解决办法
1.遇见安装页面出现dir 2.你这个肯定是已经安装过了的程序,把install文件夹下的index.html删掉,,你再看看install文件夹下有没有install_lock.txt 和index ...
- 【Ajax 基础学习】
http://www.cnblogs.com/guduoduo/p/3681296.html 今天简单的学习了 Ajax 的基础知识,总结在这里.部分代码不是原创,特此说明. [Ajax 简介] AJ ...
- CSS3绘制404页面
标题有点噱了...最近在做一个交通有关的项目, 想做一个类似标志牌的404, 所以就有了这个. 只用的CSS3中的旋转, 效果如下 上代码: <!DOCTYPE html> <htm ...
- 如何将U盘内文件拷入VMware Linux CentOS6.5虚拟机
之前在Linux CentOS下安装Oracle这篇随笔中我提到要将下载到的安装文件解压缩 那么,问题来了! 如何把下载到的文件拷入虚拟机中呢? 我是这样做的: 1.将下载到的文件拷入U盘 2.以ro ...
- Java实现验证码制作之一Kaptcha验证码
Kaptcha验证码 是google提供的验证码插件,使用起来相对简单,设置的干扰线以及字体扭曲不易让其他人读取破解. 这里我们需要 导入一个 kaptcha-2.3.jar 下载地址:http:/ ...
- NOIP2014初赛分数线及金华上线名单
NOIP2014初赛分数线及金华上线名单 分数线:提高组81.5,普及组93 这分数线还能再高些吗?悲催的浙江. 金华上线普及组名单: 地市 姓名 学校 年级 参赛语种 成绩 金华 成浩鹏 稠州丹溪校 ...
- springboot+redis
上篇整合了DB层,现在开始整合缓存层,使用redis. springboot驱动注解,使用spring注入JedisPool便可封装自己的redis工具类. package hello.configu ...