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 ...
随机推荐
- Kubernetes 实战 —— 01. Kubernetes 介绍
简介 P2 Kubernetes 能自动调度.配置.监管和故障处理,使开发者可以自主部署应用,并且控制部署的频率,完全脱离运维团队的帮助. Kubernetes 同时能让运维团队监控整个系统,并且在硬 ...
- pyhont+unittest的测试固件
在执行一条自动化测试用例时需要做一些测试前的准备工作和测试后的清理工作,如:创建数据库链接.启动服务进程.打开文件.打开浏览器.测试环境的清理.关闭数据链接.关闭文件等.如果每执行一条用例都需要编写上 ...
- 【转载】Android的事件分发(dispatchTouchEvent),拦截(onInterceptTouchEvent)与处理(onTouchEvent)
出处:https://blog.csdn.net/caifengyao/article/details/65437695 在Android中,View的结构是树状的,所以,当触发触摸事件的时候,其事件 ...
- 在windows 下查看ip 地址和 在ubundu 下查看IP地址
在windows 下查看ip 地址和 在ubundu 下查看IP地址 1.在windows 下查看 IP地址:ipconfig 2.在 ubundu 下查看IP地址:ifconfig
- Slenium详解
Slenium介绍 Selenium 是一个 Web 应用的自动化框架. 通过它,我们可以写出自动化程序,像人一样在浏览器里操作web界面. 比如点击界面按钮,在文本框中输入文字 等操作. 而且还能从 ...
- 《Asp.Net Core3 + Vue3入坑教程》 - 6.异常处理与UserFriendlyException
简介 <Asp.Net Core3 + Vue3入坑教程> 此教程适合新手入门或者前后端分离尝试者.可以根据图文一步一步进操作编码也可以选择直接查看源码.每一篇文章都有对应的源码 目录 & ...
- 翻译:《实用的Python编程》05_00_Overview
目录 | 上一节 (4 类和对象) | 下一节 (6 生成器) 5. Python 对象的内部工作原理 本节介绍 Python 对象的内部工作原理.来自其它语言的程序员通常会发现 Python 的类概 ...
- 翻译:《实用的Python编程》06_02_Customizing_iteration
目录 | 上一节 (6.1 迭代协议) | 下一节 (6.3 生产者/消费者) 6.2 自定义迭代 本节探究如何使用生成器函数自定义迭代. 问题 假设你想要自定义迭代模式. 例如:倒数: >&g ...
- 快速创建你的第一个Spring Boot项目
1. 创建工程 打开idea,利用Spring Boot搭建一个web工程,切身体会一下Spring Boot所带来的魅力!看看SpringBoot是如何快速搭建一个web项目. New-->P ...
- Android学习之CoordinatorLayout+FloatingActionButton+Snackbar
CoordinatorLayout •简介 CoordinatorLayout 协调布局,可以理解为功能更强大的 FrameLayout 布局: 它在普通情况下作用和 FrameLayout 基本一致 ...