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 ...
随机推荐
- delimiter
http://www.mysqltutorial.org/getting-started-with-mysql-stored-procedures.aspx The first command is ...
- what a fuck postgre update sql
================= what a fuck postgre update sql ================= UPDATE temp_group_temp set group_ ...
- PHP其它常用函数;<<<面向对象(OPP)的三大特性:封装、继承、加态:>>> <----面试题 ;构造方法、析构方法,魔术方法、set、get方法;静态;抽象类;接口
PHP其它常用函数: 赋值:$r->name = "元素"; 取值: echo $r->name; count() 计算数组中的元素数目或对象中 ...
- jquery全选+下拉+单选+事件+挂事件
1.全选 <body> <input type="checkbox" id="qx" /> 全选 <input type=&quo ...
- 利用HTML5的canvas制作万花筒动画特效
<!DOCTYPE HTML> <html> <head> <style> #canvas{ background-color:#cccccc; } & ...
- 兼容IE的CSS的”引入方式“
1.给IE浏览器的7版本来提供需要引用的样式(如果把7去掉则给所有的IE浏览器提供样式) <!--[if IE 7]> <Link type="text/css" ...
- 用Quartz处理定时执行的任务
这次做的项目中,有一部分功能需要实现定时执行.呃,这样说可能有点笼统,打个比方吧.例如用户在登录的时候,连续输错3次密码后,系统会将该用户冻结,不再允许该用户登录系统,等到了晚上零晨时分,再为所有被冻 ...
- Arbitrage---poj2240(floyd)
题目链接:http://poj.org/problem?id=2240 题意:有n个国家的,有m个关系,每个关系的格式是:A B C表示1单位的A国货币可以换B单位C国货币:求是否存在一种方法使得货币 ...
- sql 显示0001
- iOS Node Conflict svn冲突
当出现这个冲突时,应该是我add的文件,和同事处理的方法有冲突导致的. 这个的解决办法是:先revert这个文件,然后再update.