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:

  1. Each pattern must connect at least m keys and at most n keys.
  2. All the keys must be distinct.
  3. 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.
  4. 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的更多相关文章

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

  2. 351. Android Unlock Patterns

    这个题我真是做得想打人了卧槽. 题目不难,就是算组合,但是因为是3乘3的键盘,所以只需要从1和2分别开始DFS,结果乘以4,再加上5开始的DFS就行了. 问题是这个傻逼题目的设定是,从1到8不需要经过 ...

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

  4. Leetcode: Android Unlock Patterns

    Given an Android 3x3 key ≤ m ≤ n ≤ , count the total number of unlock patterns of the Android lock s ...

  5. Android Unlock Patterns

    Given an Android 3x3 key lock screen and two integers m and n, where 1 ≤ m ≤ n ≤ 9, count the total ...

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

  7. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  8. LeetCode All in One题解汇总(持续更新中...)

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

  9. Leetcode重点 250题-前400 题

    删除不常考,面试低频出现题目 删除重复代码题目(例:链表反转206题,代码在234题出现过) 删除过于简单题目(例:100题:Same Tree) 删除题意不同,代码基本相同题目(例:136 & ...

随机推荐

  1. STM32L1xx——ADC(中断/DMA)样例代码

    此代码欲实现的功能是:使用中断或者DMA的方式采集滑动变阻器采集到的电压值,使用单ADC单通道采样! (由于不是直接需要电压,所以转换函数我就没列出来,可根据自身需要去网上查到转换的函数.) 代码结构 ...

  2. delphi xe5 fastreport4.14 中文很多时换行不正确

    用一般的frxMEMOview 中文换行是瞎换,缺少数据,换成frxrichview 即可, frxrichview 使用注意点 1).Delphi中文很多时换行不正确 2).要在窗体上拖一个frxr ...

  3. Physical Education Lessons CodeForces - 915E (动态开点线段树)

    Physical Education Lessons CodeForces - 915E This year Alex has finished school, and now he is a fir ...

  4. ubuntu卸载/更新Cmake

    CMake安装或CMake Error at CMakeLists 发生情景: 使用cmake命令安装软件时,报如下错误: CMake Error at CMakeLists.txt:4 (CMAKE ...

  5. Redis五种数据结构(二)

    Redis数据结构 Redis数据结构介绍 Redis是一种高级的key-value的存储系统,其中value支持五种数据类型. 字符串(String) 哈希(hash) 字符串列表(list) 字符 ...

  6. 数据结构系列文章之队列 FIFO

    转载自https://mp.weixin.qq.com/s/ILgdI7JUBsiATFICyyDQ9w Osprey  鱼鹰谈单片机 3月2日 预计阅读时间: 6 分钟 这里的 FIFO 是先入先出 ...

  7. 洛谷P1417 烹调方案【dp】

    题目:https://www.luogu.org/problemnew/show/P1417 题意: 一道菜有$a,b,c$三个值.烧一道菜的时间是$c$.得到的价值是,$a-t*b$其中$t$是菜完 ...

  8. 使用EntityFramework6连接MySql数据库-db first方式

    准备工具: VS2013.MySQL For VisualStudio 1.1.4.Connector/Net 6.8.3 程序包管理器执行命令: Install-Package EntityFram ...

  9. OFDM留空中央直流子载波目的及原理

    目的: 降低峰均比! 原理: IDFT公式: 直流分量k接近0,公式近似于对X(k)进行累加,因此直流分量会产生较大的信号能量,造成严重的峰均比. 详细内容可参考: https://dwz.cn/Zl ...

  10. PHP mysqli_get_server_info() 函数

    定义和用法 mysqli_get_server_info() 函数返回 MySQL 服务器版本. 语法 mysqli_get_server_info(connection); 实例 返回 MySQL ...