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 ...
随机推荐
- passing parameters by value is inefficient when the parameters represent large blocks of data
Computer Science An Overview _J. Glenn Brookshear _11th Edition_C Note that passing parameters by va ...
- 2认识HTML中的“ML”:深入理解超文本
HTML是描述网页结构的标记语言(即HTML中的'ML'),而HT指把一个网页链接到其他网页. <a>元素可以创建超文本链接到另外一个网页,<a>元素中的内容在网页中是可点击的 ...
- Apache Kafka源码分析 - autoLeaderRebalanceEnable
在broker的配置中,auto.leader.rebalance.enable (false) 那么这个leader是如何进行rebalance的? 首先在controller启动的时候会打开一个s ...
- Oracle 安装 INS-30131错误。
需要学习SDE配置相关知识,其中Oracle数据库安装遇到错误INS-30131,虽然未能最终解决,但找到了初步的思路,记录下来给大家提供参考.下文对很多知识的理解可能存在错误或不够精准,仅作参考. ...
- Windows 下 Nginx + PHP + Xdebug + PHPStorm 调试环境配置
前期条件:安装好 Nginx.PHP.PHPStorm,使得可以正常访问 一.为 PHP 安装 Xdebug 到 Xdebug 的官网(http://xdebug.org/download.php)下 ...
- Address localhost:1099 is already in use 的错误
http://blog.csdn.net/huazhongkejidaxuezpp/article/details/41813683
- oracle communities
应该常来这看看 https://www.oracle.com/communities/index.html
- (转)CAS (4) —— CAS浏览器SSO访问顺序图详解(CAS Web Flow Diagram by Example)
CAS (4) —— CAS浏览器SSO访问顺序图详解(CAS Web Flow Diagram by Example) tomcat版本: tomcat-8.0.29 jdk版本: jdk1.8.0 ...
- Device ID
参考文章 一.CFUUID (Deprecated) 二.UDID (Deprecated) 三.NSUUID (ios6.0 and later) NSString *uuid = [[NSUUID ...
- AppDelegate
一.基础知识 1) main.m指定了程序的入口点 UIApplicationMain(argc, argv,nil,NSStringFromClass([StartingPointAppDelega ...