Last active
September 23, 2018 01:02
-
-
Save Helenyin123/15b6763fe63c5f3fb60f6f1abebdc1c7 to your computer and use it in GitHub Desktop.
906.Super Palindrome
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Solution { | |
| public int superpalindromesInRange(String L, String R) { | |
| Long l = (long)Math.sqrt(Long.parseLong(L)); | |
| Long r = (long)Math.sqrt(Long.parseLong(R)); | |
| int re = 0; | |
| for(int odd = 0;odd <= 1;odd++){ | |
| long core = 1; | |
| long num = getP(core,odd == 0); | |
| while(num <= r){ | |
| if(num >= l){ | |
| if(isP(num*num))re++; | |
| } | |
| num = getP(++core,odd == 0); | |
| } | |
| } | |
| return re; | |
| } | |
| private boolean isP(long num){ | |
| long n = num; | |
| long tmp = 0; | |
| while(num > 0){ | |
| tmp = tmp *10 + num%10; | |
| num = num/10; | |
| } | |
| return tmp == n; | |
| } | |
| private long getP(long core,boolean isOdd){ | |
| long re = core; | |
| if(isOdd){ | |
| if(re < 10)return re; | |
| else re /= 10; | |
| } | |
| while(core > 0){ | |
| re = re *10 +core%10; | |
| core /= 10; | |
| } | |
| return re; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 = "" + l; | |
| int len = s.length(); | |
| List<Long> cands = new LinkedList<>(); | |
| cands.add((long)Math.pow(10, len) - 1); | |
| String half = s.substring(0, (len + 1) / 2); | |
| String nextHalf = "" + (Long.valueOf(half) + 1); | |
| String reverse = new StringBuilder(half.substring(0, len / 2)).reverse().toString(); | |
| String nextReverse = new StringBuilder(nextHalf.substring(0, len / 2)).reverse().toString(); | |
| cands.add(Long.valueOf(half + reverse)); | |
| cands.add(Long.valueOf(nextHalf + nextReverse)); | |
| long result = Long.MAX_VALUE; | |
| for (long i : cands) { | |
| if (i >= l) { | |
| result = Math.min(result, i); | |
| } | |
| } | |
| return result; | |
| } | |
| 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; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment