1 Star 1 Fork 0

laodasbch/Leetcode-Complete-Guide

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
906.txt 1.51 KB
一键复制 编辑 原始数据 按行查看 历史
junbinLiang 提交于 2020-08-27 12:58 +08:00 . move
思路:
1. 首先此题的跑时降到 n^(1/4)
2. 因为superpalindrom 的 跟值 n^(1/2) 是palindrom, 所以我们只需要其前半部分即可(能推出后半部分) => n^(1/4)
代码:
class Solution {
public int superpalindromesInRange(String L, String R) {
int res=0;
long l=Long.parseLong(L);
long r=Long.parseLong(R);
long s=(long)(Math.sqrt(l))-10;
long e=(long)(Math.sqrt(r))+10;
for(long i=1;i<=e;i++){
String cur=i+"";
String pair[]=getP(cur);
long small=Long.parseLong(pair[0]);
small=small*small;
long big=Long.parseLong(pair[1]);
big=big*big;
if(small>r)break;
if(small<=r&&small>=l&&isP(small+"")){
res++;
}
if(big<=r&&big>=l&&isP(big+"")){
res++;
}
}
return res;
}
public String[] getP(String s){
String res[]=new String[2];
StringBuilder rev=new StringBuilder(s);
res[1]=s+rev.reverse().toString();
rev=new StringBuilder(s.substring(0,s.length()-1));
res[0]=s+rev.reverse().toString();
return res;
}
public boolean isP(String s){
int l=0,r=s.length()-1;
while(l<r){
if(s.charAt(l)!=s.charAt(r))return false;
l++;r--;
}
return true;
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/laodasbch/Leetcode-Complete-Guide.git
git@gitee.com:laodasbch/Leetcode-Complete-Guide.git
laodasbch
Leetcode-Complete-Guide
Leetcode-Complete-Guide
master

搜索帮助