leetcode_09_Palindrome Number (easy)

Palindrome Number

题目:

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.

不能使用额外空间,不能翻转因为可能溢出,说的我好怕怕>_<

多说无益,实践是检验真理的唯一方式,好的,让我们开始解题吧

直接讲头和尾进行比较,每次在头尾减少一位,就OK了,比较简单

class Solution {
public:
    bool isPalindrome(int x) {
        if(x<0)return false;
        int max = 1;
        while(x/10/max>=1){
            max*=10;
        }
        bool flag = true;
        for(int i=10;max>=i;max/=100){
            if(x%i==x/max){
                x= (x-(x/max*max+x%i))/10;
            }else{
                flag = false;
                break;
            }
        }
       return flag;
    }
};

时间: 2024-10-26 21:33:14

leetcode_09_Palindrome Number (easy)的相关文章

Div3 C good number easy version

good numbers easy version #include <iostream> #include <cmath> using namespace std; int main() { int q, n; while(cin >> q){ while(q--){ cin >> n; int m = n; int i = 0;//i用来计数,计算n需要加多少个1才能变成good number while(1){ bool flag = true;//f

[LeetCode] 009. Palindrome Number (Easy) (C++/Java/Python)

索引:[LeetCode] Leetcode 题解索引 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 009.Palindrome_Number (Easy) 链接: 题目:https://oj.leetcode.com/problems/palindrome-number/ 代码(github):https://github.com/illuz/leetcode 题意: 判断一个数是否是回文数. 分析: 按自己想

LeetCode: 171 Excel Sheet Column Number(easy)

题目: Related to question Excel Sheet Column Title Given a column title as appear in an Excel sheet, return its corresponding column number. For example: A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 代码: 1 class Solution { 2 public: 3

171. Excel Sheet Column Number (Easy)

Given a column title as appear in an Excel sheet, return its corresponding column number. For example: A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 思路:1.26进制转化为十进制,ord()函数:2.逐个读入字符串中的每个字符进行处理转换: class Solution(): def titleToNumber(

【leetcode】Excel Sheet Column Title &amp; Excel Sheet Column Number (easy)

Given a positive integer, return its corresponding column title as appear in an Excel sheet. For example: 1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA 28 -> AB 思路: 相当于10进制转26进制.与一般不一样的是10进制对应的是0 - 9.而这个26进制对应的是 A(1)- Z(26), 没有0. 我的代码: strin

【leetcode】Palindrome Number (easy)

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: 136 Single Number(easy)

题目: Given an array of integers, every element appears twice except for one. Find that single one. Note:Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 代码: 1 class Solution { 2 public: 3 int s

leetcode palindrome number(easy) /java

题: 把x翻转后,判断是否与x相等.需考虑溢出.但是如果此数翻转后溢出,那么说明其不是回文数. 推理入下,如果翻转后的数溢出且是回文数,那么原数也溢出.矛盾. public class Solution { public boolean isPalindrome(int x) { if(x<0) return false; int y=0,z=x; while(x!=0) { y=y*10+x%10; x=x/10; } return y==z; } } 以及复数不是回文数.

9. Palindrome Number - Easy

Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward. Example 1: Input: 121 Output: true Example 2: Input: -121 Output: false Explanation: From left to right, it reads -121. From right to