最后更新

三刷

15-Jan-2017

好熟悉的题了,DFS做,注意比如1-3是经过2的,如果2被访问过,那么1-3也是可以的。

public class Solution {
public int numberOfPatterns(int m, int n) {
int[][] path = new int[10][10];
boolean[] visited = new boolean[10]; /*
| 1 | 2 | 3 |
| 4 | 5 | 6 |
| 7 | 8 | 9 |
*/
path[1][3] = path[3][1] = 2;
path[1][7] = path[7][1] = 4;
path[3][9] = path[9][3] = 6;
path[9][7] = path[7][9] = 8; path[1][9] = path[9][1] = 5;
path[2][8] = path[8][2] = 5;
path[3][7] = path[7][3] = 5;
path[4][6] = path[6][4] = 5; int res = 0; for (int i = m; i <= n; i++) {
res += 4 * dfs(i - 1, path, visited, 1);
res += 4 * dfs(i - 1, path, visited, 2);
res += dfs(i - 1, path, visited, 5);
} return res;
} public int dfs(int left, int[][] path, boolean[] visited, int pos) {
if (left == 0) {
return 1;
} else {
visited[pos] = true;
int res = 0;
for (int i = 1; i <= 9; i++) {
if (visited[i]) continue;
if (path[i][pos] == 0 || visited[path[i][pos]]) {
res += dfs(left-1, path, visited, i);
}
}
visited[pos] = false;
return res;
} }
}

这个题我真是做得想打人了卧槽。

题目不难,就是算组合,但是因为是3乘3的键盘,所以只需要从1和2分别开始DFS,结果乘以4,再加上5开始的DFS就行了。

问题是这个傻逼题目的设定是,从1到8不需要经过4或者5。。。

我TEST CASE 2,2 卡了好久好久,做得我怀疑人生了,怎么算都是

(3+5)*4 + 8。。结果答案愣是56.

最后发现题设里从1-8是不需要经过4或者5的,真是要报警了。

https://discuss.leetcode.com/topic/63038/how-many-people-thought-from-1-to-8-should-visit-voth-4-and-5)

public class Solution
{
int res = 0;
public int numberOfPatterns(int m, int n)
{ boolean[][] visited = new boolean[3][3];
visited[0][0] = true;
helper(0,0,m,n,1,visited); //System.out.println(res);
visited = new boolean[3][3];
visited[0][1] = true;
helper(0,1,m,n,1,visited);
//System.out.println(res); res *= 4; visited = new boolean[3][3];
visited[1][1] = true;
helper(1,1,m,n,1,visited); return res; } public void helper(int m, int n, int x, int y, int c, boolean[][] visited)
{
if(c >= x && c <= y) res++; for(int i = 0; i < 3;i++)
for(int j = 0; j < 3; j++)
{ if(jumpable(visited,m,n,i,j))
{
visited[i][j] = true;
helper(i,j,x,y,c+1,visited);
visited[i][j] = false; } }
} public boolean jumpable(boolean[][] visited, int m1, int n1, int m2, int n2)
{
if(visited[m2][n2]) return false;
int x = Math.abs(m1-m2);
int y = Math.abs(n1-n2); // adjacent
if(x <= 1 && y <= 1) return true; // di
if(x == 2 && y == 2) return visited[1][1]; // horizontal
if(x == 0 && y == 2) return visited[m1][1]; // vertical
if(x == 2 && y == 0) return visited[1][n1];
/*
if(x == 1 && y == 2) return visited[m1][1] && visited[m2][1];
if(x == 2 && y == 1) return visited[1][n1] && visited[1][n2];
*/
return true; } }



二刷。

08-Nov-2016

这个题光记得一刷的时候颇有怨念,因为跳马子格不算碰到中间2个,卡了好久

https://discuss.leetcode.com/topic/63038/how-many-people-thought-from-1-to-8-should-visit-voth-4-and-5)

还是以 1 2 5分别作为起点,然后1 和 2的要最后乘以4,因为1-9个数字可以转90°转3次。

这次换了个做法,直接标记所有路径,反正也不是很多。

Time: O(n!)

Space: O(n^2)

public class Solution {
public int numberOfPatterns(int m, int n) {
int[][] path = new int[10][10];
/*
| 1 | 2 | 3 |
| 4 | 5 | 6 |
| 7 | 8 | 9 |
*/
path[1][3] = path[3][1] = 2;
path[1][7] = path[7][1] = 4;
path[3][9] = path[9][3] = 6;
path[9][7] = path[7][9] = 8;
path[1][9] = path[9][1] = 5;
path[2][8] = path[8][2] = 5;
path[3][7] = path[7][3] = 5;
path[4][6] = path[6][4] = 5;
int res = 0;
boolean[] visited = new boolean[10];
for (int i = m; i <= n; i++) {
res += 4 * dfs(path, visited, 1, i - 1);
res += 4 * dfs(path, visited, 2, i - 1);
res += dfs(path, visited, 5, i - 1);
}
return res;
} public int dfs(int[][] path, boolean[] visited, int cur, int left) {
if (left == 0) {
return 1;
} else {
int res = 0;
visited[cur] = true;
for (int i = 1; i <= 9; i++) {
if (visited[i]) continue;
if (path[cur][i] == 0 || visited[path[cur][i]]) {
res += dfs(path, visited, i, left - 1);
}
}
visited[cur] = false;
return res;
}
}
}

