题目:

A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside down).

Write a function to count the total strobogrammatic numbers that exist in the range of low <= num <= high.

For example,
Given low = "50", high = "100", return 3. Because 69, 88, and 96 are three strobogrammatic numbers.

Note:
Because the range might be a large number, the low and high numbers are represented as string.

链接: http://leetcode.com/problems/strobogrammatic-number-iii/

题解:

由于有了上一题目的臭长解法,这里继续沿用...优化留给二刷吧,看官图个乐子好了。 思路是使用Strobogrammatic Number II的代码,我们对于从 low.length()到high.length()的每一个n,求解Strobogrammatic Number,然后把这个数字与low和high进行比较,假如这个数字在[low,high]的闭区间里,则可以算作一个结果。下面的方法大量使用了Strobogrammatic Number II的代码,其实有很多地方可以优化,比如不需要传递一个List<>,只用传递一个数组,用首元素进行计数就可以了,比如int[] arr = new int[1],然后用a[0]来记录数字的增减。 还有methods可以由四个变为三个,合并第一和第二个method,这样也可以不用每次都create一个map,等等。复杂度也要好好计算一下。

Time Complexity - O(L * 2n), Space Complexity - O(2n)。

  1. public class Solution {
  2. public int strobogrammaticInRange(String low, String high) {
  3. if(low == null || high == null)
  4. return 0;
  5. int lo = low.length(), hi = high.length();
  6. int res = 0;
  7.  
  8. for(int i = lo; i <= hi; i++)
  9. res += findStrobogrammatic(i, low, high).size();
  10.  
  11. return res;
  12. }
  13.  
  14. private List<String> findStrobogrammatic(int n, String low, String high) {
  15. if(n < 1)
  16. return new ArrayList<String>();
  17. List<String> res = new ArrayList<>();
  18. Map<Character, Character> map = new HashMap<>();
  19. map.put('0', '0');
  20. map.put('1', '1');
  21. map.put('6', '9');
  22. map.put('8', '8');
  23. map.put('9', '6');
  24.  
  25. StringBuilder sb = new StringBuilder();
  26. int position = (n % 2 == 0) ? 0 : 1;
  27. findStrobogrammatic(res, sb, map, n, position, low, high);
  28.  
  29. return res;
  30. }
  31.  
  32. private void findStrobogrammatic(List<String> res, StringBuilder sb, Map<Character, Character> map, int n, int position, String low, String high) {
  33. if(sb.length() > n)
  34. return;
  35. if(sb.length() == n) {
  36. String s = sb.toString();
  37. if(firstStringEqualToOrSmaller(low, s) && firstStringEqualToOrSmaller(s, high))
  38. res.add(sb.toString());
  39. return;
  40. }
  41.  
  42. if(position == 1) {
  43. for(char c : map.keySet()) {
  44. if(c == '6' || c == '9')
  45. continue;
  46. sb.append(c);
  47. findStrobogrammatic(res, sb, map, n, position + 1, low, high);
  48. sb.setLength(0);
  49. }
  50. } else {
  51. for(char c : map.keySet()) {
  52. if(n - sb.length() == 2 && c == '0')
  53. continue;
  54. sb.insert(0, c);
  55. sb.append(map.get(c));
  56. findStrobogrammatic(res, sb, map, n, position + 2, low, high);
  57. sb.deleteCharAt(0);
  58. sb.deleteCharAt(sb.length() - 1);
  59. }
  60. }
  61. }
  62.  
  63. private boolean firstStringEqualToOrSmaller(String s, String t) {
  64. if(s.length() < t.length())
  65. return true;
  66. else if(s.length() > t.length())
  67. return false;
  68. else {
  69. for(int i = 0; i < s.length(); i++)
  70. if(s.charAt(i) > t.charAt(i))
  71. return false;
  72. else if(s.charAt(i) < t.charAt(i))
  73. return true;
  74. return true;
  75. }
  76. }
  77. }

Reference:

https://leetcode.com/discuss/55468/clear-java-ac-solution-using-strobogrammatic-number-method

https://leetcode.com/discuss/54562/clean-and-easy-java-recursive-solution

https://leetcode.com/discuss/50628/ac-java-solution-with-explanation

https://leetcode.com/discuss/50624/clean-and-easy-understanding-java-solution

https://leetcode.com/discuss/50604/solution-based-on-strobogrammatic-number-ii

248. Strobogrammatic Number III的更多相关文章

  1. [LeetCode] 248. Strobogrammatic Number III 对称数之三

    A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...

  2. [LeetCode] 248. Strobogrammatic Number III 对称数III

    A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...

  3. [Locked] Strobogrammatic Number & Strobogrammatic Number II & Strobogrammatic Number III

    Strobogrammatic Number A strobogrammatic number is a number that looks the same when rotated 180 deg ...

  4. [LeetCode] Strobogrammatic Number III 对称数之三

    A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...

  5. Leetcode: Strobogrammatic Number III

    A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...

  6. [Swift]LeetCode248.对称数 III $ Strobogrammatic Number III

    A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...

  7. [LeetCode] 246. Strobogrammatic Number 对称数

    A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...

  8. [LeetCode] 247. Strobogrammatic Number II 对称数II

    A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...

  9. [LeetCode] Strobogrammatic Number II 对称数之二

    A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...

随机推荐

  1. git,repo学习

    Repo:就是一组git命令的集合,repo init 下载一个分支. repo start 文件名 --all本地传建的另一个代码分支,用于备份作用. 比如:repo start zhao --al ...

  2. 【quartz】 入门

    把技术债务给还了,首先来一个最简单的demo: 2.x版比1.x有很多改进,1.x基于fw1.2: 2.x基于fw3.5以上:语法上有很大的不同,摒弃了很多1.x的很多东西: 直接以2.x来demo ...

  3. composer的安装

    HomeGetting StartedDownloadDocumentationBrowse Packages Dependency management Declaring dependencies ...

  4. SqlServer维护计划

    http://blog.csdn.net/yunye114105/article/details/6594826

  5. SpringMVC Cache注解+Redis

    依赖jar包:Xml代码  收藏代码 <!-- redis -->              <dependency>                  <groupId ...

  6. Lightmapping

    当游戏场景包含了大量的多边形时,实时光源和阴影对游戏性能的影响会很大.这时更适合使用Lightmapping技术,将光线效果预渲染成贴图使用到多边形上模拟光影效果.这种方式不用担心光源数量和阴影对性能 ...

  7. 《自学C语言》初级教程 - 目录

    我现在打算出一个C语言学习教程,目的是为了让初学者能够很容易和更深刻地理解C语言. 你可能有这样的疑问,网上不是有很多的初级教程吗,我需要这个吗?我的回答是:网上的C语言教程讲得不够全面,而且许多的初 ...

  8. 开发中/listfile.jsp(11,31) quote symbol expected 这个错误

    可能是因为11行33列,少了一个引号.

  9. rJava配置

    1. 下载安装R-3.1.1-win.exe: 2. 在R中安装rJava > install.packages("rJava") 3. 设置环境变量: PATH:D:\So ...

  10. Linux开机执行bash脚本

    问题描述:     Linux开机执行bash脚本     问题解决:         (1)在 /etc/init.d文件夹中新建一个脚本myinit                     (2) ...