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

 package AndroidUnlockPattern;
import java.util.*; public class Solution {
int res = 0;
HashSet<Integer> set = new HashSet<Integer>();
static ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> path = new ArrayList<Integer>(); public int calculate(int L) {
for (int i=1; i<=9; i++) {
set.add(i);
}
HashSet<Integer> visited = new HashSet<Integer>();
helper(0, 0, L, visited);
return res;
} public void helper(int cur, int pos, int L, HashSet<Integer> visited) {
if (pos == L) {
res++;
result.add(new ArrayList<Integer>(path));
return;
}
for (int elem : set) {
if (visited.contains(elem)) continue;
if (cur == 1) {
if (elem==3 && !visited.contains(2)) continue;
if (elem==7 && !visited.contains(4)) continue;
if (elem==9 && !visited.contains(5)) continue;
}
else if (cur == 2) {
if (elem==8 && !visited.contains(5)) continue;
}
else if (cur == 3) {
if (elem==1 && !visited.contains(2)) continue;
if (elem==7 && !visited.contains(5)) continue;
if (elem==9 && !visited.contains(6)) continue;
}
else if (cur == 4) {
if (elem == 6 && !visited.contains(5)) continue;
}
else if (cur == 6) {
if (elem == 4 && !visited.contains(5)) continue;
}
else if (cur == 7) {
if (elem==1 && !visited.contains(4)) continue;
if (elem==3 && !visited.contains(5)) continue;
if (elem==9 && !visited.contains(8)) continue;
}
else if (cur == 8) {
if (elem==2 && !visited.contains(5)) continue;
}
else if (cur == 9) {
if (elem==1 && !visited.contains(5)) continue;
if (elem==3 && !visited.contains(6)) continue;
if (elem==7 && !visited.contains(8)) continue;
}
visited.add(elem);
path.add(elem);
helper(elem, pos+1, L, visited);
visited.remove(elem);
path.remove(path.size()-1);
}
} /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Solution sol = new Solution();
int res = sol.calculate(3);
System.out.println(res);
for (ArrayList<Integer> each : result) {
System.out.println(each);
}
} }

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

  1. G面经prepare: Pattern Match

    设定一个pattern 把 'internationalization' 变成 'i18n', 比如word是house,pattern可以是h3e, 3se, 5, 1o1s1等, 给pattern ...

  2. 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 ...

  3. G面经prepare: Maximum Subsequence in Another String's Order

    求string str1中含有string str2 order的 subsequence 的最小长度 DP做法:dp[i][j]定义为pattern对应到i位置,string对应到j位置时,shor ...

  4. G面经prepare: Set Intersection && Set Difference

    求两个sorted数组的intersection e.g. [1,2,3,4,5],[2,4,6] 结果是[2,4] difference 类似merge, 分小于等于大于三种情况,然后时间O(m+n ...

  5. G面经prepare: Reorder String to make duplicates not consecutive

    字符串重新排列,让里面不能有相同字母在一起.比如aaabbb非法的,要让它变成ababab.给一种即可 Greedy: 跟FB面经Prepare task Schedule II很像,记录每个char ...

  6. Android 正则表达式,Pattern,Matcher基本使用

    Pattern类:      Pattern的创建:      Pattern pattern =Pattern.complie(String regex)     参数说明:regex:是一个正则表 ...

  7. G面经prepare: Data Stream Average

    给一个datastream和一个fixed window size, 让我design一个class可以完成add number还有find average in the window. 就是不能用v ...

  8. G面经prepare: Jump Game Return to Original Place

    第二题 算法 给你一个arr 返回 T 或者 F arr的每个数代表从这个点开始跳几部,返回T的情况:从这个arr中任意一个数开始跳,可以在每个元素都跳到且只跳到一次的情况下返回到开始跳的元素 比如[ ...

  9. G面经prepare: Sort String Based On Another

    Given a sorting order string, sort the input string based on the given sorting order string. Ex sort ...

随机推荐

  1. php配置相关

    php.ini error_reporting //配置错误的显示方式 display_errors //此项优先级高于error_reporting,如果关闭该项,会返还http状态码,如果开启,则 ...

  2. 【转】Java 5种字符串拼接方式性能比较。

    最近写一个东东,可能会考虑到字符串拼接,想了几种方法,但对性能未知,于是用Junit写了个单元测试. 代码如下: import java.util.ArrayList; import java.uti ...

  3. 【转】C#中HttpWebRequest的用法详解

    本文实例讲述了C#中HttpWebRequest的用法.分享给大家供大家参考.具体如下: HttpWebRequest类主要利用HTTP 协议和服务器交互,通常是通过 GET 和 POST 两种方式来 ...

  4. stage划分

    整个stage的划分会根据最后触发的action进行倒推,如果碰到宽依赖就将当前范围内的rdd划分为一个stage,直到所有的RDD遍历完为止.

  5. sublimtext2 资源

    https://github.com/qljiong/soda-theme/blob/master/README.md http://my.oschina.net/ruochenchen/blog/9 ...

  6. 更强大的trim功能,过滤汉字等

    第一种方法:通过php自带的函数 <?php /* trim 去除一个字符串两端空格, rtrim 是去除一个字符串右部空格, ltrim 是去除一个字符串左部空格. */ ?> < ...

  7. PySe-002-Py-简单示例及编码设定

    非常简单而又国际化的例子 Hello World!在控制台依次输入命令:python, print "This is first program %s" % "Hello ...

  8. TestNG学习-001-基础理论知识

    此 文主要讲述用 TestNG 的基础理论知识,TestNG 的特定,编写测试过程三步骤,与 JUnit4+ 的差异,以此使亲对 TestNG 测试框架能够有一个简单的认知. 希望能对初学 TestN ...

  9. Webapp的display-name问题

    临时需要做一个webapp,就按myeclipse缺省的web工程做了,web.xml也没改,本地测试没问题就放到服务器上去了. 测试发现,走 http://服务器ip:8080/appname居然出 ...

  10. vertical-align的理解

    vertical-align的理解 定义和用法 vertical-align 属性设置元素的垂直对齐方式 该属性定义行内元素的基线相对于该元素所在行的基线的垂直对齐 也就是说 它只是个适用行内元素的属 ...