Leetcode: Strobogrammatic Number 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.
Strobogrammatic Number II give us all the strings with length n, then we can call it to get strings with length between low.length() and high.length(), and remove the strings that less than low and larger than high.
public class Solution {
public int strobogrammaticInRange(String low, String high) {
int lowlen = low.length();
int highlen = high.length();
List<String> res = new ArrayList<String>();
for (int i=lowlen; i<=highlen; i++) {
res.addAll(findStrobogrammatic(i));
}
int i=0;
int count=res.size();
while(i<res.size() && res.get(i).length()==low.length()){
if(res.get(i).compareTo(low)<0){
count--;
}
i++;
}
i=res.size()-1;
while(i>=0 && res.get(i).length()==high.length()){
if(res.get(i).compareTo(high)>0){
count--;
}
i--;
}
return count;
}
char[] dict = new char[]{'0', '1', '6', '8', '9'};
public List<String> findStrobogrammatic(int n) {
List<String> res = new ArrayList<String>();
if (n <= 0) return res;
build(res, "", n);
return res;
}
public void build(List<String> res, String item, int n) {
if (item.length() == n) {
res.add(item);
return;
}
boolean last = (item.length()==n-1);
for (int i=0; i<dict.length; i++) {
char c = dict[i];
if ((n!=1 && item.length()==0 && c=='0') || (last && (c=='6' || c=='9')))
continue;
StringBuffer buf = new StringBuffer(item);
append(buf, last, c);
build(res, buf.toString(), n);
}
}
public void append(StringBuffer buf, boolean last, char c) {
if (c == '6') buf.insert(buf.length()/2, "69");
else if (c == '9') buf.insert(buf.length()/2, "96");
else {
buf.insert(buf.length()/2, last? c : ""+c+c);
}
}
}
曾经这样写,但是TLE
for (String str : res) {
if ((str.length()>low.length() || str.compareTo(low)>=0) && (str.length()<high.length() || str.compareTo(high)<=0))
count++;
}
Leetcode: Strobogrammatic Number III的更多相关文章
- [LeetCode] 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] Strobogrammatic Number 对称数
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...
- LeetCode Strobogrammatic Number II
原题链接在这里:https://leetcode.com/problems/strobogrammatic-number-ii/ 题目: A strobogrammatic number is a n ...
- LeetCode Strobogrammatic Number
原题链接在这里:https://leetcode.com/problems/strobogrammatic-number/ 题目: A strobogrammatic number is a numb ...
- [Locked] Strobogrammatic Number & Strobogrammatic Number II & Strobogrammatic Number III
Strobogrammatic Number A strobogrammatic number is a number that looks the same when rotated 180 deg ...
- [LeetCode] 248. Strobogrammatic Number III 对称数之三
A strobogrammatic number is a number that looks the same when rotated 180 degrees (looked at upside ...
- [LeetCode] 248. Strobogrammatic Number III 对称数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 ...
随机推荐
- Manipulating Data Structures
Computer Science An Overview _J. Glenn Brookshear _11th Edition We have seen that the way data struc ...
- 批处理快速创建wifi
为什么要用cmd这种古老的东西创建wifi呢,电脑管家.360安全卫士都有这种插件,一键开启关闭,多方便啊! 开始用的也是电脑管家的免费wifi插件,但是我越来越不能忍它极慢的启动关闭过程,每一次看着 ...
- 费用性支出预分摊form方式和web方式区别
预分摊 1 form方式 费用性支出先通过生产资产行请求 生成一个资产行,如果该资产行分摊到两个资产上则分割成两个资产行,既所谓的针对资产行进行分摊. 2 web方式 费用性支出直接分摊到资产上形成资 ...
- JQuery..bind命名空间
先看手册,由于bind方法有三个参数(type,[data],fn),所以手册上这么介绍: .bind() 方法是用于往文档上附加行为的主要方式.所有JavaScript事件对象, 比如focus, ...
- Redis学习笔记(9)-管道/分布式
package cn.com; import java.util.Arrays; import java.util.List; import redis.clients.jedis.Jedis; im ...
- 大数据情况下linux的配置
一:配置的大纲 主要的配置有几个方面: 主机名 IP 网络映射 增加新用户 给新用户root的权限,方便实验 关闭防火墙 安全子系统需要关闭 二:主机名的配置 命令:vi /etc/sysconfig ...
- qt如何实现一个渐隐窗口呢(开启的时候他是从上往下渐渐显示)
qt如何实现一个渐隐窗口呢?就是比如说开启的时候他是从上往下渐渐显示的,关闭的时候从下往上渐渐小时的http://stackoverflow.com/questions/19087822/how-to ...
- 【Android开发学习笔记】【第一课】初识New Project,工程文件介绍
初学者新建一个Andriod工程后,往往不知道Pakage Explorer区域的每个文件是什么作用,今天学习了一下,自我总结一下. 1.先新建一个工程 2.输入名称,以及支持的SDK版本等(这些可以 ...
- jdk 8 lambda表达式 及在Android Studio的使用示例
以前觉得java让人觉得有趣的一个特点是支持:匿名内部类,而最近发现jdk8已支持lambda并有更简便的方式,来实现匿名内部类. 这会让程序员更舒服,更喜欢java. 多年前觉得java语法和C#语 ...
- Mongoose在向集合中插入文档时的集合命名问题
Mongoose使用结构化的模式应用到MongoDB集合,为MongoDB Node.js原生驱动程序提供了更多的功能和简化了数据库操作. 从创建连接到向数据库中写入一个条数据经历了以下步骤: 1.连 ...