leetcode:Palindrome Number (判断数字是否回文串) 【面试算法题】
题目:
Determine whether an integer is a palindrome. Do this without extra space.
Could negative integers be palindromes? (ie, -1)
If you 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.
题意判断数字是否是回文串,只能用常数空间,不能转换成字符串,不能反转数字。
先计算数字的位数,分别通过除法和取余,提取出要对比的对应数字。
class Solution {
public:
bool isPalindrome(int x) {
if(x<0)return 0;
int n=0,temp=x,big=1,small=1;
while(temp!=0)
{
n++;
temp/=10;
if(temp)big*=10;
}
for(int i=0;i<n/2;++i)
{
if((x/big)%10!=(x/small)%10)return 0;
big/=10;
small*=10;
}
return 1;
}
};
// http://blog.csdn.net/havenoidea
leetcode:Palindrome Number (判断数字是否回文串) 【面试算法题】的更多相关文章
- 9 Palindrome Number(判断是否为回文数Easy)
题目意思:判断是否为回文数,不许使用额外空间 ps:一直不理解额外空间的意思,int能用吗 思路:1.比较头尾 2.翻转,越界问题需考虑 class Solution { public: bool i ...
- [Leetcode] Palindrome number 判断回文数
Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. S ...
- 《LeetBook》leetcode题解(5):Longest Palindromic [M]——回文串判断
我现在在做一个叫<leetbook>的免费开源书项目,力求提供最易懂的中文思路,目前把解题思路都同步更新到gitbook上了,需要的同学可以去看看 书的地址:https://hk029.g ...
- [LeetCode] Longest Palindromic Substring 最长回文串
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- LeetCode(125):验证回文串
Easy! 题目描述: 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写. 说明:本题中,我们将空字符串定义为有效的回文串. 示例 1: 输入: "A man, ...
- leetcode 每日签到 409. 最长回文串
题目: 最长回文串 给定一个包含大写字母和小写字母的字符串,找到通过这些字母构造成的最长的回文串. 在构造过程中,请注意区分大小写.比如 "Aa" 不能当做一个回文字符串. 注意: ...
- 判断最长回文串——暴力、延展、Manacher
1. 暴力 时间复杂度O(n^3). 2. 延展 以某一字符为中心,设置left, right两个变量同时向外扩,判断他们指向字符是否相同.注意分奇偶讨论.时间复杂度O(n^2). 3. Manach ...
- HDU 4632 Palindrome subsequence(区间dp,回文串,字符处理)
题目 参考自博客:http://blog.csdn.net/u011498819/article/details/38356675 题意:查找这样的子回文字符串(未必连续,但是有从左向右的顺序)个数. ...
- Palindrome - URAL - 1297(求回文串)
题目大意:RT 分析:后缀数组求回文串,不得不说确实比较麻烦,尤其是再用线段数进行查询,需要注意的细节地方比较多,比赛实用性不高......不过练练手还是可以的. 线段数+后缀数组代码如下: ...
随机推荐
- mockito学习
mockito学习 写一个测试用例,如果在测试类上面添加了注解@RunWith(SpringJUnit4ClassRunner.class),必须添加@ContextConfiguration(&qu ...
- phpcms v9中模板标签使用及联动菜单
{template "content","header"} 调用根目录下phpcms\template\content\header文件 {charset} 字 ...
- php与http协议
1.预定义变量$_SERVER $_SERVER 是一个包含了诸如头信息(header).路径(path).以及脚本位置(script locations)等等信息的数组. 可以再后台输出 f ...
- 64位系统中开启32位应用,特别是OLEDB
IIS7 - Running 32-bit and 64-bit ASP.NET versions at the same time on different worker processes IIS ...
- Python Socket Programming
本文介绍使用Python进行Socket网络编程,假设读者已经具备了基本的网络编程知识和Python的基本语法知识,本文中的代码如果没有说明则都是运行在Python 3.4下. Python的sock ...
- 找出指定目录下,大于指定大小的文件(LINUX SHELL)
当前目录下: find ./ -size +2048k |xargs du -b|awk '{print $1/1024/1024 "M" $2}'|sort -n ...... ...
- DOCKER脚本一例---快速建立大批测试机
这个会由一系列的脚本构成,比如: 系统重启后,如何快速恢复服务,如何建立网桥(也可一次写入),如何在新系统上快速部署. ADDBRIDGE #!/bin/sh br_name=br100 brctl ...
- LeetCode _ Gas Station
There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...
- Cracking the coding interview--Q1.4
原文 Write a method to replace all spaces in a string with'%20'. You may assume that the string has su ...
- Altium Designer 等长线&&蛇形线
Altium Designer 里面怎么画等长线 (1)一般是将走线布完后,新建一个class. Design -> Classes 如上图添加完后可以点击close. (2)快捷键 T + R ...