一刷的办法通过坐标来判断是否经过,也不错……思考起来稍微麻烦点,实际上正解应该是一刷那种不使用N^2空间。

351. Android Unlock Patterns的更多相关文章

  1. [LeetCode] 351. Android Unlock Patterns 安卓解锁模式

    Given an Android 3x3 key lock screen and two integers m and n, where 1 ≤ m ≤ n ≤ 9, count the total ...

  2. LC 351. Android Unlock Patterns

    Given an Android 3x3 key lock screen and two integers m and n, where 1 ≤ m ≤ n ≤ 9, count the total ...

  3. [LeetCode] Android Unlock Patterns 安卓解锁模式

    Given an Android 3x3 key lock screen and two integers m and n, where 1 ≤ m ≤ n ≤ 9, count the total ...

  4. Leetcode: Android Unlock Patterns

    Given an Android 3x3 key ≤ m ≤ n ≤ , count the total number of unlock patterns of the Android lock s ...

  5. Android Unlock Patterns

    Given an Android 3x3 key lock screen and two integers m and n, where 1 ≤ m ≤ n ≤ 9, count the total ...

  6. [Swift]LeetCode351. 安卓解锁模式 $ Android Unlock Patterns

    Given an Android 3x3 key lock screen and two integers m and n, where 1 ≤ m ≤ n ≤ 9, count the total ...

  7. LeetCode All in One 题目讲解汇总(持续更新中...)

    终于将LeetCode的免费题刷完了,真是漫长的第一遍啊,估计很多题都忘的差不多了,这次开个题目汇总贴,并附上每道题目的解题连接,方便之后查阅吧~ 477 Total Hamming Distance ...

  8. LeetCode All in One题解汇总(持续更新中...)

    突然很想刷刷题,LeetCode是一个不错的选择,忽略了输入输出,更好的突出了算法,省去了不少时间. dalao们发现了任何错误,或是代码无法通过,或是有更好的解法,或是有任何疑问和建议的话,可以在对 ...

  9. Leetcode重点 250题-前400 题

    删除不常考,面试低频出现题目 删除重复代码题目(例:链表反转206题,代码在234题出现过) 删除过于简单题目(例:100题:Same Tree) 删除题意不同,代码基本相同题目(例:136 & ...

随机推荐

  1. Topas命令详解

    Topas命令详解 执行topas命令后如图所示: #topas 操作系统的最全面动态,而又查看方便的性能视图就是topas命令了,下面以topas输出为例,对AIX系统的性能监控做简要描述,供运维工 ...

  2. healthkit 记录每天用户的运动情况

    //详细操作步骤 http://www.csdn.net/article/2015-01-23/2823686-healthkit-tutorial-with-swift //官方api https: ...

  3. 微信小程序开发之 下拉刷新,上拉加载更多

    本文记载了如何在微信小程序里面实现下拉刷新,上拉加载更多 先开看一下界面 大致如此的界面吧. 这个Demo使用了微信的几个Api和事件,我先列出来. 1.wx.request (获取远程服务器的数据, ...

  4. STL库list::sort()实现深度解析

    原创,转载请注明出处:STL库list::sort()实现深度解析 list模板的定义以及一些基本成员函数的实现这里我就不赘述了,还不清楚的同学可以到网上查找相关资料或者直接查看侯捷翻译的<ST ...

  5. 24种设计模式--多例模式【Multition Pattern】

    这种情况有没有?有!大点声,有没有?有,是,确实有,就出现在明朝,那三国期间的算不算,不算,各自称帝,各有各的地盘,国号不同.大家还 记得那首诗<石灰吟>吗?作者是谁?于谦,他是被谁杀死的 ...

  6. meta里面的viewport属性

    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1, ...

  7. 解决xp共享的批处理文件

    在空白地方点右键选择新建一个文本文档,将默认的“新建 文本文档.txt”文件名改名为以下红色加粗字体内容,再复制红色内容以下的黑字部分到改名后的文档.其他文件生成的方法相同.完成后根据需要双击CMD扩 ...

  8. 重装Ubuntu系统并配置开发环境

    安装 Ubuntu 并配置开发环境 写一篇文章详细记录下来所有的过程,以便以后参考. 安装前的准备 备份所有代码和配置文件 备份下载的各类文件 Ubuntu 安装 下载安装 Ubuntu14.04,下 ...

  9. "sessionFactory " or "hibernateTemplate " is required异常

    <beans    xmlns="http://www.springframework.org/schema/beans"    xmlns:xsi="http:/ ...

  10. Android 安装过程中的问题

    Android  安装过程中的问题 上一篇我说到配置android环境,但是在具体的安装过程中,因为下载的软件或者方法不同,导致没有正确的结果,如果有一些错误的时候,可以试一试关闭eclipse软件, ...