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. 这题貌似解法挺多,直接用简单的把数倒置,没有考虑数 ...
随机推荐
- perl post 带上请求头
my $url='https://www.zjcap.cn/business/dispatch_post.do?action=submitAdminLogin'; my $res = $ua-> ...
- 基于visual Studio2013解决C语言竞赛题之0402奇偶求和
题目 解决代码及点评 这道题考察我们对循环和判断的综合应用 #include <stdio.h> #include <stdlib.h> #include < ...
- hadoop的WordCount样例
package cn.lmj.mapreduce; import java.io.IOException; import java.util.Iterator; import org.apache.h ...
- 【Eclipse】报错提示删掉@Override
是因为项目的JRE System Library版本不对,点击Edit进入Edit Library 界面,因为项目默认是使用Eclipse自带的jdk版本(Workspace default JRE) ...
- Xcode使用source control 时提示the server certificate failed to verify 的解决办法
wusipingdeMacBook-Pro:~ railgun$ wusipingdeMacBook-Pro:~ railgun$ svn ls https://13.13.13.134:8443/s ...
- js回调函数2
使用sublime结合node.js来写js代码特别爽,具体网上有教程.详见:http://blog.csdn.net/dududu01/article/details/42804367 ,其中最主要 ...
- 2014 HDU多校弟五场J题 【矩阵乘积】
题意很简单,就是两个大矩阵相乘,然后求乘积. 用 Strassen算法 的话,当N的规模达到100左右就会StackOverFlow了 况且输入的数据范围可达到800,如果变量还不用全局变量的话连内存 ...
- 模拟Struts2的AOP实现
在Struts2中有拦截器的概念,通过它的拦截器可以拦截Action.Struts2的拦截器是通过AOP来实现的,在Spring也有类似的概念.下面的我们先来比较一下Struts2和Spring中AO ...
- c语言指针数组与数组指针
一.指针数组和数组指针的内存布局初学者总是分不出指针数组与数组指针的区别.其实很好理解:指针数组:首先它是一个数组,数组的元素都是指针,数组占多少个字节由数组本身决定.它是“储存指针的数组”的简称.数 ...
- 基于visual Studio2013解决算法导论之022队列实现(基于链表)
题目 基于链表的队列实现 解决代码及点评 #include <stdio.h> #include <stdlib.h> #include <time.h> ...