ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2019-04-20 11:50:13  阅读:373  来源: 互联网

标签:Patterns Int LeetCode351 安卓 selected keys through key pattern


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

 

标签:Patterns,Int,LeetCode351,安卓,selected,keys,through,key,pattern
来源: https://www.cnblogs.com/strengthen/p/10740374.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有