leetcode9 Palindrome Number(按进阶要求)
题目描述
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 left, it becomes 121-. Therefore it is not a palindrome.
Example 3:
Input: 10
Output: false
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
Follow up:
Coud you solve it without converting the integer to a string?
#include<math.h>
class Solution {
public:
bool isPalindrome(int x) {
if(x<0)
return false;
if(x>=0&&x<=9)
return true;
int weishu=0;
int temp=x;
while(temp) //先看一下数字总共有几位
{
temp=temp/10;
weishu++;
}
int jishu=0; //看一下是奇数还是偶数
jishu=weishu%2;
int mowei=1;
int shouwei=weishu+1-mowei; //shouwei代表着要和末位对比的位置,而不是真正的首位
if(jishu)
{
while(x&&shouwei!=1){
int a=pow(10,shouwei-1); //这个动作是为了取shouwei,先把后面的位数去了
if((x/a%10)!=(x%10)) //(x/a%10)是shouwei,(x%10)是末位
return false;
x=x/10;
shouwei-=2; //对比完一对,shouwei的位置就减2,因为前面的指针要往后移一位,最后一位抛弃,相当于移了2位
}
}else{ //偶数个位数也一样,区别就是shouwei剩一个还是0个
while(x&&shouwei!=0){
int a=pow(10,shouwei-1);
if((x/a%10)!=(x%10))
return false;
x=x/10;
shouwei-=2;
}
}
return true;
}
};
总结
按要求不转成string来做的。没看题解,我感觉我想的还挺巧妙的,就是提交情况来看,似乎在通过中算比较慢的,但比较省内存。我估计是int转string的方法时间复杂度低。
然后看了题解,发现把数转成回文,然后看是否相等就可以了。瞬间又觉得自己有点蠢。
leetcode9 Palindrome Number(按进阶要求)的更多相关文章
- LeetCode9 Palindrome Number
题意: Determine whether an integer is a palindrome. Do this without extra space. (Easy) 分析: 自己考虑的方法是利 ...
- 65. Reverse Integer && Palindrome Number
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, re ...
- 有趣的数-回文数(Palindrome number)
文章转自http://blog.163.com/hljmdjlln@126/blog/static/5473620620120412525181/ 做LC上的题"Palindrome num ...
- 9. Palindrome Number
/* Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers ...
- No.009 Palindrome Number
9. Palindrome Number Total Accepted: 136330 Total Submissions: 418995 Difficulty: Easy Determine whe ...
- 【LeetCode】9 & 234 & 206 - Palindrome Number & Palindrome Linked List & Reverse Linked List
9 - Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. Som ...
- leetcode 第九题 Palindrome Number(java)
Palindrome Number time=434ms 负数不是回文数 public class Solution { public boolean isPalindrome(int x) { in ...
- HDU 5062 Beautiful Palindrome Number(数学)
主题链接:http://acm.hdu.edu.cn/showproblem.php? pid=5062 Problem Description A positive integer x can re ...
- Reverse Integer - Palindrome Number - 简单模拟
第一个题目是将整数进行反转,这个题实现反转并不难,主要关键点在于如何进行溢出判断.溢出判断再上一篇字符串转整数中已有介绍,本题采用其中的第三种方法,将数字转为字符串,使用字符串比较大小的方法进行比较. ...
随机推荐
- 分析了京东内衣销售记录,告诉你妹子们的真Size!
>今天闲暇之余写了一个爬虫例子.通过爬虫去爬取京东的用户评价,通过分析爬取的数据能得到很多结果,比如,哪一种颜色的胸罩最受女性欢迎,以及中国女性的平均size(仅供参考哦~) 打开开发者工具-n ...
- [NLP] 相对位置编码(二) Relative Positional Encodings - Transformer-XL
参考: 1. Transformer-XL: Attentive Language Models Beyond a Fixed-Length Context https://arxiv.org/pdf ...
- 基于tess4j的图片文字提取
1.文件结构目录 2.具体实现 ①添加maven依赖 <dependency> <groupId>net.sourceforge.tess4j</groupId> ...
- lr参数化
为什么做参数化? 数据库校验:注册用户时会看数据库有没有这个账号 应用程序校验:pc端qq登陆,一个账号只能登陆一台电脑 1.数据库或应用程序提交值的唯一性校验 数据库查询过程: 1.语法检查.语义检 ...
- VS Code 安装 LeetCode 插件
练习算法绕不开的一个网站就是力扣,很多小伙伴为了拿到大厂 offer,刷题都刷到吐了. 然而如果直接在 LeetCode 上写代码,那是很痛苦的一件事,那就相当于用 txt 写代码一样,没有 IDE ...
- Android通过辅助功能实现抢微信红包原理简单介绍
简书文章:https://www.jianshu.com/p/e1099a94b979 附抢红包开源项目地址,代码已全改为Kotlin了,已适配到最新微信7.0.5版本,如果对你有所帮助赏个star吧 ...
- java8-流的操作
流的操作 流的使用一般包括三件事: 一个数据源来执行一个查询; 一个中间操作链,形成一条流的流水线; 一个终端操作,执行流水线,并能生成结果 中间操作 操作 类型 返回类型 操作参数 函数描述符 fi ...
- [nghttp2]压测工具,源码编译并进行deb打包过程
编译环境:deepin 15.11桌面版 nghttp2下载地址:https://github.com/nghttp2/nghttp2 环境要求 emm只能在类Linux环境才能完整编译,想在Wind ...
- 48.QT-网络通信讲解1
网络概念 MAC地址(硬件地址) 网络IP地址(如192.168.1.101) 网络端口(实现多路通信,用来给不同应用程序来区分使用,范围0~65535,比如浏览网页服务(80端口), FTP服务(2 ...
- WebSphere MQ性能调优浅谈
导读:目前随着我们在中国的WebSphere MQ(MQSeries)用户数量越来越多,越来越多的用户开始对MQ使用时的性能优化问题提出要求,我根据日常积累的经验谈一谈在MQ性能优化方面应该考虑的因素 ...