博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Leetcode: Strobogrammatic Number III
阅读量:5956 次
发布时间:2019-06-19

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

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.

1 public class Solution { 2     public int strobogrammaticInRange(String low, String high) { 3         int lowlen = low.length(); 4         int highlen = high.length(); 5         List
res = new ArrayList
(); 6 for (int i=lowlen; i<=highlen; i++) { 7 res.addAll(findStrobogrammatic(i)); 8 } 9 int i=0;10 int count=res.size();11 while(i
=0 && res.get(i).length()==high.length()){19 if(res.get(i).compareTo(high)>0){20 count--;21 }22 i--;23 }24 return count;25 }26 27 char[] dict = new char[]{'0', '1', '6', '8', '9'};28 29 public List
findStrobogrammatic(int n) {30 List
res = new ArrayList
();31 if (n <= 0) return res;32 build(res, "", n);33 return res;34 }35 36 public void build(List
res, String item, int n) {37 if (item.length() == n) {38 res.add(item);39 return;40 }41 boolean last = (item.length()==n-1);42 for (int i=0; i

曾经这样写,但是TLE

1         for (String str : res) {2             if ((str.length()>low.length() || str.compareTo(low)>=0) && (str.length()

 

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

你可能感兴趣的文章
扩展easyui 的表单验证
查看>>
MySQL锁之一:锁详解
查看>>
选择29部分有用jQuery应用程序插件(免费点数下载)
查看>>
JS类的封装及实现代码
查看>>
HDOJ 3480 Division
查看>>
BeanFactory、ApplicationContext、ApplicationContextAware区别
查看>>
关于WEB Service&WCF&WebApi实现身份验证之WCF篇(2)
查看>>
HDU2586 How far away ?(LCA模板题)
查看>>
点我吧工作总结(技术篇) Velocity
查看>>
IOS-线程(GCD)
查看>>
Ehcache详细解读(转)
查看>>
Android游戏之平台接入的一点记录
查看>>
源码编译php5.4 ./configure参数
查看>>
13、Cocos2dx 3.0游戏开发找小三之3.0中的Director :郝萌主,一统江湖
查看>>
超人学院Hadoop大数据技术资源分享
查看>>
Oracle迁移:Linux->Windows
查看>>
【转】利用mybatis-generator自动生成代码
查看>>
C# 将MSMQ消息转换成Json格式 【优化】
查看>>
传纸条(一)(双线程dp)
查看>>
bootstrap精简教程
查看>>