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. PHP-FPM的知识点

    https://blog.csdn.net/resilient/article/details/82420863 这个URL,将php的各种模式与知识点说清楚了. 因为php-fpm默认编译进了php ...

  2. Linux学习21-设置定时任务crontab

    前言 做自动化测试写的脚本需设置定时任务,在指定的时间去执行,这就需要用到定时任务.之前用jenkins可以在里面设置定时任务,很好用,其实不用jenkins,在linux上也可以用crontab做个 ...

  3. 账户(/etc/passwd、/etc/shadow)与组(/etc/group、/etc/gshadow)文件解析

    1. 账户信息文件 账户信息被保存在 /etc/passwd 文件中,通过命令 cat /etc/passwd 查看文件内容如下: [root@192 ~]# cat /etc/passwdroot: ...

  4. [USACO08OCT]:打井Watering Hole(MST)

    题意:有N个牧场,每个牧场修水井花费Wi,连接牧场花费Pij,问最小花费,使得每个牧场要么有水井,要么和有水井的牧场有通道. 思路:加一个格外的节点O,连接O表示修井,边权是修井的费用.     那么 ...

  5. 前端知识--控制input按钮的可用和不可用

    最近在项目的开发的时候,自己虽然是写后端的,但是,在开发核心的时候,前端的知识自己还是会用到的,多以前端这块自己由于好长时间都没有去看,所以几乎已经忘记的差不多了,现在也只能是想起一点记录一点,以便能 ...

  6. MySQL备份的三中方式

    一.备份的目的 做灾难恢复:对损坏的数据进行恢复和还原需求改变:因需求改变而需要把数据还原到改变以前测试:测试新功能是否可用 二.备份需要考虑的问题 可以容忍丢失多长时间的数据:恢复数据要在多长时间内 ...

  7. fitnesse wiki界面设置变量

    有时候我们可能多组测试数据会到同一个值,这样我们就可以设置一个变量,修改时只需要修改一个地方即可,而不需要对每组测试数据的这列数据进行修改 如下图: (1)定义变量:!define A {10}  , ...

  8. centos7.2(二)搭建lamp(Apache+PHP+Mysql环境)教程

    开始安装前,看说明. 说明0  查看服务器是否能被ssh登陆 http://tool.chinaz.com/port/ 如果显示关闭,说明被大陆封闭了,删除服务器重新建立一个. 说明1:Centos7 ...

  9. CF 768B

    CF 768B题意:In each operation Sam must remove any element x, such that x>1, from the list and inser ...

  10. 初识es

    初识es es是什么? es是基于Apache Lucene的开源分布式(全文)搜索引擎,,提供简单的RESTful API来隐藏Lucene的复杂性. es除了全文搜索引擎之外,还可以这样描述它: ...