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.

246. Strobogrammatic Number和247. Strobogrammatic Number II的拓展,求一个区间的数里有多少个对称数。有了247一题作基础,这里我们可以先用迭代方法求出所有长度满足的数,再通过与low,high的compare比较,选出最终的结果并count。

Java:

public class Solution {
public List<String> helper(int n, int max) {
if (n == 0) return new ArrayList<String>(Arrays.asList(""));
if (n == 1) return new ArrayList<String>(Arrays.asList("1", "8", "0")); List<String> list = helper(n - 2, max);
List<String> result = new ArrayList<String>();
for (int i = 0; i < list.size(); i++) {
String s = list.get(i);
if (n != max) result.add("0" + s + "0");
result.add("1" + s + "1");
result.add("8" + s + "8");
result.add("6" + s + "9");
result.add("9" + s + "6");
}
return result;
} public int strobogrammaticInRange(String low, String high) {
int count = 0;
List<String> result = new ArrayList<String>();
for (int i = low.length(); i <= high.length(); i++) {
result.addAll(helper(i, i));
}
for (String str : result) {
if (str.length() == low.length() && str.compareTo(low) < 0 || str.length() == high.length() && str.compareTo(high) > 0) continue;
count++;
}
return count;
}
}

  

Java:

public class Solution {
private int count = 0;
private Map<Character, Character> map = new HashMap<>(); public int strobogrammaticInRange(String low, String high) {
if (low == null || low.length() == 0 || high == null || high.length() == 0) {
return 0;
} fillMap(); for (int n = low.length(); n <= high.length(); n++) {
char[] arr = new char[n];
getStrobogrammaticNumbers(arr, 0, n - 1, low, high);
} return count;
} private void getStrobogrammaticNumbers(char[] arr, int start, int end, String low, String high) {
if (start > end) {
String s = new String(arr);
if ((s.length() == 1 || s.charAt(0) != '0') && compare(low, s) && compare(s, high)) {
count++;
}
return;
} for (char c : map.keySet()) {
arr[start] = c;
arr[end] = map.get(c); if ((start < end) || (start == end && map.get(c) == c)) {
getStrobogrammaticNumbers(arr, start + 1, end - 1, low, high);
}
}
} // Return true if s1 <= s2
private boolean compare(String s1, String s2) {
if (s1.length() == s2.length()) {
if (s1.compareTo(s2) <= 0) {
return true;
} else {
return false;
}
} return true;
} private void fillMap() {
map.put('0', '0');
map.put('1', '1');
map.put('8', '8');
map.put('6', '9');
map.put('9', '6');
}
}

 

类似题目:

[LeetCode] 246. Strobogrammatic Number 对称数

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

All LeetCode Questions List 题目汇总

[LeetCode] 248. Strobogrammatic Number III 对称数III的更多相关文章

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

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

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

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

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

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

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

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

  5. [LeetCode] 260. Single Number III 单独数 III

    Given an array of numbers nums, in which exactly two elements appear only once and all the other ele ...

  6. [LeetCode] 137. Single Number II 单独数 II

    Given a non-empty array of integers, every element appears three times except for one, which appears ...

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

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

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

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

  9. 248. Strobogrammatic Number III

    题目: A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at ups ...

随机推荐

  1. Mac Docker安装MySQL5.7

    mkdir mysql 在~目录下创建mysql目录 docker run --restart=always --name mysql5.7 -p 3306:3306 -v ~/mysql:/var/ ...

  2. CentOS7安装Chrome

    1. 进入官网:https://www.google.cn/intl/zh-CN/chrome/2. 点击下载3. 直接安装:sudo yum localinstall google-chrome-s ...

  3. Linux TTY介绍

    1. TTY介绍 TTY(TeleType)指Linux中的一类终端(Terminal)设备, 是一种字符设备 在Linux中, tty可分为如下几类- 串行端口终端(serial port term ...

  4. 洛谷 P3605 [USACO17JAN]Promotion Counting晋升者计数

    题目描述 The cows have once again tried to form a startup company, failing to remember from past experie ...

  5. Bootstrap Method

    bootstrap方法是一种重采样技术,用于通过抽样数据集来估计总体统计数据.是一种面向应用的.基于大量计算的统计思维——模拟抽样统计推断. 它可以用来估计统计数据,例如平均值或标准差.在应用机器学习 ...

  6. 堆内存腐败异常(STATUS_HEAP_CORRUPTION---0xC0000374)

    什么是内存腐败 当堆内存位置的内容由于编程行为而被修改,超出了原始程序构造的意图时,计算机程序就会发生内存腐败,也可以叫内存破坏:这被称为违反内存安全.内存腐败的最可能原因是编程错误.当腐败的内存内容 ...

  7. Redis-5.0.5集群配置

    版本:redis-5.0.5 参考:http://redis.io/topics/cluster-tutorial. 集群部署交互式命令行工具:https://github.com/eyjian/re ...

  8. 微信小程序音乐播放器组件

    wxml <image bindtap="click" src="{{isPlay?'/images/':'/images/'}}"/> JS Pa ...

  9. Go字符串常用处理

    应用到strings包 /** * @Author: jadeshu * @Description: * @File: main * @Version: 1.0.0 * @Date: 2019/11/ ...

  10. Beta冲刺(5/5)

    队名:無駄無駄 组长博客 作业博客 组员情况 张越洋 过去两天完成了哪些任务 验收了小程序的主要功能 制作Beta展示所需要用到的视频 制作Beta展示PPT 准备Beta答辩 提交记录(全组共用) ...