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. CentOS6.9下手动编译并安装Python3.7.0

    CentOS6.9默认安装的python版本为2.6.6,若想安装python3以上版本,只能手工编译安装 下面介绍Python-3.7.0版本的手动编译并安装的步骤 1.下载Python-3.7.0 ...

  2. PHP程序员最容易犯的Mysql错误

    对于大多数web应用来说,数据库都是一个十分基础性的部分.如果你在使用PHP,那么你很可能也在使用MySQL—LAMP系列中举足轻重的一份子. 对于很多新手们来说,使用PHP可以在短短几个小时之内轻松 ...

  3. npm run build打包时修改的路径

  4. jquery ajax请求数据超时设置

    var ajaxTimeoutTest = $.ajax({ url:'', //请求的URL timeout : 1000, //超时时间设置,单位毫秒 type : 'get', //请求方式,g ...

  5. LeetCode 1061. Lexicographically Smallest Equivalent String

    原题链接在这里:https://leetcode.com/problems/lexicographically-smallest-equivalent-string/ 题目: Given string ...

  6. 2019-2020-1 20199302《Linux内核原理与分析》第九周作业

    一.进程调度的时机 硬中断和软中断 中断:程序执行过程中的强制性转移到操作系统内核相应的处理程序,起到切出指令流的作用. 中断处理程序:与进程无关的内核指令流. 进程切换:切换两个进程的内核堆栈. 硬 ...

  7. Linux 字符集的查看及修改

    一·查看字符集 字符集在系统中体现形式是一个环境变量,以CentOS6.5为例,其查看当前终端使用字符集的方式可以有以下几种方式: 第一种: [root@Testa-www tmp]# echo $L ...

  8. MySQL 中间件 - DBLE 简单使用

    DBLE 是企业级开源分布式中间件,江湖人送外号 “MyCat Plus”:以其简单稳定,持续维护,良好的社区环境和广大的群众基础得到了社区的大力支持: 环境准备 DBLE项目资料   DBLE官方网 ...

  9. 使用WinDbg调试入门(内核模式)

    windbg是一个内核模式和用户模式调试器,包含在Windows调试工具中.这里我们提供了一些实践练习,可以帮助您开始使用windbg作为内核模式调试器. 设置内核模式调试 内核模式调试环境通常有两台 ...

  10. mysql linux上安装使用

    安装启动 安装之前可以看下系统中有没有已经安装. 查看所有软件:dpkg -l 1.查看mysql安装的版本 mysql --version 2.mysql状态 service mysql statu ...