【Palindrome Number】cpp

题目:

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 extra space.

You could also try reversing an integer. However, if you have solved the problem "Reverse Integer", you know that the reversed integer might overflow. How would you handle such case?

There is a more generic way of solving this problem.

代码:

class Solution {
public:
        bool isPalindrome(int x)
        {
            if (x<0) return false;
            return x==Solution::reverse(x);
        }
        static int reverse(int x)
        {
            int ret = 0;
            while (x)
            {
                if ( ret>INT_MAX/10 || ret<INT_MIN/10 ) return 0;
                ret = ret*10 + x%10;
                x = x/10;
            }
            return ret;
        }
};

tips:

1. 如果是负数,认为不是回文数

2. 如果是非负数,则求这个数的reverse,如果等于其本身就是回文数,否则就不是。

利用的就是reverse integer这道题(http://www.cnblogs.com/xbf9xbf/p/4554684.html)。

====================================

还有一种解法可以不用reverse,有点儿类似求字符串是否是回文的题目(头尾两个指针往中间夹逼)。

class Solution {
public:
        bool isPalindrome(int x)
        {
            if (x<0) return false;
            int d = 1;
            while ( x/d>=10 ) d = d*10;
            while ( x>0 )
            {
                int high = x/d;
                int low = x%10;
                if (high!=low) return false;
                x = x%d/10;
                d = d/100;
            }
            return true;
        }
};

对于这道题的整数来说,就是找到给定整数的最大位数-1,即d。

每次从高位取一个数,从低位取一个数;然后,再把高低位都去掉获得新数(x=x%d/10);这样照比上一次的数少了两位,因此还有d=d/100。

时间: 2024-08-06 11:37:14

【Palindrome Number】cpp的相关文章

【Palindrome Partitioning】cpp

题目: 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

【Valid Number】cpp

题目: Validate if a given string is numeric. Some examples:"0" => true" 0.1 " => true"abc" => false"1 a" => false"2e10" => true Note: It is intended for the problem statement to be ambiguous.

【Letter Combinations of a Phone Number】cpp

题目: Given a digit string, return all possible letter combinations that the number could represent. A mapping of digit to letters (just like on the telephone buttons) is given below. Input:Digit string "23" Output: ["ad", "ae"

【Text Justification】cpp

题目: Given an array of words and a length L, format the text such that each line has exactly L characters and is fully (left and right) justified. You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pa

【Distinct Subsequences】cpp

题目: Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relat

【Gray Code】cpp

题目: The gray code is a binary numeral system where two successive values differ in only one bit. Given a non-negative integer n representing the total number of bits in the code, print the sequence of gray code. A gray code sequence must begin with 0

【Stirling Number】

两类Stirling Number的简介与区别(参考自ACdreamer的CSDN) Stirling Number I --- s(n,k):将n个物体排成k个非空循环排列(环)的方法数. 递推式:s(n, k) = (n-1)*s(n-1, k) + s(n-1, k-1); 1<= k<n 解释:考虑第n+1个元素1.单独形成循环排列,剩下的有s(n-1, k-1)种方法         2.和别的元素一起形成循环排列,n-1个元素形成k个循环排列的方法数是s(n-1,k),插入时共有n

【Maximum Subarray 】cpp

题目: Find the contiguous subarray within an array (containing at least one number) which has the largest sum. For example, given the array [−2,1,−3,4,−1,2,1,−5,4],the contiguous subarray [4,−1,2,1] has the largest sum = 6. 代码: class Solution { public:

【ZigZag Conversion】cpp

题目: The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like this: (you may want to display this pattern in a fixed font for better legibility) P A H N A P L S I I G Y I R And then read line by line: "PAHNAP