866. Prime Palindrome

2023-05-16

866. Prime Palindrome

Find the smallest prime palindrome greater than or equal to N.

Recall that a number is prime if it’s only divisors are 1 and itself, and it is greater than 1.

For example, 2,3,5,7,11 and 13 are primes.

Recall that a number is a palindrome if it reads the same from left to right as it does from right to left.

For example, 12321 is a palindrome.

Example 1:

Input: 6
Output: 7

Example 2:

Input: 8
Output: 11

Example 3:

Input: 13
Output: 101

Note:

  • 1 <= N <= 10^8
  • The answer is guaranteed to exist and be less than 2 * 10^8.

solution1:timeout?

  public int primePalindrome(int N) {
        while(!isPrimePalindrome(N))
            N++;

        return N;
    }

    public boolean isPrimePalindrome(int n){
        int n0 = n;
        if(n == 1) return false;
        for(int i = 2; i <= n / 2; i++){
            if(n % i == 0)
                return false;
        }

        List<Integer> list = new ArrayList<>();
        while(n0 > 0){
            list.add(n0 % 10);
            n0 /= 10;
        }
        for(int i = 0; i <= list.size() / 2; i++){
            if(!(list.get(i) == list.get(list.size()-i-1)))
                return false;
        }

        return true;
    }

solution2

class Solution {
 public int primePalindrome(int N) {
       
        while(true){
            if(isPrime(N) && isPalindrome(N))
                return N;
            N++;
         if (10_000_000 < N && N < 100_000_000)
                N = 100_000_000;
        }


    }

    public boolean isPrime(int n){
        if(n == 1) return false;
        int middle =(int) Math.sqrt(n);
        for(int i = 2; i <= middle; i++){
            if(n % i == 0)
                return false;
        }
        return true;
    }

    public boolean isPalindrome(int n){
        int n0 = n;
        int n1 = 0;
        while(n > 0){
            n1 = n1 * 10 + n % 10;
            n = n / 10;
        }
       if(n1 == n0)
           return true;
        return false;
    }
}

判断是否为回文的时候,根据回文的性质,构造一个新的数和原来的数比较是否相等

同时注意一个性质,当N∈(107,108)时,N一定不是回文数,所以就直接跳过

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

866. Prime Palindrome 的相关文章

  • 9. Palindrome Number

    Palindrome Number Easy Determine whether an integer is a palindrome An integer is a palindrome when it reads the same ba
  • palindrome-partitioning

    题目 xff1a 给定一个字符串s xff0c 分割s使得s的每一个子串都是回文串 返回所有的回文分割结果 例如 给定字符串s 61 aab 返回 aa b a a b Given a string s partition s such t
  • Codeforces D. Prefix-Suffix Palindrome

    Codeforces D Prefix Suffix Palindrome 题解 xff1a 和D1相同 xff0c 区别是找中间的回文串要压缩时间 xff0c 用到了马拉车算法 xff08 算法介绍在下面 xff1a span class
  • Palindrome(补全回文串+最长公共子序列的应用)hdu1513+poj1159+动态规划

    Palindrome Time Limit 4000 2000 MS Java Others Memory Limit 65536 32768 K Java Others Total Submission s 4277 Accepted S
  • 检查字符串是否为回文

    A 回文是一个单词 短语 数字或其他单位序列 可以在任一方向以相同的方式阅读 为了检查一个单词是否是回文 我获取该单词的字符数组并比较字符 我测试了它 它似乎有效 但我想知道这是否正确或者是否有需要改进的地方 这是我的代码 public c
  • 使用正则表达式查找回文

    这个问题是为了试图理解以下答案之一 如何使用正则表达式检查字符串是否为回文 给出的答案马库斯 贾德罗 is 1 2 有人可以解释一下 这里到底发生了什么 我需要做类似的事情Perl 但无法理解这个解决方案 PS 我对 Perl 不太擅长 所
  • 寻找两个三位数乘积的最大回文数问题

    所以在欧拉计划中问题4陈述如下 回文数读起来是一样的 双向 制作的最大回文数 两个 2 位数字的乘积 数字是 9009 91 99 找到最大的回文 两个 3 位数字的乘积 我已经尝试过以下方法 include
  • 使用 JavaScript 进行递归回文检查

    我试图使用 javascript 通过递归来找出字符串是否是回文 但我无法弄清楚代码中缺少什么 var firstCharacter function str return str slice 0 1 var lastCharacter f
  • Python 回文

    所以我的任务是查看并检查正整数是否是回文 我已经正确完成了所有工作 但在最后的部分需要帮助 从用户给出的回文中生成一个新的回文的任务 我的 while 循环走在正确的轨道上还是应该使用其他东西 所以结果是如果你输入 192 它会返回Gene
  • 使用 Java 的回文测试器,忽略空格和标点符号

    我已经编写了程序 直到它必须忽略线程中的标点符号和空格 我想知道是否有人可以帮助我编码 我一直在尝试的似乎不起作用 这是我到目前为止所拥有的 import java util Scanner public class PalindromeT
  • 不同回文子串的数量

    给定一个字符串 我知道如何找到回文子串的数量使用 Manacher 算法在线性时间内完成 但现在我需要找到数量独特 独特回文子串 现在 这可能会导致 O n n 2 算法 一个 n 用于查找所有此类子字符串 而 n 2 用于将这些子字符串中
  • 检查字符串是否为回文

    我有一个字符串作为输入 必须将字符串分成两个子字符串 如果左子串等于右子串 则执行一些逻辑 我怎样才能做到这一点 Sample public bool getStatus string myString 例子 myString ankYkn
  • 在 Coq 中证明可逆列表是回文

    这是我对回文的归纳定义 Inductive pal X Type list X gt Prop pal0 pal pal1 forall x X pal x pal2 forall x X l list X pal l gt pal x l
  • 最长回文子串和后缀 trie

    我在谷歌上搜索了一个相当著名的问题 即 the longest palindromic substring我发现推荐后缀尝试的链接可以很好地解决该问题 例子SO https stackoverflow com questions 70437
  • 从文本文件打印所有回文

    我看了这个问题 BASH 回文检查器 https stackoverflow com questions 26601234 bash palindrome checker 这就是该线程中问题答案所显示的内容 grep E a z 3 45
  • 如何在不使用 C++ 中的字符串函数的情况下检查字符串是否为回文

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我的任务是创建一个程序来检查输入的字符串是否是回文 所以我在这里有这个简单的程序 include
  • 递归函数:检查 Java 中的回文数

    我有一个类检查字符串是否是回文 我有两个问题 1 这是检查回文的最有效方法吗 2 这可以递归实现吗 public class Words public static boolean isPalindrome String word Stri
  • python 和回文

    我最近写了一个循环的方法 usr share dict words并使用我的返回回文列表ispalindrome x 方法 这是一些代码 有什么问题吗 它只会停止 10 分钟 然后返回文件中所有单词的列表 def reverse a ret
  • 检查一个数字是否是回文数

    我尝试使用以下代码检查一个数字是否是回文 unsigned short digitsof unsigned int x unsigned short n 0 while x x 10 n return n bool ispalindrome
  • 正则表达式引擎如何解析具有递归子模式的正则表达式?

    此正则表达式匹配回文 1 2 我无法理解它是如何工作的 递归何时结束 以及正则表达式何时从递归子模式中断并转到 part Thanks 编辑 抱歉我没有解释 2 and 1 1 指第一个子模式 对其自身 2 反向引用第二个子模式的匹配 即

