【Leet Code】Palindrome Number

Palindrome Number

Total Accepted: 19369 Total
Submissions: 66673My Submissions

Determine whether an integer is a palindrome. Do this without extra space.

判断一个数整数是不是回文?例如121,1221就是回文,好吧,直接利用前面写过的【Leet
Code】Reverse Integer——“%”你真的懂吗?

不过这里要考虑翻转后,数值溢出的问题,代码如下:

/*
//first method
class Solution {
public:
    bool isPalindrome(int x)
    {
        long long temp = x;
        long long  ret = 0;
        bool isNegative = false;
        if (temp < 0)
        {
            return false;
        }
        while (temp)
        {
            ret = ret * 10 + temp % 10;
            temp /= 10;
        }
        if(x == ret)
        {
            return true;
        }
        else
        {
            return false;
        }

    }
};
*/

当然,我们还有更好的方法,其实,初见这个题目,第一个想法是取头取尾进行比较,然后把头尾去掉,再循环,直到数值为个位数为止:

class Solution {
public:
    bool isPalindrome(int x)
    {
        if (x < 0)
        {
            return false;
        }
        int divisor = 1;
        while (x / divisor >= 10)
        {
            divisor *= 10;
        }
        while (x)
        {
            if (x / divisor != x % 10)
            {
                return false;
            }
            x = (x % divisor) / 10;
            divisor /= 100;
        }
        return true;
    }
};
时间: 2024-12-27 10:36:53

【Leet Code】Palindrome Number的相关文章

【Leet Code】Median of Two Sorted Arrays

Median of Two Sorted Arrays Total Accepted: 17932 Total Submissions: 103927My Submissions There are two sorted arrays A and B of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n

【Leet Code】ZigZag Conversion

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: "PAHNAPLSII

【Leet Code】Longest Substring Without Repeating Characters

Longest Substring Without Repeating Characters Total Accepted: 20506 Total Submissions: 92223My Submissions Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating le

【Leet Code】Add Two Numbers

Add Two Numbers Total Accepted: 20255 Total Submissions: 88115My Submissions You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numb

【Leet Code】Reverse Integer——“%”你真的懂吗?

Reverse Integer Total Accepted: 27372 Total Submissions: 68133My Submissions Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 题目咋看起来很简单,其实,真的很简单: class Solution { public: int reverse(int x) { int ret = 0; bo

【Leet Code】String to Integer (atoi) ——常考类型题

String to Integer (atoi) Total Accepted: 15482 Total Submissions: 106043My Submissions Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. If you want a challenge, please do not see below and ask yours

【Leet Code】Longest Palindromic Substring ——传说中的Manacher算法

Longest Palindromic Substring Total Accepted: 17474 Total Submissions: 84472My Submissions Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindr

【POJ 3974】 Palindrome

[POJ 3974] Palindrome Manacher纯模板题 忘记的时候可以拿来找感觉 代码如下: #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> using namespace std; char str[1111111]; char nwstr[2333333]; int p[2333333]; int Manacher() { int len

【POJ 1159】Palindrome

[POJ 1159]Palindrome 最近各种题各种奇葩思路已经司空见惯了...又新出个滚动数组= = 该题还有一点需要知道 最少需要补充的字母数 = 原序列S的长度 - S和S'的最长公共子串长度 然而窝原本并不知道--然后写出了一个奇葩dp做法 居然比LCS快0.0 我的思路是从左往右遍历 每个字符从右往左遍历到他的后一位置 dp数组标记当前位置往右对应匹配字符串左半边的最长序列长度的两倍(即为要删除序列长度) 每找到一个str[i]==str[j] 更新dp[j]为此时最长序列+2(左