题目:

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)。

public class Solution {
public int strobogrammaticInRange(String low, String high) {
if(low == null || high == null)
return 0;
int lo = low.length(), hi = high.length();
int res = 0; for(int i = lo; i <= hi; i++)
res += findStrobogrammatic(i, low, high).size(); return res;
} private List<String> findStrobogrammatic(int n, String low, String high) {
if(n < 1)
return new ArrayList<String>();
List<String> res = new ArrayList<>();
Map<Character, Character> map = new HashMap<>();
map.put('0', '0');
map.put('1', '1');
map.put('6', '9');
map.put('8', '8');
map.put('9', '6'); StringBuilder sb = new StringBuilder();
int position = (n % 2 == 0) ? 0 : 1;
findStrobogrammatic(res, sb, map, n, position, low, high); return res;
} private void findStrobogrammatic(List<String> res, StringBuilder sb, Map<Character, Character> map, int n, int position, String low, String high) {
if(sb.length() > n)
return;
if(sb.length() == n) {
String s = sb.toString();
if(firstStringEqualToOrSmaller(low, s) && firstStringEqualToOrSmaller(s, high))
res.add(sb.toString());
return;
} if(position == 1) {
for(char c : map.keySet()) {
if(c == '6' || c == '9')
continue;
sb.append(c);
findStrobogrammatic(res, sb, map, n, position + 1, low, high);
sb.setLength(0);
}
} else {
for(char c : map.keySet()) {
if(n - sb.length() == 2 && c == '0')
continue;
sb.insert(0, c);
sb.append(map.get(c));
findStrobogrammatic(res, sb, map, n, position + 2, low, high);
sb.deleteCharAt(0);
sb.deleteCharAt(sb.length() - 1);
}
}
} private boolean firstStringEqualToOrSmaller(String s, String t) {
if(s.length() < t.length())
return true;
else if(s.length() > t.length())
return false;
else {
for(int i = 0; i < s.length(); i++)
if(s.charAt(i) > t.charAt(i))
return false;
else if(s.charAt(i) < t.charAt(i))
return true;
return true;
}
}
}

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. Eclipse导入android包错误

    错误提示:Invalid project description… 解决方案:假设你的工作空间是workshop,那么你可以在你的workshop下新建一个文件夹,然后放入你的包,再在Eclipse中 ...

  2. printf的格式控制的完整格式

    printf的格式控制的完整格式:%  -  0  m.n  l或h  格式字符下面对组成格式说明的各项加以说明:①%:表示格式说明的起始符号,不可缺少.②-:有-表示左对齐输出,如省略表示右对齐输出 ...

  3. vs2010旗舰版后,运行调试一个项目时调试不了,提示的是:无法使用“pc”附加到应用程序“webdev.webserver40.exe(PID:2260”

    具体问题描述: vs2010旗舰版后,运行调试一个项目时调试不了,能编译,按ctrl+f5 可以运行,但是就是调试就不行,提示的是:无法使用“pc”附加到应用程序“webdev.webserver40 ...

  4. C预处理,条件编译

    预处理是指在编译器之前运行,常以“#”开头 包含3个方面的内容: 1)宏定义与宏替换 2)文件包含 3)条件编译 宏定义与宏替换: 宏名一般大写,替换发生在编译之前,且是机械替换,不做语法检查,也不分 ...

  5. 【原创】CHROME 最小字体限制为12PX的终极解决方案

    CHROME 最小字体限制为12PX的终极解决方案 本文由五月雨恋提供,转载请注明出处. 相信不少做网站的用户会有这样一个问题,Chrome 默认最小字体是12px(最新版英文也有此问题),这个是 C ...

  6. android 解决启动页面加载图片空白以及去掉标题栏

    有时候启动页面根据白天晚上来启动时实现加载不同的图片效果,但是加载时会出现短暂的空白,解决方法如下: android:theme="@android:style/Theme.Transluc ...

  7. 【转载】Spring加载resource时classpath*:与classpath:的区别

    免责声明:     本文转自网络文章,转载此文章仅为个人收藏,分享知识,如有侵权,请联系博主进行删除.     原文作者:kyfxbl     原文地址: spring配置中classpath和cla ...

  8. jQuery+css+div--一些细节详解

    (一).首先.让我们认识一下最基本普通的alert()弹出框!(改变alert()提示弹出框的样式) 我们在写html或是jsp页面的时候,谁都不希望自己精心设计,且非常美观的页面颜色布局被破坏掉吧! ...

  9. 导致Asp.Net站点重启的10个原因 ,记录重启原因

    Asp.Net站点有时候会莫名其妙的重启,什么原因导致的却不得而知,经过一番折腾后,我总结了导致Asp.Net站点重启的10个原因 1. 回收应用程序池会导致站点重启,记录的原因是: HostingE ...

  10. Codeforces Round #363 (Div. 2)->B. One Bomb

    B. One Bomb time limit per test 1 second memory limit per test 256 megabytes input standard input ou ...