随机推荐

  • Linux权限详解,多用户多组各种权限配置原理

    网上有太多关于Linux权限详解 xff0c 这里不啰嗦那些 主要解释下这些权限是杂用的 xff0c 否则知道了什么用户 组之类的权限也配不好 一 权限分类 r xff1a 读取权限 xff0c 数字代号为 34 4 34 w xff1a
  • 1.tow sum

    文章目录 题目c 43 43 版本java版本利用hashmap正确做法 题目 Two Sum Easy Given an array of integers return indices of the two numbers such t
  • 2. Add Two Numbers

    2 Add Two Numbers Medium 59971568Share You are given two non empty linked lists representing two non negative integers T
  • Linux VNC server的安装及简单配置使用

    Linux VNC server的安装及简单配置使用 转 xff1a http www 2cto com os 201309 241104 html Linux VNC server的安装及简单配置使用 摘要 xff1a Linux vnc
  • 3. Longest Substring Without Repeating Characters

    Longest Substring Without Repeating Characters Given a string find the length of the longest substring without repeating
  • 4. Median of Two Sorted Arrays

    Median of Two Sorted Arrays Hard There are two sorted arrays nums1 and nums2 of size m and n respectively Find the media
  • 7. Reverse Integer

    文章目录 Reverse IntegersolutionAlgorithm Reverse Integer Easy Given a 32 bit signed integer reverse digits of an integer Ex
  • 8. String to Integer (atoi)

    String to Integer atoi Implement atoi which converts a string to an integer The function first discards as many whitespa
  • 9. Palindrome Number

    Palindrome Number Easy Determine whether an integer is a palindrome An integer is a palindrome when it reads the same ba
  • 1071. Greatest Common Divisor of Strings

    1071 Greatest Common Divisor of Strings Easy 30985Share For strings S and T we say 34 T divides S 34 if and only if S 61
  • 这个问题搞了我一天

  • 150逆波兰式

    文章目录 150 Evaluate Reverse Polish NotationSolution 150 Evaluate Reverse Polish Notation Medium Evaluate the value of an a
  • 收到礼物最大值

    题目描述 在一个m n的棋盘的每一个格都放有一个礼物 xff0c 每个礼物都有一定价值 xff08 大于0 xff09 从左上角开始拿礼物 xff0c 每次向右或向下移动一格 xff0c 直到右下角结束 给定一个棋盘 xff0c 求拿到礼物
  • 64. Minimum Path Sum

    64 Minimum Path Sum Given a m x n grid filled with non negative numbers find a path from top left to bottom right which
  • 找出亲密对数

    题目内容 xff1a 求数n之内的亲密对数 所谓 亲密对数 xff0c 即A的所有因子 xff08 包含1但不包含其本身 xff09 之和等于B xff0c 而B的所有因子之和等于A 输入格式 某个数字n 输出格式 xff1a 此数字n之内
  • 5. Longest Palindromic Substring

    5 Longest Palindromic Substring Given a string s find the longest palindromic substring in s You may assume that the max
  • 516. Longest Palindromic Subsequence

    516 Longest Palindromic Subsequence Given a string s find the longest palindromic subsequence s length in s You may assu
  • 第一讲_网站架构的演变及海量数据的解决方案

    文章目录 看透springMVC 读书笔记 第一讲单机类型CS结构 xff08 Client Server xff09 BS结构 xff08 Browser Server xff09 BS结构网络传输方式OSI七层模型 TCP IP四层模型
  • 10. Regular Expression Matching

    10 Regular Expression Matching Given an input string s and a pattern p implement regular expression matching with suppor
  • 866. Prime Palindrome

    866 Prime Palindrome Find the smallest prime palindrome greater than or equal to N Recall that a number is prime if it s