[Swift]LeetCode351. 安卓解锁模式 $ Android Unlock Patterns

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

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 : 0
10         if n == 0 {return 1}
11         for i in 0..<3
12         {
13             for j in 0..<3
14             {
15                 var I:Int = i1 + i
16                 var J:Int = j1 + j
17                 var used2:Int = used | 1 << (i * 3 + j)
18                 let num1:Int = ((I % 2 == 0) || (J % 2 == 0) || (used2 == 0)) ? 1 : 0
19                 let num2:Int = 1 << (I / 2 * 3 + J / 2)
20                 if used2 > used && (num1 & num2) == 0
21                 {
22                     res += count(m - 1, n - 1, used2, i, j)
23                 }
24             }
25         }
26         return res
27     }
28 }

点击:Playground测试

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

 

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值