百度质量部电面一面算法题

来源:互联网 发布:蘑菇街互刷软件 编辑:程序博客网 时间:2024/05/16 05:17

        题目不难,意思是让我们判断一个数是否为回文数,时间复杂度为O(n),空间复杂度为O(1)。

       我想到的是递归逆序求和,将和与原数比较,打印输出。

       代码如下:

       

#include <iostream>#include <cstdio>#include <cstdlib>using namespace std;static long long ans=0;long long getN(long long n){    if(n==0)        return ans;    ans=ans*10+n%10;    return getN(n/10);}int main(){    long long x;    while(scanf("%lld",&x)==1)    {        if(x%10==0)//剪枝,去掉个位数为0的情况        {            printf("NO\n");        }        else if(x==getN(x))        {            printf("YES\n");        }        else        {            printf("NO\n");        }        ans=0;//重置0    }    return 0;}

0 0