Determine whether an integer is a palindrome. Do this without extra space.

解题思路一:

双指针法,逐位判断

Java代码如下:

	static public boolean isPalindrome(int x) {
if (x < 0)
return false;
int temp = x,beginIndex = 0, endIndex = 0;
while (temp >= 10) {
temp /= 10;
endIndex++;
}
while (beginIndex < endIndex) {
if ((int)((x / Math.pow(10,beginIndex))%10) != (int)((x / Math.pow(10,endIndex))%10))
return false;
beginIndex++;
endIndex--;
}
return true;
}

解题思路二:

计算出回文的值,然后判断回文的值是否等于自身:

C++:

 #include<algorithm>
using namespace std;
class Solution {
public:
bool isPalindrome(int x) {
if (x < )
return false;
long longx = x, palindromex = ;
while (x) {
palindromex = palindromex * + x % ;
x /= ;
}
return longx == palindromex;
}
};

【JAVA、C++】LeetCode 009 Palindrome Number的更多相关文章

  1. 【JAVA、C++】LeetCode 012 Integer to Roman

    Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 t ...

  2. 【JAVA、C++】LeetCode 005 Longest Palindromic Substring

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  3. 【JAVA、C++】LeetCode 002 Add Two Numbers

    You are given two linked lists representing two non-negative numbers. The digits are stored in rever ...

  4. 【JAVA、C++】LeetCode 022 Generate Parentheses

    Given n pairs of parentheses, write a function to generate all combinations of well-formed parenthes ...

  5. 【JAVA、C++】LeetCode 010 Regular Expression Matching

    Implement regular expression matching with support for '.' and '*'. '.' Matches any single character ...

  6. 【JAVA、C++】 LeetCode 008 String to Integer (atoi)

    Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...

  7. 【JAVA、C++】LeetCode 007 Reverse Integer

    Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return -321 解题思路:将数字 ...

  8. 【JAVA、C++】LeetCode 006 ZigZag Conversion

    The string "PAYPALISHIRING" is written in a zigzag pattern on a given number of rows like ...

  9. 【JAVA、C++】LeetCode 004 Median of Two Sorted Arrays

    There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two ...

随机推荐

  1. 【HDU 4150】Powerful Incantation

    题 题意 给你s1,s2两个字符串,求s1中有多少个s2 代码 #include<stdio.h> #include<string.h> int t,len1,len2,pos ...

  2. 【codevs1257】 打砖块

    http://codevs.cn/problem/1257/ (题目链接) 题意 在等腰三角形上打砖块,总共有m发炮弹,每块砖有一个权值,求打出的最大权值 Solution 今天考试题,考场上的2个小 ...

  3. Nginx Installation、Configuration、Rreverse Proxy、Load Balancing Learning

    目录 . Nginx简介 . Nginx安装部署 . Nginx安全配置 . Nginx反向代理实践 . Nginx负载均衡实践 1. Nginx简介 0x1: Nginx的基本特性 Nginx(&q ...

  4. C++ 中的名称冲突之 "y1"

    已经是第二次遇到这个问题了: #include <bits/stdc++.h> using namespace std; ); ][N][][N]; int x1, x2, y1, y2; ...

  5. eclipse中新建python项目报错:Project interpreter not specified

    eclipse-windows-preferences-python

  6. thinkphp ajax添加及删除

    写在前面的话:应客户需求需要给后台增加自助添加电影名称和链接的功能,添加后在微信前台能自动读取显示.开发步骤:1.由于是给后台添加一个增加电影及电影链接的功能,所以控制器在Admin下.在路径 App ...

  7. Android:View中的performClick()触发条件

    http://blog.sina.com.cn/s/blog_70ae1d7b0102v7uk.html 先看看performClick()源码:   public boolean performCl ...

  8. Java Web 设置默认首页

    一.问题描述 这里所谓的默认首页,是指在访问项目根目录时(如 http://localhost:8080/zhx-web/ )展示的页面,通过在web.xml里配置 <welcome-file- ...

  9. 批处理:echo的用法

    批处理:echo的用法 若要用 echo 命令显示一条命令,可用下述语法:  echo [message] 参数 ON|OFF   指定是否允许命令的回显.若要显示当前的 ECHO 的设置,可使用不带 ...

  10. 神经网络训练中的Tricks之高效BP(反向传播算法)

    神经网络训练中的Tricks之高效BP(反向传播算法) 神经网络训练中的Tricks之高效BP(反向传播算法) zouxy09@qq.com http://blog.csdn.net/zouxy09 ...