博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Swift]LeetCode351. 安卓解锁模式 $ Android Unlock Patterns
阅读量:4511 次
发布时间:2019-06-08

本文共 2818 字,大约阅读时间需要 9 分钟。

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

➤微信公众号:山青咏芝(shanqingyongzhi)
➤博客园地址:山青咏芝()
➤GitHub地址:
➤原文地址:  
➤如果链接不是山青咏芝的博客园地址,则可能是爬取作者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持作者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★

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.

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:

Given m = 1, n = 1, return 9.

Credits:

Special thanks to @elmirap for adding this problem and creating all test cases.


给定一个android 3x3密钥锁屏和两个整数m和n,其中1≤m≤n≤9,计算android锁屏的解锁模式总数,该解锁模式由最小M个密钥和最大N个密钥组成。 

有效模式的规则:

  1.  每个模式必须连接至少M个键和最多N个键。
  2. 所有键必须是不同的。
  3. 如果连接模式中两个连续键的线通过任何其他键,则其他键必须事先在模式中选择。不允许跳过未选定的键。
  4. 钥匙的使用顺序很重要。

说明:

| 1 | 2 | 3 || 4 | 5 | 6 || 7 | 8 | 9 |

无效移动:4-1-3-6

第1-3行通过模式中未选择的键2。

无效移动:4-1-9-2

第1-9行通过模式中未选择的键5。

有效移动:2-4-1-3-6

第1-3行有效,因为它通过模式中选择的键2

有效移动:6-5-4-1-9-2

第1-9行有效,因为它通过模式中选择的键5。

例子:

给定m=1,n=1,返回9。

信用:

特别感谢@elmirap添加此问题并创建所有测试用例。


Solution:

1 class Solution { 2     func numberOfPatterns(_ m:Int,_ n:Int) -> Int 3     { 4         return count(m, n, 0, 1, 1) 5     } 6      7     func count(_ m:Int,_ n:Int,_ used:Int,_ i1:Int,_ j1:Int) -> Int 8     { 9         var res:Int = m <= 0 ? 1 : 010         if n == 0 {
return 1}11 for i in 0..<312 {13 for j in 0..<314 {15 var I:Int = i1 + i16 var J:Int = j1 + j17 var used2:Int = used | 1 << (i * 3 + j)18 let num1:Int = ((I % 2 == 0) || (J % 2 == 0) || (used2 == 0)) ? 1 : 019 let num2:Int = 1 << (I / 2 * 3 + J / 2)20 if used2 > used && (num1 & num2) == 021 {22 res += count(m - 1, n - 1, used2, i, j)23 }24 }25 }26 return res27 }28 }

点击:

1 var sol = Solution()2 print(sol.numberOfPatterns(1,1))3 //Print 9

 

转载于:https://www.cnblogs.com/strengthen/p/10740374.html

你可能感兴趣的文章
数据库中使用自增量字段与Guid字段作主键的性能对比(补充篇)-----转
查看>>
Thinkphp3.2 cms之角色开发
查看>>
windows批量创建用户
查看>>
jquery 时间运算、格式化的方法扩张
查看>>
WebSocket与消息推送
查看>>
paip.中文 分词 ---paoding 3.1 的使用
查看>>
mysql中 DateDiff(date1, date2)函数的第一个参数需比第二个参数晚
查看>>
mybatis--面向接口编程
查看>>
保护您眼睛视力 请对Win7/Vista/xp作如下设置
查看>>
Oracle 统计信息窗口的相关知识
查看>>
Pollard-Rho大整数拆分模板
查看>>
uva11429(生成随机数 期望)
查看>>
sublime 安装ctags跳转以及跳转快捷键
查看>>
Unity3D的单例模式
查看>>
链表的基本使用
查看>>
大批量数据导出到Excel的实现
查看>>
Cortex M3 - STM32 Developing with GCC Tools
查看>>
基于模糊集理论的一种图像二值化算法
查看>>
mysql_connect 弃用之后使用mysqli替换需要注意事项
查看>>
CentOS7安装MySQL
查看>>