题目:

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. Delphi ISO 收藏!

    CodeGear RAD Studio 2007 最终版(With Update4) v11.0.2902.10471http://altd.codegear.com/download/radstud ...

  2. 边界函数Bounding Function(成长函数的上界)

    根据成长函数的定义,猜测    -->break point K restricts maximum possible mh(N) a lot for N>k bounding funct ...

  3. (转)《深入理解java虚拟机》学习笔记10——并发编程(二)

    Java的并发编程是依赖虚拟机内存模型的三个特性实现的: (1).原子性(Atomicity): 原子性是指不可再分的最小操作指令,即单条机器指令,原子性操作任意时刻只能有一个线程,因此是线程安全的. ...

  4. http概述

    HTTP是一个属于应用层的面向对象的协议,由于其简捷.快速的方式,适用于分布式超媒体信息系统.它于1990年提出,经过几年的使用与发展,得到不断地完善和扩展.目前在WWW中使用的是HTTP/1.0的第 ...

  5. Entity Framework学习笔记(五)----Linq查询(2)---贪婪加载

    请注明转载地址:http://www.cnblogs.com/arhat 在上一章中,我们使用了Linq对Entity Framework进行了一个查询,但是通过学习我们却发现了懒加载给我来的性能上的 ...

  6. duilib中各控件响应的消息类型

    消息 说明 Sender click 鼠标点击 CButtonUI dropdown 下拉显示 CComboUI headerclick 点击列标题 CListHeaderItemUI itemact ...

  7. android退出登陆后,清空之前所有的activity,进入登陆主界面

    如题: android退出登陆后,清空之前所有的activity,进入登陆主界面 在退出登陆时只需要增加一个intent标志 Intent intent_login = new Intent(); i ...

  8. 在ubuntu16.04 下安装haproxy 1.5.11 做tcp负载均衡

    由于haproxy需要FQ下载,所以从csdn下载了较为新版的haproxy1.5.11,安装过程如下: 1. 解压haproxy-1.5.11.tar.gz : tar xzvf haproxy-1 ...

  9. 在windows 7搭建xcode开发环境

    前言:本文适用于没有ios开发环境(如无iphone,mac等),在windows上搭建出ios的开发环境.值得注意的是,在windows搭建的ios开发环境只能做开发测试,无法发布产品.若需要发布产 ...

  10. 1502: [NOI2005]月下柠檬树 - BZOJ

    Description Input 文件的第1行包含一个整数n和一个实数alpha,表示柠檬树的层数和月亮的光线与地面夹角(单位为弧度).第2行包含n+1个实数h0,h1,h2,…,hn,表示树离地的 ...