[LeetCode] 248. Strobogrammatic Number III 对称数III
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的更多相关文章
- [LeetCode] 247. Strobogrammatic Number II 对称数II
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...
- [LeetCode] 248. Strobogrammatic Number III 对称数之三
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...
- [LeetCode] Strobogrammatic Number II 对称数之二
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...
- [LeetCode] 246. Strobogrammatic Number 对称数
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...
- [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 ...
- [LeetCode] 137. Single Number II 单独数 II
Given a non-empty array of integers, every element appears three times except for one, which appears ...
- [LeetCode] Strobogrammatic Number III 对称数之三
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...
- [Swift]LeetCode248.对称数 III $ Strobogrammatic Number III
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...
- 248. Strobogrammatic Number III
题目: A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at ups ...
随机推荐
- CentOS7安装Chrome
1. 进入官网:https://www.google.cn/intl/zh-CN/chrome/2. 点击下载3. 直接安装:sudo yum localinstall google-chrome-s ...
- 【MySQL】测试MySQL表中安全删除重复数据只保留一条的相关方法
第二篇文章测试说明 开发测试中,难免会存在一些重复行数据,因此常常会造成一些测试异常. 下面简单测试mysql表删除重复数据行的相关操作. 主要通过一下三个大标题来测试说明: 02.尝试删除dept_ ...
- C#各版本
C#各版本 本系列文章主要整理并介绍 C# 各版本的新增功能. C# 8.0 C#8.0 于 2019年4月 随 .NET Framework 4.8 与 Visual Studio 2019 一同发 ...
- js正则实现从一段复杂html代码字符串中匹配并处理特定信息
js正则实现从一段复杂html代码字符串中匹配并处理特定信息 问题: 现在要从一个复杂的html代码字符串(包含各种html标签,数字.中文等信息)中找到某一段特别的信息(被一对“|”包裹着),并对他 ...
- CentOS7.6 yum方式安装redis最新版
sudo yum install -y http://rpms.famillecollet.com/enterprise/remi-release-7.rpm sudo yum --enablerep ...
- Dubbo源码分析:设计总结
设计原则 1. 多用组合,少用继承 2. 针对接口编程,不针对实现编程 3. 依赖抽象,不要依赖具体实现类. 设计模式 1. 策略设计模式:Dubbo扩展Spring的xml标签解析 ...
- 用Wget下载的文件在哪里可以找到。。
输入命令: cd ~ 然后 ls 就ok了.
- 线性回归和Ridge回归
网址:https://www.cnblogs.com/pinard/p/6023000.html 线性回归和交叉验证 import matplotlib.pyplot as plt import nu ...
- JavaScript基础06——字符串
字符串的创建: 字符串的创建: var str = "hello world"; //常量,基本类型创建 var str2 = new String("hello wor ...
- PDB文件会影响性能吗?
有人问了这样的问题:"我工作的公司正极力反对用生成的调试信息构建发布模式二进制文件,这也是我注册该类的原因之一.他们担心演示会受到影响.我的问题是,在发布模式下生成符号的最佳命令行参数是什么 ...