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 ...
随机推荐
- the core or essence of a computer
COMPUTER ORGANIZATION AND ARCHITECTURE DESIGNING FOR PERFORMANCE NINTH EDITION The ALU is that part ...
- Apache Kafka源码分析 - ReplicaStateMachine
startup 在onControllerFailover中被调用, /** * Invoked on successful controller election. First registers ...
- Delphi 如何操作外部程序的控件(如按钮,文本框,单选按钮等)
看你要做什么,比较现在网络很流行的QQ.MSN这些软件都屏蔽了,你可能还可以访问一些小软件的这些控制,思路及方案如下(API函数自己去百度查一下)1.得到你要这个窗口的句柄 使用FindWindow2 ...
- spring-3-mvc-hello-world-example
http://www.mkyong.com/spring3/spring-3-mvc-hello-world-example/
- go access database demo
package main import ( "database/sql" "fmt" _ "github.com/lib/pq" " ...
- .Net Install类的Install、Commit等事件触发顺序
.Net Install类的Install.Commit等事件触发顺序 空间 首先是Install其中调用base.Install过程中导致OnBeforeInstallOnAfterInstal ...
- 大数据下的java client连接JDBC
1.前提 启动hiveserver2服务 url,username,password 2.程序 3.结果 emp的第一列与第二列
- 介绍UDF,以及完成大小写的转换
一:概述 1.UDF 用户自定义函数,用java实现自定义的需求 2.UDF的类型 udf:一进一出 udaf:多进一出 udtf:一进多出 3.udf的实现步骤 继承UDF类 实现evaluate的 ...
- 移动设备优先viewport
Bootstrap 3 的设计目标是移动设备优先,然后才是桌面设备.这实际上是一个非常及时的转变,因为现在越来越多的用户使用移动设备. 为了让 Bootstrap 开发的网站对移动设备友好,确保适当的 ...
- magento
打开 magento/app/code/core/Mage/Core/Model/Session/Abstract/varien.php//if (isset($cookieParams['doma ...