【LeetCode】351.Android Unlock Patterns(Medium)解题报告
2018-01-11 21:18
501 查看
【LeetCode】351.Android Unlock Patterns(Medium)解题报告
题目地址:https://leetcode.com/problems/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.
Solution(有更简单解法):
Date:2018年1月11日
题目地址:https://leetcode.com/problems/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.
Explanation: 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.
Solution(有更简单解法):
时间和空间复杂度不清楚 public class AnaroidUnlockPatterns{ public int numberOfPatterns(int m,int n){ int[][] skip = new int[10][10]; skip[1][3] = skip[3][1] = 2; skip[1][7] = skip[7][1] = 4; skip[3][9] = skip[9][3] = 6; skip[7][9] = skip[9][7] = 8; skip[1][9] = skip[9][1] = skip[2][8] = skip[8][2] = skip[3][7] = skip[7][3] = skip[4][6] = skip[6][4] = 5; boolean[] isVisited = new boolean[10]; int res = 0; for(int i=m ; i<=n ; i++){ res += DFS(isVisited,skip,1,i-1)*4; res += DFS(isVisited,skip,2,i-1)*4; res += DFS(isVisited,skip,5,i-1); } return res; } public int DFS(boolean[] isVisited,int[][] skip,int cur,int remain){ //现在位置,剩下几个数要走 if(remain<0) return 0; if(remain==0) return 1; isVisited[cur] = true; int res = 0; for(int i=1 ; i<=9 ; i++){ if(!isVisited[i] && (skip[cur][i]==0 || isVisited[skip[cur][i]])){ res += DFS(isVisited,skip,i,remain-1); } } isVisited[cur] = false; return res; } }
Date:2018年1月11日
相关文章推荐
- [leetcode] 351. Android Unlock Patterns 解题报告
- [Leetcode] 351. Android Unlock Patterns 解题报告
- 【LeetCode】117.Populating Next Right Pointers in Each Node II(Medium)解题报告
- LeetCode解题报告 199. Binary Tree Right Side View [medium]
- 【LeetCode】435.Non-overlapping Intervals(Medium)解题报告
- LeetCode解题报告 322. Coin Change [medium]
- 【LeetCode】46.Permutations(Medium)解题报告
- LeetCode解题报告 279. Perfect Squares [medium]
- 【LeetCode】648.Replace Words(Medium)解题报告
- 【LeetCode】228.Summary Ranges(Medium)解题报告
- 【LeetCode】406.Queue Reconstruction by Height(Medium)解题报告
- 【LeetCode】56.Merge Intervals(Medium)解题报告
- 【LeetCode】280.Wiggle Sort (Medium)(带锁题)解题报告
- 【LeetCode】199.Binary Tree Right Side View(Medium)解题报告
- LeetCode解题报告 309. Best Time to Buy and Sell Stock with Cooldown[medium]
- 【LeetCode】413.Arithmetic Slices(Medium)解题报告
- 【LeetCode】109.Convert Sorted List to Binary Search Tree(Medium)解题报告
- LeetCode解题报告 338. Counting Bits [medium]
- LeetCode解题报告 406. Queue Reconstruction by Height [medium]
- LeetCode解题报告 394. Decode String [medium]