|
1 | 1 | package com.fishercoder.solutions;
|
2 | 2 |
|
3 | 3 | /**
|
| 4 | + * 351. Android Unlock Patterns |
| 5 | + * |
4 | 6 | * 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.
|
5 | 7 |
|
6 | 8 | Rules for a valid pattern:
|
|
32 | 34 | */
|
33 | 35 | public class _351 {
|
34 | 36 |
|
35 |
| - private int[][] jumps; |
36 |
| - private boolean[] visited; |
| 37 | + public static class Solution1 { |
| 38 | + private int[][] jumps; |
| 39 | + private boolean[] visited; |
37 | 40 |
|
38 |
| - public int numberOfPatterns(int m, int n) { |
39 |
| - jumps = new int[10][10]; |
40 |
| - jumps[1][3] = jumps[3][1] = 2; |
41 |
| - jumps[4][6] = jumps[6][4] = 5; |
42 |
| - jumps[7][9] = jumps[9][7] = 8; |
43 |
| - jumps[1][7] = jumps[7][1] = 4; |
44 |
| - jumps[2][8] = jumps[8][2] = jumps[1][9] = jumps[9][1] = 5; |
45 |
| - jumps[9][3] = jumps[3][9] = 6; |
46 |
| - jumps[3][7] = jumps[7][3] = 5; |
47 |
| - visited = new boolean[10]; |
48 |
| - int count = 0; |
49 |
| - count += dfs(1, 1, 0, m, n) * 4;//1,3,7,9 are symmetric, so we only need to use 1 to do it once and then multiply the result by 4 |
50 |
| - count += dfs(2, 1, 0, m, n) * 4;//2,4,6,8 are symmetric, so we only need to use 1 to do it once and then multiply the result by 4 |
51 |
| - count += dfs(5, 1, 0, m, n); |
52 |
| - return count; |
53 |
| - } |
54 |
| - |
55 |
| - private int dfs(int num, int len, int count, int m, int n) { |
56 |
| - if (len >= m) { |
57 |
| - count++; |
58 |
| - } |
59 |
| - len++; |
60 |
| - if (len > n) { |
| 41 | + public int numberOfPatterns(int m, int n) { |
| 42 | + jumps = new int[10][10]; |
| 43 | + jumps[1][3] = jumps[3][1] = 2; |
| 44 | + jumps[4][6] = jumps[6][4] = 5; |
| 45 | + jumps[7][9] = jumps[9][7] = 8; |
| 46 | + jumps[1][7] = jumps[7][1] = 4; |
| 47 | + jumps[2][8] = jumps[8][2] = jumps[1][9] = jumps[9][1] = 5; |
| 48 | + jumps[9][3] = jumps[3][9] = 6; |
| 49 | + jumps[3][7] = jumps[7][3] = 5; |
| 50 | + visited = new boolean[10]; |
| 51 | + int count = 0; |
| 52 | + count += dfs(1, 1, 0, m, n) |
| 53 | + * 4;//1,3,7,9 are symmetric, so we only need to use 1 to do it once and then multiply the result by 4 |
| 54 | + count += dfs(2, 1, 0, m, n) |
| 55 | + * 4;//2,4,6,8 are symmetric, so we only need to use 1 to do it once and then multiply the result by 4 |
| 56 | + count += dfs(5, 1, 0, m, n); |
61 | 57 | return count;
|
62 | 58 | }
|
63 |
| - visited[num] = true; |
64 |
| - for (int next = 1; next <= 9; next++) { |
65 |
| - int jump = jumps[num][next]; |
66 |
| - if (!visited[next] && (jump == 0 || visited[jump])) { |
67 |
| - count = dfs(next, len, count, m, n); |
| 59 | + |
| 60 | + private int dfs(int num, int len, int count, int m, int n) { |
| 61 | + if (len >= m) { |
| 62 | + count++; |
68 | 63 | }
|
| 64 | + len++; |
| 65 | + if (len > n) { |
| 66 | + return count; |
| 67 | + } |
| 68 | + visited[num] = true; |
| 69 | + for (int next = 1; next <= 9; next++) { |
| 70 | + int jump = jumps[num][next]; |
| 71 | + if (!visited[next] && (jump == 0 || visited[jump])) { |
| 72 | + count = dfs(next, len, count, m, n); |
| 73 | + } |
| 74 | + } |
| 75 | + visited[num] = false;//backtracking |
| 76 | + return count; |
69 | 77 | }
|
70 |
| - visited[num] = false;//backtracking |
71 |
| - return count; |
72 | 78 | }
|
73 |
| - |
74 | 79 | }
|
0 commit comments