博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Palindrome Number
阅读量:6903 次
发布时间:2019-06-27

本文共 1058 字,大约阅读时间需要 3 分钟。

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

 

 to see which companies asked this question

Show Tags
Show Similar Problems
主要是获取整数的长度,然后两个指针分别从整数的两边进行比较,然后往中心移动,如果不相等就返回false,全部相等返回true
class Solution {public:    bool isPalindrome(int x) {        if (x < 0) {            return false;        }        int n = len_of_integer(x);        int left = n;        int right = 1;        while (left > right) {            int l_num = get_num_of_position(x, left);            int r_num = get_num_of_position(x, right);            if (l_num == r_num) {                left--;                right++;            } else {                return false;            }        }        return true;    }    int len_of_integer(int x) {        int n = 0;        while (x) {            x = x / 10;            n++;        }        return n;    }    int get_num_of_position(int x, int p) {        int m = pow(10, p);        int n = pow(10, p - 1);        int y = x % m;        return y / n;    }};

 

转载于:https://www.cnblogs.com/SpeakSoftlyLove/p/5097134.html

你可能感兴趣的文章
WEB打印控件Lodop(V6.x)
查看>>
我的友情链接
查看>>
UI集成测试运行说明
查看>>
ES与Javscript,JScript,ActionScript等脚本
查看>>
断点的技巧
查看>>
mariadb配置安装
查看>>
自己做网站怎么计算带宽需求
查看>>
流镜像,端口镜像
查看>>
3月23日作业
查看>>
C语言之枚举
查看>>
我的友情链接
查看>>
程序员学习能力提升三要素
查看>>
Mysqli的批量CRUD数据
查看>>
oracle 10g升级流程
查看>>
linux下DNS服务器的实现1
查看>>
BGinfo设置记录文档
查看>>
爆款打造之中小卖家如何做到零成本选/测款?(一)
查看>>
性能监测工具 dstat
查看>>
匿名无须交互输入用户名和密码的samba配置方法(security=user)
查看>>
我的友情链接
查看>>