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

解题思路一:

双指针法,逐位判断

Java代码如下:

  1. static public boolean isPalindrome(int x) {
  2. if (x < 0)
  3. return false;
  4. int temp = xbeginIndex = 0, endIndex = 0;
  5. while (temp >= 10) {
  6. temp /= 10;
  7. endIndex++;
  8. }
  9. while (beginIndex < endIndex) {
  10. if ((int)((x / Math.pow(10,beginIndex))%10) != (int)((x / Math.pow(10,endIndex))%10))
  11. return false;
  12. beginIndex++;
  13. endIndex--;
  14. }
  15. return true;
  16. }

解题思路二:

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

C++:

  1. #include<algorithm>
  2. using namespace std;
  3. class Solution {
  4. public:
  5. bool isPalindrome(int x) {
  6. if (x < )
  7. return false;
  8. long longx = x, palindromex = ;
  9. while (x) {
  10. palindromex = palindromex * + x % ;
  11. x /= ;
  12. }
  13. return longx == palindromex;
  14. }
  15. };

【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. 腾讯云CentOS7安装LNMP+wordpress

    许多云主机都有学生优惠,于是我趁着现在大一买了个腾讯1元云主机+免费cn域名(高中生的话就别想了).鉴于我只知道用服务器安装博客,别的用途不了解,所以我就去安装wordpress. 而由于我看的教程有 ...

  2. bzoj3555 企鹅QQ

    3555: [Ctsc2014]企鹅QQ Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 1640  Solved: 613 Description P ...

  3. maven运行javaWeb项目

    首先从svn下载下来的maven项目,需要点击项目,然后import--->Existing Maven Projects->全选之后点next就转换成功了,然后 run as--> ...

  4. 使用Jquery+EasyUI 进行框架项目开发案例讲解之四 组织机构管理源码分享

    http://www.cnblogs.com/huyong/p/3404647.html 在上三篇文章  <使用Jquery+EasyUI进行框架项目开发案例讲解之一---员工管理源码分享> ...

  5. xcrun: error: active developer path ("/Volumes/Xcode/Xcode-beta.app/Contents/Developer") does not exist, use `xcode-select --swi

    xcrun: error: active developer path ("/Volumes/Xcode/Xcode-beta.app/Contents/Developer") d ...

  6. java中获取路径中的空格处理(%20)问题

    在java中获取文件路径的时候,有时候会获取到空格,但是在中文编码环境下,空格会变成“%20”从而使得路径错误. 解决办法: String path = Parameter.class.getReso ...

  7. linux安全加固(1)

    Redhat是目前企业中用的最多的一类Linux,而目前针对Redhat攻击的黑客也越来越多了.我们要如何为这类服务器做好安全加固工作呢? 一. 账户安全 1.1 锁定系统中多余的自建帐号 检查方法: ...

  8. 用80x86汇编语言编程:1 + 2 + 3 + 4 + 5 + …… + n,和小于100,在屏幕上显示次数和结果。

    ;============================================== ;1+...+n < 100 ;--------------------------------- ...

  9. motto6

    当你发现自己不错的时候,要适当的给自己加点凉水,让自己知道自己离“不错”还很远.

  10. pdo调用

    php单次调用,例题 <body> <?php //造DSN:驱动名:dbname=数据库名;host=服务器地址 $dsn = "mysql:dbname=mydb;ho ...