LeetCode09:Palindrome Number

来源:互联网 发布:图像 meanshift算法 编辑:程序博客网 时间:2024/06/05 09:55
原题目:
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 extra space.
You could also try reversing an integer. However, if you have solved the problem "Reverse Integer", you know that the reversed integer might overflow. How would you handle such case?
There is a more generic way of solving this problem.
判断一个整数是否是回文:
需要考虑:
1.判断负数是否是回文?(负数不是回文)
2.本题限制不能使用额外的空间,所以不能通过将整数转化为字符串来进行判断
3.可以使用翻转整数的方法,但是要确定翻转后的整数是否会超出int的范围

算法分析:

先统计出整数的位数,通过取余和相除来取出整数的第一位和最后一位,通过判断第一位和最后一位是否相等来实现判断是否是回文。
例如,对于整数23432,首先统计位数Index= 5,然后定义两个指针i和j分别指向整数的第一位和最后一位,通过取余,j=2,通过i=23432/10000来取出第一位,依次这样判断,直到整个整数都判断完成。


LeetCode提交源码:
算法1:时间复杂度O(1),空间复杂度O(1),运行时间较小
  1. public boolean isPalindrome(int x){
  2. if(x == 0){
  3. return true;
  4. }
  5. long longnum = x;
  6. if(longnum > Integer.MAX_VALUE || longnum < Integer.MIN_VALUE){
  7. return false;
  8. }
  9. boolean flag = true;
  10. if(x < 0){
  11. //x = x*(-1);
  12. return false;
  13. }
  14. else{
  15. int index = 0;
  16. int indextmp = x;
  17. while(indextmp != 0){
  18. index++;
  19. indextmp = indextmp/10;
  20. }
  21. System.out.println("index= " + index);
  22. int i = 0,j = 0;
  23. while( x != 0){
  24. j = x%10;
  25. i = (int)(x/(Math.pow(10, (index-1))));
  26. System.out.println("i=" + i + ",j=" + j + ",x=" + x);
  27. if(i != j){
  28. flag = false;
  29. break;
  30. }
  31. else{
  32. x = (int)((x - i*Math.pow(10, (index-1))-j)/10);
  33. index = index - 2;
  34. System.out.println("index=" + index + ",x="+x);
  35. if(index == -1){
  36. break;
  37. }
  38. }
  39. }
  40. }
  41. return flag;
  42. }
 

算法2:时间复杂度O(1),空间复杂度O(1)

  1. public boolean isPalindrome(int x) {
  2. if (x < 0)
  3. return false;
  4. int d = 1; // divisor
  5. while (x / d >= 10)
  6. d *= 10;//d的位数与x的位数相同
  7. while (x > 0) {
  8. int q = x / d; // 取第一位
  9. int r = x % 10; // 取最后一位
  10. if (q != r) //判断是否相等
  11. return false;//不相等直接返回退出
  12. x = x % d / 10;//x与d取余,去除第一位,和10相除,去除最后一位,巧妙!
  13. d /= 100;
  14. }
  15. return true;
  16. }

 

完整运行程序:

  1. /**************************************************************
  2. * Copyright (c) 2016
  3. * All rights reserved.
  4. * 版 本 号:v1.0
  5. * 题目描述: Palindrome Number
  6. * Determine whether an integer is a palindrome. Do this without extra space.
  7. *判断一个整数是否是回文:
  8. * 输入描述:请输入一个整数:
  9. *12344321
  10. * 程序输出: 算法1:输入的整数是否是回文:true
  11. *算法2:输入的整数是否是回文:true
  12. * 问题分析:1.判断负数是否是回文?(负数不是回文)
  13. *2.本题限制不能使用额外的空间,所以不能通过将整数转化为字符串来进行判断
  14. *3.可以使用翻转整数的方法,但是要确定翻转后的整数是否会超出int的范围
  15. * 算法描述:先统计出整数的位数,通过取余和相除来取出整数的第一位和最后一位,通过判断第一位和最后一位是否相等来实现判断是否是回文。
  16. *例如,对于整数23432,首先统计位数Index= 5,然后定义两个指针i和j分别指向整数的第一位和最后一位,
  17. *通过取余,j=2,通过i=23432/10000来取出第一位,依次这样判断,直到整个整数都判断完成。
  18. * 完成时间:2016-11-17
  19. ***************************************************************/
  20. package org.GuoGuoFighting.LeetCode01;
  21. import java.util.Scanner;
  22. //-2147483648
  23. class SolutionMethod1{
  24. public boolean isPalindrome(int x){
  25. if(x == 0){
  26. return true;
  27. }
  28. long longnum = x;
  29. if(longnum > Integer.MAX_VALUE || longnum < Integer.MIN_VALUE){
  30. return false;
  31. }
  32. boolean flag = true;
  33. if(x < 0){
  34. //x = x*(-1);
  35. return false;
  36. }
  37. else{
  38. int index = 0;
  39. int indextmp = x;
  40. while(indextmp != 0){
  41. index++;
  42. indextmp = indextmp/10;
  43. }
  44. int i = 0,j = 0;
  45. while( x != 0){
  46. j = x%10;
  47. i = (int)(x/(Math.pow(10, (index-1))));
  48. if(i != j){
  49. flag = false;
  50. break;
  51. }
  52. else{
  53. x = (int)((x - i*Math.pow(10, (index-1))-j)/10);
  54. index = index - 2;
  55. if(index == -1){
  56. break;
  57. }
  58. }
  59. }
  60. }
  61. return flag;
  62. }
  63. }
  64. //时间复杂度O(1),空间复杂度O(1)
  65. class SolutionMethod2{
  66. public boolean isPalindrome(int x) {
  67. if (x < 0)
  68. return false;
  69. int d = 1; // divisor
  70. while (x / d >= 10)
  71. d *= 10;//d的位数与x的位数相同
  72. while (x > 0) {
  73. int q = x / d; // 取第一位
  74. int r = x % 10; // 取最后一位
  75. if (q != r) //判断是否相等
  76. return false;//不相等直接返回退出
  77. x = x % d / 10;//x与d取余,去除第一位,和10相除,去除最后一位,巧妙!
  78. d /= 100;
  79. }
  80. return true;
  81. }
  82. }
  83. public class PalindromeNumber {
  84. public static void main(String[] args){
  85. System.out.println("请输入一个整数:");
  86. Scanner scanner = new Scanner(System.in);
  87. int num = scanner.nextInt();
  88. scanner.close();
  89. SolutionMethod1 solution1 = new SolutionMethod1();
  90. System.out.println("算法1:输入的整数是否是回文:" + solution1.isPalindrome(num));
  91. SolutionMethod2 solution2 = new SolutionMethod2();
  92. System.out.println("算法2:输入的整数是否是回文:" + solution2.isPalindrome(num));
  93. }
  94. }


序运行结果:
 
0 0