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
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的更多相关文章
- G面经prepare: Pattern Match
设定一个pattern 把 'internationalization' 变成 'i18n', 比如word是house,pattern可以是h3e, 3se, 5, 1o1s1等, 给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 ...
- G面经prepare: Maximum Subsequence in Another String's Order
求string str1中含有string str2 order的 subsequence 的最小长度 DP做法:dp[i][j]定义为pattern对应到i位置,string对应到j位置时,shor ...
- 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 ...
- G面经prepare: Reorder String to make duplicates not consecutive
字符串重新排列,让里面不能有相同字母在一起.比如aaabbb非法的,要让它变成ababab.给一种即可 Greedy: 跟FB面经Prepare task Schedule II很像,记录每个char ...
- Android 正则表达式,Pattern,Matcher基本使用
Pattern类: Pattern的创建: Pattern pattern =Pattern.complie(String regex) 参数说明:regex:是一个正则表 ...
- G面经prepare: Data Stream Average
给一个datastream和一个fixed window size, 让我design一个class可以完成add number还有find average in the window. 就是不能用v ...
- G面经prepare: Jump Game Return to Original Place
第二题 算法 给你一个arr 返回 T 或者 F arr的每个数代表从这个点开始跳几部,返回T的情况:从这个arr中任意一个数开始跳,可以在每个元素都跳到且只跳到一次的情况下返回到开始跳的元素 比如[ ...
- 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 ...
随机推荐
- HTmlTableTOExcel
function TableTOExcel() { //第一种方法 //var url = 'data:application/vnd.ms-excel;base64,' + encodeURICom ...
- nginx反向代理初探
1.安装nginx 2.在nginx.conf的http区段中配置负载均衡段 #cluserupstream myCluster{ server 192.168.1.110:1300 weight=5 ...
- PHP iconv()函数转字符编码的问题(转)
PHP iconv()函数转字符编码的问题(转) 载自:http://www.nowamagic.net/php/php_FunctionIconv.php 在php函数库有一个函数:iconv(), ...
- 【转】CodeIgniter定义自己的Helper和Helper的方法
最近做程序,采用了PHP+CodeIgniter框架,总体来说这框架很轻而且上手也很快的. 首先,说下如何定义自己的Helper,个人理解helper其实就是定义函数方法. 如果要建个全新的自己的He ...
- Iphone H5上传照片被旋转
最近做项目发现在Iphone下,我们上传图片都会被翻转,最后查阅资料发现,的确是IOS的问题 不说过程,直接解决方法 iOS下,html方式使用<input type="file&qu ...
- Python的安装
篇幅主要是别人的分享,我这里主要是添加注意点.我当初就是按照下面的图片开始安装python,安装的是python3.5,pyDev也是使用的博主的(还花了1资源分下载).但是运行程序时,一直显示 Er ...
- Nginx+Django+Uwsgi+php
在FreeBSD结合Nginx和FastCGI简单配置Django和PHP http://blog.chinaunix.net/uid-11131943-id-3031767.html Nginx+ ...
- python join
# 对序列进行操作 ' '.join(['hello','good','boy','doiido']) hello:good:boy:doiido # 对字符串进行操作 ':'.join(" ...
- Android Lint简介(转)
转载自原文:http://blog.csdn.net/hudashi/article/details/8333349,感谢原作者. 英文原文:http://tools.android.com/tips ...
- office-002-onenote、word、outlook取消首字母大小写图文详解
此文主要讲述如何取消微软办公软件 onenote.work.outlook 中首字母大写等的自动更正项,其他 office 办公软件相关设置的操作,可参考此文进行相应的设置.希望能对亲有所帮助,若有错 ...