美文网首页
判断回文数字

判断回文数字

作者: 静水流深ylyang | 来源:发表于2018-12-03 20:57 被阅读0次

版权声明:本文为博主原创文章,转载请注明出处。
个人博客地址:https://yangyuanlin.club
欢迎来踩~~~~


  • Ppalindrome Number
    Determine whether an integer is a palindrome. Do this without extra space.
    Some hints:
    Could negative integers be palindromes? (ie, -1)
    If yu 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.
  • 题目大意:判断一个整数是否是回文数字,不要使用额外的空间。
  • 思路:反转这个整数,看与原来的数字是否相等。
  • 代码:
#include<iostream>
using namespace std;
// 方法一
bool isPalindrome(int x)
{
    if(x<0 || x%10==0&&x!=0)return false;
    long y = x;
    long temp=0;
    while(y>0)
    {
        temp = temp*10+y%10;
        y/=10;
    }
    return temp==x;
}
int main()
{
    int x;
    cin>>x;
    if(isPalindrome(x))
    {
        cout<<"是回文数字"<<endl;
    }
    else
    {
        cout<<"不是回文数字"<<endl;
    }
    return 0;
}
    //方法二
    bool isPalindrome(int x)
    {
        if(x<0)return false;
        char ch[100];
        int k=0;
        while(x>0)
        {
            ch[k++] = char(x%10+'0');
            x/=10;
        }
        ch[k]='\0';
        k--;
        for(int i=0;i<k;i++,k--)
        {
            if(ch[i]!=ch[k])return false;
        }
        return true;
    }
  • 以上。

版权声明:本文为博主原创文章,转载请注明出处。
个人博客地址:https://yangyuanlin.club
欢迎来踩~~~~


相关文章

  • leetcode

    题目:判断一个数字是否问回文数 负数不是回文数

  • 判断回文数字

    版权声明:本文为博主原创文章,转载请注明出处。个人博客地址:https://yangyuanlin.club欢迎来...

  • 判断回文数字

    不转字符串的方式 转字符串后用双指针的形式

  • 第七周ARTS

    Algorithmic 回文数字的判断 负数不算,结尾为0的数字不算。 利用x/10得出反转后的数字,之后判断相等...

  • Leetcode-Easy-9 Palindrome Numbe

    题目 思路 判断一个给定的数字是否是回文数字。需要注意的是,不仅奇数会出现回文数字,偶数也可以,2442,2222...

  • 035-判断一个字符串是否是回文

    描述 判断一个由字母、数字和空格组成的字符串是否是回文。 约束: ​ 空字符串为回文; 示例: ​ ...

  • LeetCode - #9 判断回文数字

    前言 我们社区陆续会将顾毅(Netflix 增长黑客,《iOS 面试之道》作者,ACE 职业健身教练。微博:@故胤...

  • 字符串进阶

    1.反转字符串 2.字符串包含问题 3.字符串转数字 4.判断是否为回文判断一条单向链表是不是“回文” 分析:对于...

  • 力扣系列(三):判断回文数

    给定一个整形数字,判断是否是回文串 回文串:回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。 例如1...

  • 字符串面试题总结

    规则判断 判断字符串是否符合整数规则 判断字符串是否符合浮点数规则 判断字符串是否符合回文字符串规则 数字运算in...

网友评论

      本文标题:判断回文数字

      本文链接:https://www.haomeiwen.com/subject/rtmucqtx.html