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. 这题貌似解法挺多,直接用简单的把数倒置,没有考虑数 ...
随机推荐
- asp.net将object或string转为int
在C#强制转换中,(int),Int32.Parse() 和 Convert.toInt32() 三种方法有何区别? int 关键字表示一种整型,是32位的,它的 .NET Framework 类型为 ...
- android面试题之三
十一.对一些资源以及状态的操作保存,最好是保存在生命周期的哪个函数中进行? 若在onPause()中进行保存,相应需要在onResume()中进行恢复. 若在onStop()中进行保存,相应需要在on ...
- POJ 1472 Coins (多重背包+滚动数组)
Coins Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 25827 Accepted: 8741 Description Pe ...
- 正确的lnamp支持SSI的方法!即支持SHTML和include调用!
正确的lnamp支持SSI的方法!即支持SHTML和include调用! 个地方:一个是apache和nginx里的conf文件 第一步:修改apache里的httpd.conf文件 查找:AddTy ...
- Gradle 1.12 翻译——第十四章. 教程 - 杂七杂八
有关其它已翻译的章节请关注Github上的项目:https://github.com/msdx/gradledoc/tree/1.12,或訪问:http://gradledoc.qiniudn.com ...
- ARM
ARM全新架构:cortex架构 cortex-A:高端:cortex-R:实时嵌入式系统:cortex-m:廉价: 哈佛结构:数据总线和地址总线分开: 冯若依曼:地址总线,数据总线不分开: cort ...
- 移动端rem,scale动态设置
pt:物理像素(电容屏上像素块个数) px:逻辑像素.设备独立像素 高清屏:1px = 4pt 普通屏:1px = 1pt dpr:设备像素比:(某一方向上)物理像素/逻辑像素 通常设置1rem=屏幕 ...
- C++,对象成员的访问
成员变量和成员函数的访问可以采用以下几种访问方式:对象.成员变量名: 对象.成员函数名(实参列表)对象的指针->成员变量名; 对象的指针->成员函数名(实参列表)对象的引用.成员变量名对象 ...
- 机器学习笔记(二)- from Andrew Ng的教学视频
省略了Octave的使用方法结束,以后用得上再看吧 week three: Logistic Regression: 用于0-1分类 Hypothesis Representation: :Sigmo ...
- Basic DataList
一,效果图. 二,源代码. <!DOCTYPE html><html><head> <meta charset="UTF-8"> & ...