Skip to content

Commit b772914

Browse files
refactor 351
1 parent c874518 commit b772914

File tree

1 file changed

+38
-33
lines changed
  • src/main/java/com/fishercoder/solutions

1 file changed

+38
-33
lines changed
Lines changed: 38 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.fishercoder.solutions;
22

33
/**
4+
* 351. Android Unlock Patterns
5+
*
46
* 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.
57
68
Rules for a valid pattern:
@@ -32,43 +34,46 @@
3234
*/
3335
public class _351 {
3436

35-
private int[][] jumps;
36-
private boolean[] visited;
37+
public static class Solution1 {
38+
private int[][] jumps;
39+
private boolean[] visited;
3740

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);
6157
return count;
6258
}
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++;
6863
}
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;
6977
}
70-
visited[num] = false;//backtracking
71-
return count;
7278
}
73-
7479
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy