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 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.

思路:
1. 求路径可行性,需要返回 —— 回溯
2. 用过的不能再用—— map
3. 下面是遍历1-9的做法,其实对称性可以看出1,3,7,9是一样的数量,2,4,6,8是一样数量,5单独一个数量。这样只用算三个数量即可。有时间实现。
class Solution {
public:
int numberOfPatterns(int m, int n) {
if (m > || m < || n > || n < ) return ;
vector<int> path;
int ret = ;
set<int> used;
helper(m, n, path, used, ret);
return ret;
}
void helper(int m, int n, vector<int> path, set<int> used, int& ret) {
if (used.size() >= m && used.size() <= n) ret++;
if (used.size() == n) return;
for (int i = ; i <= ; i++) {
if(used.count(i)) continue;
if (path.empty()) {
path.push_back(i);
used.insert(i);
helper(m, n, path, used, ret);
path.pop_back();
used.erase(i);
}
else {
bool cond1 = path.back() % == && i == ;
bool cond2 = path.back() == ;
bool cond3 = path.back() % == && i % == && path.back() != && used.count((path.back() + i) / ) != ;
bool cond4 = path.back() % == && i % == ;
bool cond5 = path.back() % == && (path.back() + i) / == && used.count() != ;
bool cond6 = path.back() % == && (path.back() + i) != ;
if (cond1 || cond2 || cond3 || cond4 || cond5 || cond6) {
path.push_back(i);
used.insert(i);
helper(m, n, path, used, ret);
path.pop_back();
used.erase(i);
}
}
}
}
};
LC 351. 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 ...
- 351. Android Unlock Patterns
这个题我真是做得想打人了卧槽. 题目不难,就是算组合,但是因为是3乘3的键盘,所以只需要从1和2分别开始DFS,结果乘以4,再加上5开始的DFS就行了. 问题是这个傻逼题目的设定是,从1到8不需要经过 ...
- [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: Android Unlock Patterns
Given an Android 3x3 key ≤ m ≤ n ≤ , count the total number of unlock patterns of the Android lock s ...
- 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 ...
- LeetCode All in One 题目讲解汇总(持续更新中...)
终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...
- LeetCode All in One题解汇总(持续更新中...)
突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...
- Leetcode重点 250题-前400 题
删除不常考,面试低频出现题目 删除重复代码题目(例:链表反转206题,代码在234题出现过) 删除过于简单题目(例:100题:Same Tree) 删除题意不同,代码基本相同题目(例:136 & ...
随机推荐
- Oracle和MySql的分页查询区别和PL/SQL的基本概念
Oracle和MySql的分页查询区别: Oracle的分析查询,之前Oracle的分页是使用伪列 ROWNUM 结合子查询实现,mysql的分页更简单,直接使用 LIMIT 关键字就可以实现 ...
- C和指针课后问答题答案翻译
第11章 动态内存分配 1.在你的系统中,你能够声明的静态数组最大长度能达到多少?使用动态内存分配,你最大能够获取的内存块有多大? 英文答案原文: This will vary from system ...
- Kubernetes的yaml文件中command的使用
前面说了init容器initContainers,这主要是为应用容器做前期准备工作的,一般都会用到shell脚本,这就会用到command,这里写写command的用法. command就是将命令在创 ...
- 使用canvas 代码画小猪佩奇
最近不是小猪佩奇很火嘛!!! 前几天 在知乎 看见了别人大佬用python写的 小猪佩奇, 顿时想学 ,可是 自己 没学过python(自己就好爬爬图片,,,,几个月没用 又丢了) 然后 就想画一个 ...
- web开发: css高级与盒模型
一.组合选择器 二.复制选择器优先级 三.伪类选择器 四.盒模型 五.盒模型显示区域 六.盒模型布局 一.组合选择器 <!DOCTYPE html> <html> <he ...
- 屏蔽恶意IP
#!/bin/bash cat /var/log/secure | grep Failed | awk -F " " '{print $11}'| sort| uniq -c| a ...
- golang 中Pointers Vs References
原文: https://spf13.com/post/go-pointers-vs-references/ Pointers Vs References Some languages includin ...
- 【转载】总结:几种生成HTML格式测试报告的方法
总结:几种生成HTML格式测试报告的方法 写自动化测试时,一个很重要的任务就是生成漂亮的测试报告. 1.用junit或testNg时,可以用ant辅助生成html格式: <target name ...
- 数据统计,包括mysql和MongoDB
select ct.dt, COUNT(DISTINCT c.id) from tms_service_customer c, tms_dispatch_details d, (select DIST ...
- 通过LVM备份mysql数据库脚本
#!/bin/bash #******************************************************************** #encoding -*-utf8- ...