博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[LeetCode] 248. Strobogrammatic Number III
阅读量:6038 次
发布时间:2019-06-20

本文共 1762 字,大约阅读时间需要 5 分钟。

Problem

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.

Example:

Input: low = "50", high = "100"

Output: 3
Explanation: 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.

Solution

class Solution {    int count = 0;    public int strobogrammaticInRange(String low, String high) {        Map
map = new HashMap<>(); map.put('0', '0'); map.put('1', '1'); map.put('6', '9'); map.put('8', '8'); map.put('9', '6'); List
res = new ArrayList<>(); for (int len = low.length(); len <= high.length(); len++) { dfs(map, low, high, new char[len], 0, len-1, res); } System.out.println(res); return count; } private void dfs(Map
map, String low, String high, char[] buffer, int left, int right, List
res) { if (left > right) { String num = new String(buffer); if ((num.length() == low.length() && num.compareTo(low) < 0) || (num.length() == high.length() && num.compareTo(high) > 0)) { return; } count++; res.add(num); return; } for (char ch: map.keySet()) { if (buffer.length != 1 && left == 0 && ch == '0') continue; if (left == right && ch != map.get(ch)) continue; buffer[left] = ch; buffer[right] = map.get(ch); dfs(map, low, high, buffer, left+1, right-1, res); } }}

转载地址:http://pslhx.baihongyu.com/

你可能感兴趣的文章
快准车服完成3000万元A+轮融资,年底将开始B轮融资
查看>>
让我去健身的不是漂亮小姐姐,居然是贝叶斯统计!
查看>>
MySQL 数据约束
查看>>
我的友情链接
查看>>
SERVLET容器简介与JSP的关系
查看>>
《服务器SSH Public Key认证指南》-补充
查看>>
我的友情链接
查看>>
Java break continue return 的区别
查看>>
算法(Algorithms)第4版 练习 1.3.4
查看>>
jquery easyUI checkbox复选项获取并传后台
查看>>
浅析NopCommerce的多语言方案
查看>>
设计模式之简单工厂模式
查看>>
C++中变量的持续性、链接性和作用域详解
查看>>
2017 4月5日上午
查看>>
Google Chrome开发者工具
查看>>
第一阶段冲刺报告(一)
查看>>
使用crontab调度任务
查看>>
【转载】SQL经验小记
查看>>
zookeeper集群搭建 docker+zk集群搭建
查看>>
Vue2.5笔记:Vue的实例与生命周期
查看>>