906. Super Palindromes
Let's say a positive integer is a superpalindrome if it is a palindrome, and it is also the square of a palindrome.
Now, given two positive integers
LandR(represented as strings), return the number of superpalindromes in the inclusive range[L, R].
Example 1:
Input: L = "4", R = "1000"
Output: 4
Explanation: 4, 9, 121, and 484 are superpalindromes.
Note that 676 is not a superpalindrome: 26 * 26 = 676, but 26 is not a palindrome.
Note:
1 <= len(L) <= 181 <= len(R) <= 18LandRare strings representing integers in the range[1, 10^18).int(L) <= int(R)
Approach #1: Math. [Java]
class Solution {
public int superpalindromesInRange(String L, String R) {
Long l = Long.valueOf(L), r = Long.valueOf(R);
int result = 0;
for (long i = (long)Math.sqrt(l); i * i <= r;) {
long p = nextP(i);
if (p * p <= r && isP(p * p)) {
result++;
}
i = p + 1;
}
return result;
}
private long nextP(long l) {
String s = Long.toString(l);
int N = s.length();
String half = s.substring(0, (N + 1) / 2);
String reverse = new StringBuilder(half.substring(0, N/2)).reverse().toString();
long first = Long.valueOf(half + reverse);
if (first >= l) return first;
String nextHalf = Long.toString(Long.valueOf(half) + 1);
String reverseNextHalf = new StringBuilder(nextHalf.substring(0, N/2)).reverse().toString();
long second = Long.valueOf(nextHalf + reverseNextHalf);
return second;
}
private boolean isP(long l) {
String s = "" + l;
int i = 0, j = s.length() - 1;
while (i < j) {
if (s.charAt(i++) != s.charAt(j--)) {
return false;
}
}
return true;
}
}
Reference:
Calculating the sqrt of nums from [L, R] (denote with 's')
finding the front half of 's' (denote with 'half')
the combination of the front half and its reverse (denote with 'p')
if p * p >= l and p * p <= r:
judge p * p is palindromes or not
else :
Calculating the combination of the front half + 1 and its reverse (denote with 'p')
Reference:
https://leetcode.com/problems/super-palindromes/discuss/170774/Java-building-the-next-palindrome
906. Super Palindromes的更多相关文章
- [LeetCode] 906. Super Palindromes 超级回文数
Let's say a positive integer is a superpalindrome if it is a palindrome, and it is also the square o ...
- 【LeetCode】906. Super Palindromes 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 BFS解法 相似题目 参考资料 日期 题目地址:ht ...
- [Swift]LeetCode906. 超级回文数 | Super Palindromes
Let's say a positive integer is a superpalindrome if it is a palindrome, and it is also the square o ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
- leetcode hard
# Title Solution Acceptance Difficulty Frequency 4 Median of Two Sorted Arrays 27.2% Hard ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- URAL 2040 Palindromes and Super Abilities 2 (回文自动机)
Palindromes and Super Abilities 2 题目链接: http://acm.hust.edu.cn/vjudge/contest/126823#problem/E Descr ...
- Ural 2040. Palindromes and Super Abilities 2 回文自动机
2040. Palindromes and Super Abilities 2 题目连接: http://acm.timus.ru/problem.aspx?space=1&num=2040 ...
- 回文树(回文自动机) - URAL 1960 Palindromes and Super Abilities
Palindromes and Super Abilities Problem's Link: http://acm.timus.ru/problem.aspx?space=1&num=19 ...
随机推荐
- 后端程序员之路 17、LaTeX公式
之前的文章写了两个公式:d(x,y)=\sqrt{\sum_{i=1}^{n}(x_i-y_i)^2} H_x=-\sum_{i=1}^{n}p(x_i)\log_{2}{p(x_i)} LaTex ...
- C++入门(1):计算机组成
系列文章尽在 | 公众号:lunvey 学习C++之前,我们有必要了解一下计算机的简单组成,毕竟C++是需要操作内存的一门语言.大家或许知道内存是什么,但是内存怎么读取和操作数据以及数据的表现形式会不 ...
- dubbo使用和配置讲解
1. 分布式系统中相关概念 1.1 互联网项目特点及目标 1.1.1 特点: 用户多 流量大.并发高 海量数据 易受攻击 功能繁琐 变更快 1.1.2 指标及相关目标 互联网项目三高目标:高并发.高可 ...
- .NET并发编程-反应式编程
本系列学习在.NET中的并发并行编程模式,实战技巧 本小节开始学习反应式编程.本系列保证最少代码呈现量,虽然talk is cheap, show me the code被奉为圭臬,我的学习习惯是,只 ...
- android分析之mutex
Android的锁是对Linux锁的一种包装: // ------------------------------------------------------------------------- ...
- react第三方库
作者:慕课网链接:https://www.zhihu.com/question/59073695/answer/1071631250来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请 ...
- CSS-clear属性的作用
1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="U ...
- Visual Studio添加引用的方式
- gsoap多wsdl集成
gsoap常规用法: 通过wsdl文件创建头文件 //通过wsdl文件创建头文件 wsdl2h [options] -o file.h ... WSDL and XSD files or URLs t ...
- tips 【总结】
需求 移入a标签把对应的详情显示出来并且根据位置判断,当前详情是否超出父级可视区范围,如果超出就定位的距离方向应该正好在父级可视区范围内 需求分析: 需要用到: offsetLeft 获取外边框到 ...