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 - 简单模拟
第一个题目是将整数进行反转,这个题实现反转并不难,主要关键点在于如何进行溢出判断.溢出判断再上一篇字符串转整数中已有介绍,本题采用其中的第三种方法,将数字转为字符串,使用字符串比较大小的方法进行比较. ...
随机推荐
- JavaScript基础学习第六天
目标: 能够使用对象的方式处理数据 ☞ 代码预解析: 1. 变量提升 :当程序中遇到定义变量后,就会将该变量的定义提升到当前作用域的开始位置,不包括变量的赋值 2. 函数提升:当程序中遇到函数的声明时 ...
- 100天搞定机器学习|Day9-12 支持向量机
机器学习100天|Day1数据预处理 100天搞定机器学习|Day2简单线性回归分析 100天搞定机器学习|Day3多元线性回归 100天搞定机器学习|Day4-6 逻辑回归 100天搞定机器学习|D ...
- 【Android】Fresco 初次使用遇到的坑
初次使用开源框架 Fresco,结果遇到了坑,被虐了半下午--暂且记下. 下面的错误 android.view.InflateException: Binary XML file line #** 报 ...
- 【iOS】Apple Mach-O Linker Error Linker command failed with exit code 1
又遇到了这个问题,貌似之前遇到过……如图所示: 解决方法寻找中………… 在 Stack Overflow 找到了解决方法,如下: 参考链接:Apple Mach-O Linker Error
- mysql5.7.18-winx64安装
win10下装mysql-5.7.18-winx64 步骤1 官网下载地址:https://dev.mysql.com/downloads/mysql/ 选择手动安装版: 解压到D盘mysql文件夹下 ...
- oracle的开窗函数
原创 select * from (select province, commodity, sum(price), ROW_NUMBER() OVER(PARTITION BY province o ...
- Wpf窗口设置屏幕居中最前显示
public Window() { InitializeComponent(); WindowStartupLocation = Win ...
- 1、大型项目的接口自动化实践记录--robotframework环境搭建
因为人力.团队技术问题,选用robotframework来做自动化,首先说下环境搭建 齐涛道长的入门教程非常棒:http://blog.csdn.net/tulituqi/article/detail ...
- Redis总结(八)如何搭建高可用的Redis集群
以前总结Redis 的一些基本的安装和使用,大家可以这这里查看Redis 系列文章:https://www.cnblogs.com/zhangweizhong/category/771056.html ...
- 创建String对象过程的内存分配
转载自 https://blog.csdn.net/xiabing082/article/details/49759071 常量池(Constant Pool):指的是在编译期被确定 ...