G面经prepare: Android Phone Unlock Pattern

1 2 3
4 5 6
7 8 9
只有中间没有其他键的两个键才能相连,比如1可以连 2 4 5 6 8 但不能连 3 7 9
但是如果中间键被使用了,那就可以连,比如5已经被使用了,那1就可以连9
每个键只能用一次,给定一个长度L,求问有多少unique path with length L

Backtracking: 我的code不光可以知道数目,还可以打印所有Pattern

 1 package AndroidUnlockPattern;
 2 import java.util.*;
 3
 4 public class Solution {
 5     int res = 0;
 6     HashSet<Integer> set = new HashSet<Integer>();
 7     static ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
 8     ArrayList<Integer> path = new ArrayList<Integer>();
 9
10     public int calculate(int L) {
11         for (int i=1; i<=9; i++) {
12             set.add(i);
13         }
14         HashSet<Integer> visited = new HashSet<Integer>();
15         helper(0, 0, L, visited);
16         return res;
17     }
18
19     public void helper(int cur, int pos, int L, HashSet<Integer> visited) {
20         if (pos == L) {
21             res++;
22             result.add(new ArrayList<Integer>(path));
23             return;
24         }
25         for (int elem : set) {
26             if (visited.contains(elem)) continue;
27             if (cur == 1) {
28                 if (elem==3 && !visited.contains(2)) continue;
29                 if (elem==7 && !visited.contains(4)) continue;
30                 if (elem==9 && !visited.contains(5)) continue;
31             }
32             else if (cur == 2) {
33                 if (elem==8 && !visited.contains(5)) continue;
34             }
35             else if (cur == 3) {
36                 if (elem==1 && !visited.contains(2)) continue;
37                 if (elem==7 && !visited.contains(5)) continue;
38                 if (elem==9 && !visited.contains(6)) continue;
39             }
40             else if (cur == 4) {
41                 if (elem == 6 && !visited.contains(5)) continue;
42             }
43             else if (cur == 6) {
44                 if (elem == 4 && !visited.contains(5)) continue;
45             }
46             else if (cur == 7) {
47                 if (elem==1 && !visited.contains(4)) continue;
48                 if (elem==3 && !visited.contains(5)) continue;
49                 if (elem==9 && !visited.contains(9)) continue;
50             }
51             else if (cur == 8) {
52                 if (elem==2 && !visited.contains(5)) continue;
53             }
54             else if (cur == 9) {
55                 if (elem==1 && !visited.contains(5)) continue;
56                 if (elem==3 && !visited.contains(6)) continue;
57                 if (elem==7 && !visited.contains(8)) continue;
58             }
59             visited.add(elem);
60             path.add(elem);
61             helper(elem, pos+1, L, visited);
62             visited.remove(elem);
63             path.remove(path.size()-1);
64         }
65     }
66
67
68     /**
69      * @param args
70      */
71     public static void main(String[] args) {
72         // TODO Auto-generated method stub
73         Solution sol = new Solution();
74         int res = sol.calculate(3);
75         System.out.println(res);
76         for (ArrayList<Integer> each : result) {
77             System.out.println(each);
78         }
79     }
80
81 }
时间: 2024-10-11 17:04:59

G面经prepare: Android Phone Unlock Pattern的相关文章

Deal with Android phones with pattern lock on

Yesterday my colleague asked me for help...She has two android phones , one is hTC and the other is samsung. One day she changed the pattern lock and later she forgot...she tried all possible combination but in vain.. I check her phones and find them

G面经Prepare: Print Zigzag Matrix

For instance, give row = 4, col = 5, print matrix in zigzag order like: [1, 8, 9, 16, 17] [2, 7, 10, 15, 18] [3, 6, 11, 14, 19] [4, 5, 12, 13, 20] 1 package GooglePhone; 2 3 import java.util.Arrays; 4 5 public class PrintMatrix { 6 7 static void prin

G面经Prepare: Longest All One Substring

give a string, all 1 or 0, we can flip a 0 to 1, find the longest 1 substring after the flipping 这是一个简单版本of LC 424 Longest Repeating Character Replacement 又是Window, 又是Two Pointers Window还是采用每次都try to update left使得window valid, 每次都检查最大window 1 package

G面经prepare: Friends Recommendation

想想如果你用linkedin或者facebook, 给你一个人和他的朋友关系网,你会怎么给一个人推荐朋友 一个例子就是A-B, A-C, B - D, B - E, C - D,这个时候问我应该推荐谁给A,我说D,因为他是BC的共同好友,而E只是B的好友,到这我才明白干啥,就是给一个图和里面的一个节点A,用bfs从A出发,找出第二层中indegree度数最大节点 用HashMap<Character, HashSet<Character>>来建图 用HashMap<Chara

G面经prepare: Straight Partition of A Deck of Cards

Define “Straight” as 5 cards with consecutive numbers. Determine if the deck can be fully divided into sets of “Straight”. Example: 1, 2, 3, 4, 4, 5, 5, 6, 7, 8 -> True 这个是用一个hashtable,key是数字,value是出现次数 然后遍历原数组,每一个数字都把hash里从自己开始往后5个color数都-1,如果发现缺数则说

G面经prepare: Chucked Palindrome

给定一个字符串,找出最多有多少个chunked palindrome, 正常的palindrome是abccba, chunked palindrome的定义是:比如volvo, 可以把vo划分在一起,(vo) (l) (vo),那么它是个palindrome.求实现返回最大的chunk 数量. 比如aaaaaa可以是(aaa)(aaa), 但是最大chunk数量应该是(a)(a)(a)(a)(a)(a)为6 这就是一个greedy的问题,从string的两边开始,用i和j记录当前scan到的位

G面经Prepare: Search word delete sequence in dictionary

给一个单词一个字典,每次删除单词里任一个字母直到剩下一个字母,形成一个序列,比如office->offce->ofce->ofc->oc->c.问是否字典里存在一个这种序列 1 package checkDictExistSequence; 2 import java.util.*; 3 4 public class Solution { 5 HashSet<String> dict = new HashSet<String>(); 6 7 publi

G面经Prepare: Valid Preorder traversal serialized String

1 求问下各位大神,怎么判断一个按照Preorder traversal serialized的binary tree的序列是否正确呢?不能deserialize成树比如 2 A) 9 3 4 # # 1 # # 2 # 6 # #是对的,因为表示 3 9 4 / 5 3 2 6 / \ 7 4 1 6 8 B ) 9 3 4 # # 1 # #就是错的,因为无法反构造回一棵树 我觉得可以在字符串里找"n##"这种结构(对应tree里两个children都是Null的叶节点),找到之后

G面经prepare: set difference

给你A{1,2,3,4,4,5}, B{2,4},求A-B={1,3,4,5},很简单. visit 1 只用一个HashMap 1 package TwoSets; 2 import java.util.*; 3 4 public class Solution { 5 public ArrayList<Integer> findDiff(int[] arr1, int[] arr2) { 6 ArrayList<Integer> res = new ArrayList<In