最后更新

三刷

15-Jan-2017

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

  1. public class Solution {
  2. public int numberOfPatterns(int m, int n) {
  3. int[][] path = new int[10][10];
  4. boolean[] visited = new boolean[10];
  5. /*
  6. | 1 | 2 | 3 |
  7. | 4 | 5 | 6 |
  8. | 7 | 8 | 9 |
  9. */
  10. path[1][3] = path[3][1] = 2;
  11. path[1][7] = path[7][1] = 4;
  12. path[3][9] = path[9][3] = 6;
  13. path[9][7] = path[7][9] = 8;
  14. path[1][9] = path[9][1] = 5;
  15. path[2][8] = path[8][2] = 5;
  16. path[3][7] = path[7][3] = 5;
  17. path[4][6] = path[6][4] = 5;
  18. int res = 0;
  19. for (int i = m; i <= n; i++) {
  20. res += 4 * dfs(i - 1, path, visited, 1);
  21. res += 4 * dfs(i - 1, path, visited, 2);
  22. res += dfs(i - 1, path, visited, 5);
  23. }
  24. return res;
  25. }
  26. public int dfs(int left, int[][] path, boolean[] visited, int pos) {
  27. if (left == 0) {
  28. return 1;
  29. } else {
  30. visited[pos] = true;
  31. int res = 0;
  32. for (int i = 1; i <= 9; i++) {
  33. if (visited[i]) continue;
  34. if (path[i][pos] == 0 || visited[path[i][pos]]) {
  35. res += dfs(left-1, path, visited, i);
  36. }
  37. }
  38. visited[pos] = false;
  39. return res;
  40. }
  41. }
  42. }

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

题目不难,就是算组合,但是因为是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)

  1. public class Solution
  2. {
  3. int res = 0;
  4. public int numberOfPatterns(int m, int n)
  5. {
  6. boolean[][] visited = new boolean[3][3];
  7. visited[0][0] = true;
  8. helper(0,0,m,n,1,visited);
  9. //System.out.println(res);
  10. visited = new boolean[3][3];
  11. visited[0][1] = true;
  12. helper(0,1,m,n,1,visited);
  13. //System.out.println(res);
  14. res *= 4;
  15. visited = new boolean[3][3];
  16. visited[1][1] = true;
  17. helper(1,1,m,n,1,visited);
  18. return res;
  19. }
  20. public void helper(int m, int n, int x, int y, int c, boolean[][] visited)
  21. {
  22. if(c >= x && c <= y) res++;
  23. for(int i = 0; i < 3;i++)
  24. for(int j = 0; j < 3; j++)
  25. {
  26. if(jumpable(visited,m,n,i,j))
  27. {
  28. visited[i][j] = true;
  29. helper(i,j,x,y,c+1,visited);
  30. visited[i][j] = false;
  31. }
  32. }
  33. }
  34. public boolean jumpable(boolean[][] visited, int m1, int n1, int m2, int n2)
  35. {
  36. if(visited[m2][n2]) return false;
  37. int x = Math.abs(m1-m2);
  38. int y = Math.abs(n1-n2);
  39. // adjacent
  40. if(x <= 1 && y <= 1) return true;
  41. // di
  42. if(x == 2 && y == 2) return visited[1][1];
  43. // horizontal
  44. if(x == 0 && y == 2) return visited[m1][1];
  45. // vertical
  46. if(x == 2 && y == 0) return visited[1][n1];
  47. /*
  48. if(x == 1 && y == 2) return visited[m1][1] && visited[m2][1];
  49. if(x == 2 && y == 1) return visited[1][n1] && visited[1][n2];
  50. */
  51. return true;
  52. }
  53. }



