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. BZOJ-1925 地精部落 烧脑DP+滚动数组

    1925: [Sdoi2010]地精部落 Time Limit: 10 Sec Memory Limit: 64 MB Submit: 1053 Solved: 633 [Submit][Status ...

  2. 【蒟蒻の进阶PLAN】 置顶+持续连载

    看到周围神犇们纷纷列计划,本蒟蒻也决定跟随他们的步伐,计划大约是周计划吧,具体怎么安排我也不确定.. 2015.12.30 刚刚学习完最基础的网络流,需要进行这方面的练习,从简到难,有空余的话尝试学习 ...

  3. mysql 中如何查找相同的数据

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAbcAAAEYCAIAAABQvy+HAAAAA3NCSVQICAjb4U/gAAAgAElEQVR4Xu

  4. BZOJ3626 LCA

    Description 给出一个n个节点的有根树(编号为0到n-1,根节点为0).一个点的深度定义为这个节点到根的距离+1. 设dep[i]表示点i的深度,LCA(i,j)表示i与j的最近公共祖先. ...

  5. 运行 appium 自带实例报错:unresolved import:webdriver

    python demo 中from appium import webdriver报错unresolved import:webdriver 之所以会报这样的error是因为没有装clientclie ...

  6. AppStore占坑注意事项

    AppStore占坑注意事项 我们会提前在AppStore(iTunesConnect)里注册一些应用名称,以满足未来业务需要和防止恶意注册,其中有一些需要注意的事情,整理如下: 倒计时180天 为了 ...

  7. 菲涅尔反射(Fresnel Reflection)

    离线渲染中,通常可以用kd,ks,kt(分别代表物体的漫反射系数,镜面反射系数,透射系数)来简单地描述一个物体的基本材质,例如,我们将一个物体设置为:kd=0,ks=0.1,kt=0.9,即代表一束光 ...

  8. zabbix 汉化

    zabbix2.x的版本自带汉化,3.x的版本也可以通过修改配置文件强制使用自带的汉化,但是不管哪种,翻译的精准度令人费解:偶然发现一个专门翻译zabbix的网站https://www.zabbix. ...

  9. JS实现上传本地图片前先预览

    <style type="text/css"> #preview /*这个就是预览的DIV的ID*/ { filter:progid:DXImageTransform. ...

  10. js中查找一个字符是否存在。

    <script> var a = 'd'; var re = a.indexOf('d'); ){ alert('存在'); } else { alert('不存在'); } </s ...