Determine whether an integer is a palindrome. Do this without extra space.
看到这个题目的时候,首先不认识 Determine这个单词。英文不好没办法,查了下是确认的意思,然后不懂
palindrome这个单词, 查了下是回文的意思。
问题是 回文是个什么东西,官方解释: A palindrome is
a word, phrase, number,
or other sequence of characters which
reads the same backward or forward. 回文
尽管英文不好,可是这个英文解释还是看懂了的。意思就是从前读到后面和倒过来读是一样的。
然后又不理解后面那句 do this without extra space. 大概意思是实现的时候不能使用其它空间,事实上还是不懂。
不知道第二个方法里的,Math.pow()这种方法的调用算不算使用其它空间。
public class palindrome { //using with extra space
public static boolean check(int x){
String temp = Integer.toString(x);
boolean flag = true;
for(int i=0;i<temp.length()/2;i++){
char a = temp.charAt(i);
char b = temp.charAt(temp.length()-i-1);
if(a!=b){
flag = false;
}
}
return flag;
} //using without extra space
public static boolean check2(int x){
if(x<0)
return false;
int n=1;
int temp = x;
while(temp/10!=0){
temp=temp/10;
n++;
}
for(int i=0;i<n/2;i++){
int a = i;
int b = n-1-i;
if(getInt(x,a)!=getInt(x,b)){
return false;
}
}
return true;
}
// 比方 896698 这个数字。要获取百位,用896698除以100。得到8966然后取余10得到6。即为百位的数值
private static int getInt(int x,int i){
int a = (int) Math.pow(10, i);
return (x/a)%10;
}
}
Determine whether an integer is a palindrome. Do this without extra space.的更多相关文章
- [Algorithms] Determine if a string is a palindrome
A palindrome is a string that reads the same forward and backward, for example, radar, toot, and mad ...
- 65. Reverse Integer && Palindrome Number
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, re ...
- Palindrome Number & Reverse Integer
Determine whether an integer is a palindrome. Do this without extra space. 分析:把一个数倒过来,然后看两个数是否相同. pu ...
- leetcode:Reverse Integer 及Palindrome Number
Reverse Integer Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, retur ...
- Leetcode 题目整理-3 Palindrome Number & Roman to Integer
9. Palindrome Number Determine whether an integer is a palindrome. Do this without extra space. clic ...
- 【leetcode】Palindrome Number
题目简述: Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could n ...
- No.009:Palindrome Number
问题: Determine whether an integer is a palindrome. Do this without extra space. 官方难度: Easy 翻译: 不使用额外空 ...
- Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space. public class Solution { p ...
- [Leetcode]Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space. 这题貌似解法挺多,直接用简单的把数倒置,没有考虑数 ...
随机推荐
- AnsiString 在 Delphi 中虽然不可用,但是,在 C++ 中可以用
[C++] C++ Builder 中 Ansi 编码的字符串在Android/iOS程序中显示的问题 呃,这个问题说起来,其实也不麻烦,C++ Builder 本身在 TEncoding 做了处理, ...
- vs2010 调试 调用堆栈 窗口
msdn 如何使用call stack窗口: http://msdn.microsoft.com/zh-cn/library/a3694ts5(v=vs.90).aspx 使用“调用堆栈”窗口可以查看 ...
- libpng causes error concerning pngconf.h
Bug Description Ubuntu Gutsy Gibbon 7.10 - libpng 1.2.15~beta5-2ubuntu0.1 (bug probably concerned wi ...
- Treasure Exploration(二分最大匹配+floyd)
Treasure Exploration Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 7455 Accepted: 3 ...
- veridata实验例(5)在更改主键列值,update操作将被分成两个语句
veridata实验例(5)更改主键列值,update操作将被分成两个语句 续接"veridata实验举例(4)验证veridata查找出updata.delete操作导致的不同步现象&qu ...
- ceph存储之查找对象
对象存储(而非块存储): 要把对象存入ceph集群,客户端必须做到: 1.指定对象名 2.指定存储池 Ceph客户端检出最新集群运行图,客户端用CRUSH算法动态地计算出如何把对象映射到归置组.然后计 ...
- ACE编译
(1)下载ACE6.1.0版本,路径如下 http://download.dre.vanderbilt.edu/previous_versions/ACE-6.1.0.tar.gz
- php 学习笔记 数组2
10.切割数组 array_slice(array, offset, length);返回一个由原始数组中的连续元素组成的新数组,参数1为原始数组,参数2为要复制的起始位置, 参数3要复制的个数:新数 ...
- 笔记之Cyclone IV第一卷第四章Cyclone IV器件中的嵌入式乘法器
嵌入式乘法器可以配置成一个 18 × 18 乘法器,或者配置成两个 9 × 9 乘法器.对于那些大于18 × 18 的乘法运算 ,Quartus II 软件会将多个嵌入式乘法器模块级联在一起.虽然没有 ...
- nginx access_log 完全关闭
最近在配置本地nginx开发环境时,发现一个问题,当server段不指定access_log时,并且http段中也未指定任何 access_log参数时,它会默认写到logs/access.log这个 ...