二刷。

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)

  1. public class Solution {
  2. public int numberOfPatterns(int m, int n) {
  3. int[][] path = new int[10][10];
  4. /*
  5. | 1 | 2 | 3 |
  6. | 4 | 5 | 6 |
  7. | 7 | 8 | 9 |
  8. */
  9. path[1][3] = path[3][1] = 2;
  10. path[1][7] = path[7][1] = 4;
  11. path[3][9] = path[9][3] = 6;
  12. path[9][7] = path[7][9] = 8;
  13. path[1][9] = path[9][1] = 5;
  14. path[2][8] = path[8][2] = 5;
  15. path[3][7] = path[7][3] = 5;
  16. path[4][6] = path[6][4] = 5;
  17. int res = 0;
  18. boolean[] visited = new boolean[10];
  19. for (int i = m; i <= n; i++) {
  20. res += 4 * dfs(path, visited, 1, i - 1);
  21. res += 4 * dfs(path, visited, 2, i - 1);
  22. res += dfs(path, visited, 5, i - 1);
  23. }
  24. return res;
  25. }
  26. public int dfs(int[][] path, boolean[] visited, int cur, int left) {
  27. if (left == 0) {
  28. return 1;
  29. } else {
  30. int res = 0;
  31. visited[cur] = true;
  32. for (int i = 1; i <= 9; i++) {
  33. if (visited[i]) continue;
  34. if (path[cur][i] == 0 || visited[path[cur][i]]) {
  35. res += dfs(path, visited, i, left - 1);
  36. }
  37. }
  38. visited[cur] = false;
  39. return res;
  40. }
  41. }
  42. }

一刷的办法通过坐标来判断是否经过,也不错……思考起来稍微麻烦点,实际上正解应该是一刷那种不使用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. Linux系统swap已分区但无法挂载与cryptswap1问题

    linux下察看swap分区大小的命令 top 或者fdisk -l 或者free -m SWAP分区一般大小为物理内存的2倍,但最大不超过2G; 增加SWAP空间的方法有两个:增加另外一个SWAP分 ...

  2. JSONKit 在iOS9 arm7 64位下出现的问题

    最近遇到了一个关于JSONKit的问题,在项目加了arm7 64位以后,JSONKIT会出现[params JSONString] forKey:@”gson”];报错的情况,如下图 具体原因不太清楚 ...

  3. wordpress 当前栏目名,当前栏目的分类名

    wordpress在设计主题和做模板时经常会用到调用当前分类栏目名称,常见的有当前栏目页.文章页,详情代码如下: 1.分类名称与链接 <?php the_category(); ?> 2. ...

  4. 2016.7.16equals的使用(一)

    class V{ private int a; V(int a){ rhis a=a; } public  boolean equals(int a,int b){ if(this.a equals( ...

  5. js手机站跳转

    var yunzhuanhua_pc_domain = "http://www.域名.com#yht"; //PC站网址var yunzhuanhua_wap_domain = & ...

  6. Notepad++插件之FingerText

    FingerText是一个标签触发片段插件记事本.支持多个热点同时编辑,嵌套的热点,动态热点(很多不仅仅是纯文本的,可以通过命令,或触发另一个片段中的片段),热点的文本提示(而不是仅仅是$或#号)和热 ...

  7. PM加油站

    老郭讲述深航CSM 1.需求有遗漏,人员水平不足:加班导致人员流失:但是这样,客户后来还是好评,并且项目被评为深航的标杆项目:老郭也是被指定为未来项目的项目经理:--!我想起了古时候的一句话:功夫在诗 ...

  8. DevOps - Development And Operations

    简介: 研发运维一体化 相关资料: 关于DevOps你必须知道的11件事 我眼中的DevOps DevOps 门户 docker for dotnet系列 docker4dotnet #1 前世今生 ...

  9. PHP框架、库和软件资源大全(整理篇)

    php的资料 https://github.com/ziadoz/awesome-php Awesome PHP A curated list of amazingly awesome PHP lib ...

  10. Unity3d 读取网络xml

    Unity3d 读取网络xml Unity3d 读取网络xml,这个xml文件需要不包含BOM信息,可以用UltraEdit打开xml文件,并且另存为的时候,选择不包含BOM的utf-8格式存储!