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 ...
随机推荐
- Asp.net 服务器Application,Session,Cookie,ViewState和Cache区别
2.8 Context 的使用Context 对象包含与当前页面相关的信息,提供对整个上下文的访问,包括请求.响应.以及上文中的Session 和Application 等信息.可以使用此对象在网页之 ...
- 专家来了-提测-改bug-上线10号
集成那天,同事帮忙改了三个bug, 适配ios6约束,方法被调用两次, 郑晓杨吃饭,好像还欠我钱呢 Product-archive 打包 ------------------------------ ...
- 关于jQuery学习
≡[..]≡≡[..]≡ 所有的实例都位于document.ready里面--为了防止文档在未完全加载之前就运行函数导致操作失败. $(document).ready(function(){ --- ...
- 如何在外网中访问自己在另一个局域网中的某个机器(SSH为例)
UBUNTU 14.04 LTS 为例 如何在外网中访问自己在另一个局域网中的某个机器(SSH为例) 2013-05-01 16:02 2693人阅读 评论(0) 收藏 举报 情景描述: 计算机C1放 ...
- 使用NSTimer过程中最大的两个坑
坑1. retain cycle问题. 在一个对象中使用循环执行的nstimer时,若希望在对象的dealloc方法中释放这个nstimer,结局会让你很失望. 这个timer会导致你的对象根本不会被 ...
- MongoDB过过瘾
MongoDB 中默认的数据库为 test,连接后尝试以下操作 连接 插入数据:用过json的同学看到这格式相信不会陌生吧! db.person.insert({}) db.person.insert ...
- Android性能优化典范(转)
转载自oschina. 2015年伊始,Google发布了关于Android性能优化典范的专题, 一共16个短视频,每个3-5分钟,帮助开发者创建更快更优秀的Android App.课程专题不仅仅介绍 ...
- 算法训练 A+B Problem
算法训练 A+B Problem 时间限制:1.0s 内存限制:512.0MB 问题描述 输入A,B. 输出A+B. 输入格式 输入包含两个整数A,B,用一个空格分隔. 输出格式 ...
- gulp-less插件之less文件编译成css
gulp 是基于node的,所以第一步要确保你已经安装了node环境,具体怎么安装可以到node官网去看一下(https://nodejs.org/en/) 1.全局按钮gulp 打开node窗口输入 ...
- vb 修改数据库
Dim rscode As New ADODB.Recordset ................... Set RsCode = zwpub.DataMdb.DbConnect.Execute(& ...