9. 回文数 Palindrome Number

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

题意:判断一个数字是否为回文数

如何取得一个Integer的最高位?假设x = 688答:x / mask. 设置一个和x位数一样的mask,mask = 100,然后用x/mask,表示x里面有几个mask,即是最高位数字. 688里有6个100,即为6.

如何删去一个Integer的最高位?假设x = 688答:x = x % mask. 还是用这个mask,用 x = x % mask 即可得到688除以100的余数,这个余数其实等于删掉了x的最高位剩下的数.

如何取得一个Integer的最低位?假设x = 688答:x % 10.

如何删去一个Integer的最低位?假设x = 688答:x = x / 10.

  1. public class Solution {
  2. public bool IsPalindrome(int x) {
  3. if(x < 0) return false;
  4. if(x == 0) return true;
  5. int num = x;
  6. int div = 1;
  7. while(num >= 10){
  8. num /= 10;
  9. div *= 10;
  10. }
  11. while (x > 0) {
  12. int left = x / div;
  13. int right = x % 10;
  14. if (left != right){
  15. return false;
  16. }
  17. x = (x % div) / 10;
  18. div = div / 100;
  19. Console.WriteLine(x);
  20. Console.WriteLine(div);
  21. }
  22. return true;
  23. }
  24. }
  1. public boolean isPalindrome1(int x) {
  2. if (x<0 || (x!=0 && x%10==0)) return false;
  3. int rev = 0;
  4. while (x>rev){
  5. rev = rev*10 + x%10;
  6. x = x/10;
  7. }
  8. return (x==rev || x==rev/10);
  9. }

null

时间: 2024-08-05 19:37:02

9. 回文数 Palindrome Number的相关文章

[亚马逊amazon] 在线笔试题 大于非负整数N的第一个回文数 Symmetric Number

1.题目 如标题,求大于整数N(N>=0)的第一个回文数的字符串表示形式. 这个题目也是当时笔试第一次见到,花了一个小时才做出了.慢慢总结还是挺简单的. 2.分析 分析如下: (1)一位数N(9除外). 第一个大于N回文数等于N+1,如大于3的第一个回文数是4. (2)奇数位(一位数除外) 需要看“左边反转数字”是否大于"右边数字". 1)如果小于等于,则“左边+中间字母”组成的数字+1,再对称就可以. 2)如果大于,则左边数字直接对称到右边就可以啦. (3)偶数位 需要看“左边

大于非负整数N的第一个回文数 Symmetric Number

1.题目 如标题,求大于整数N(N>=0)的第一个回文数的字符串表示形式. 2.样例 1  --> 2 9  -->11 12345 -->12421 123456 -->124421 999 -->1001 3.分析 借用:http://www.cnblogs.com/xudong-bupt/p/4015226.html 4.代码 1 import java.util.Scanner; 2 3 4 public class SymmetricNumber { 5 6

回文数问题

问题描述 我在2008年6月写了一篇随笔"可以使用C#语言的在线ACM题库",其中提到 Sphere Onlile Judge (SPOJ) 网站.现在我们来看看该网站 SPOJ Problem Set (classical) 中的"5. The Next Palindrome".这道题目的主要内容如下所示: The Next Palindrome: A positive integer is called a palindrome if its represent

空间复杂度为O(1)的回文数判定算法

空间复杂度为O(1)的回文数判定算法 一.题设 实现空间复杂度为O(1)的回文数判定,输入为整型常数,要求输出判断是否为回文数. 要求格式如下: public boolean isPalindrome(int x) { //Your judge code } 二.概念 回文数(Palindrome)的定义:设n是一任意自然数.若将n的各位数字反向排列所得自然数n1与n相等,则称n为一回文数.例如,若n=1234321,则称n为一回文数:但若n=1234567,则n不是回文数. 特点: 1.负数.

Palindrome Number (回文数)

回文数是指这样的数字:正读和倒读都是一样的.如:595,2332都是回文数,234不是回文数. 注意:负数不是回文数 Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string

LeetCode:Palindrome Number - 回文数

1.题目名称 Palindrome Number(回文数) 2.题目地址 https://leetcode.com/problems/palindrome-number 3.题目内容 英文:Determine whether an integer is a palindrome. Do this without extra space. 中文:确认一个整数是否是回文数 4.解题方法1 将数字翻转后判断与原数字是否相等,可以参考LeetCode第7题(Reverse Integer)的解题思路.J

leetcode 9 Palindrome Number 回文数

Determine whether an integer is a palindrome. Do this without extra space. click to show spoilers. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using ext

LeetCode Problem 9:Palindrome Number回文数

描述:Determine whether an integer is a palindrome. Do this without extra space. Some hints: Could negative integers be palindromes? (ie, -1) If you are thinking of converting the integer to string, note the restriction of using extra space. You could a

判断一个整数是否为回文数 Check if a number is palindrome

一种方法是先翻转当前数,然后把它和原数比较(略) 另一种是递归方法,借用一个复制数,对原数递归,使之翻转,然后配合复制数比较 package recursion; public class Check_if_a_number_is_palindrome { public static void main(String[] args) { int num = 121321; System.out.println(check(num)); num = 12321; System.out.printl