Leetcode: Palindrome Numbers

尝试用两头分别比较的方法,结果发现无法解决1000021这种问题


 1 public class Solution {
2 public boolean isPalindrome(int x) {
3 if(x<0) return false;
4 int lastbit=x%10;
5 int firstbit;
6 int num=x;
7 int bits=0;
8 while(num/10!=0){
9 num=num/10;
10 bits++;
11 }
12 firstbit=num;
13 if(firstbit!=lastbit) return false;
14 else{
15 x=x-firstbit*((int)(Math.pow(10,bits)));
16 x=x/10;
17 if(x==0) return true;
18 else return isPalindrome(x);
19 }
20 }
21 }

查看了论坛的解答,看到一个很好的solution, 它怎么想到13行的
div/=100的,给跪了,这样刚好解决了中间有0的问题,比如1021会被判定为false, 而121会被判定为true。 解答详见:http://leetcode.com/2012/01/palindrome-number.html


 1 public class Solution {
2 public boolean isPalindrome(int x) {
3 if (x < 0) return false;
4 int div = 1;
5 while (x / div >= 10) {
6 div *= 10;
7 }
8 while (x != 0) {
9 int l = x / div;
10 int r = x % 10;
11 if (l != r) return false;
12 x = (x % div) / 10;
13 div /= 100;
14 }
15 return true;
16 }
17 }

时间: 2024-07-29 10:12:56

Leetcode: Palindrome Numbers的相关文章

[LeetCode] Palindrome Partition [11]

题目 Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. For example, given s = "aab", Return [ ["aa","b"], ["a","a",&q

[leetcode]Palindrome Partitioning @ Python

原题地址:https://oj.leetcode.com/problems/palindrome-partitioning/ 题意: Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. For example, given s = "aab",Return [

LeetCode——Palindrome Partition

Palindrome Partitioning Given a string s, partition s such that every substring of the partition is a palindrome. Return all possible palindrome partitioning of s. For example, given s = "aab", Return [ ["aa","b"], ["a&q

[LeetCode] [Palindrome Number 2012-01-04]

Determine whether an integer is a palindrome. Do this without extra space. if use recursive, like check the first dig and last dig, then remove them, check the rest, it will fail when digint like "1021", when remove the first and last one, the r

Leetcode | Palindrome

Valid Palindrome Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases. For example,"A man, a plan, a canal: Panama" is a palindrome."race a car" is not a palindrome. Note:Have you

LeetCode——Palindrome Number

Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using extra space. You could also

LeetCode: Palindrome 回文相关题目

LeetCode: Palindrome 回文相关题目汇总 LeetCode: Palindrome Partitioning 解题报告 LeetCode: Palindrome Partitioning II 解题报告 Leetcode:[DP]Longest Palindromic Substring 解题报告 LeetCode: Valid Palindrome 解题报告

[LeetCode] Palindrome Number [13]

题目 Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using

Uva - 12050 Palindrome Numbers【数论】

题目链接:uva 12050 - Palindrome Numbers 题意:求第n个回文串 思路:首先可以知道的是长度为k的回文串个数有9*10^(k-1),那么依次计算,得出n是长度为多少的串,然后就得到是长度为多少的第几个的回文串了,有个细节注意的是, n计算完后要-1! 下面给出AC代码: 1 #include <bits/stdc++.h> 2 typedef long long ll; 3 using namespace std; 4 const int maxn=3010; 5