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 & ...
随机推荐
- 8.Spring整合Hibernate_2_声明式的事务管理(Annotation的方式)
声明式的事务管理(AOP的主要用途之一) (Annotation的方式) 1.加入annotation.xsd 2.加入txManager bean 3.<tx:annotation-drive ...
- List集合复制
方法一: public static void main(String[] args) { // TODO Auto-generated method stub List<String> ...
- hadoop中hive常用的交互式操作
hive的帮助命令: [hadoop@master tmp]$ hive -help usage: hive -d,--define <key=value> Variable substi ...
- 织梦后台系统设置在PHP5.4环境中不能保存中文参数的解决方法
在没用PHP5.4的环境做Dede后台的时候,织梦58一直没有遇到这个问题,昨天上传一个新的模版到空间去测试发现后台的系统基本参数设置中所有的中文内容都无法保存,关于这个问题,其实以前也听说过,知识一 ...
- HDU 5876 补图最短路
开两个集合,一个存储当前顶点可以到达的点,另一个存储当前顶点不能到达的点.如果可以到达,那肯定由该顶点到达是最短的,如果不能,那就留着下一次再判. #include<bits/stdc++.h& ...
- idea目录因包名而未合并、逐级显示的问题
如图包名里含有多个.,从而导致一个加载时出现了好多层.. 只要右键java目录,转换为source root就行.
- anguar @input绑定的属性变化
Intercept @Input property change in Angular Bharat TiwariFollow Mar 15, 2018 this post is good for A ...
- shell编程expr表达式----传智播客的书linux编程基础中出现的问题
首先声明:本人是传智播客的粉丝,拥有他出的多本编程书籍,此文绝无诋毁抹黑之意. 但在linux系统编程第88页给出的while循环范例中,代码运行无法得到预期结果 原代码如下 #!/bin/sh su ...
- 引爆炸弹——DFS&&联通块
题目 链接 在一个$n \times m$方格地图上,某些方格上放置着炸弹.手动引爆一个炸弹以后,炸弹会把炸弹所在的行和列上的所有炸弹引爆,被引爆的炸弹又能引爆其他炸弹,这样连锁下去. 现在为了引爆地 ...
- React组件式编程Demo-用户的增删改查
1.项目结构 项目是基于webpack4, 参考 创建基本的webpack4.x项目 2.页面效果 初始化效果