zoukankan      html  css  js  c++  java
  • Leetcode 回文数字判断

    一、问题描述

    判断一个integer 型的数字是否是回文,空间复杂度应该是常数级别的 。

    二、问题分析

       首先,负数不是回文,10的整数倍不会是回文,个位数一定是回文。

    三、代码实现

           思路:将一个数字翻转,即最高位变成最低位,最低位变成最高位,然后比较输入的字符和翻转之后的字符。

     1 class Solution {
     2     bool isPalindrome(int x)
     3     {
     4         if( x < 0 || (x%10 == 0 && x != 0))
     5         {
     6             return false;
     7         }
     8         
     9         int i = x; // 将 x 保存起来
    10         int reveredNum = 0; // 保存翻转之后的数字 
    11         while (x != 0)
    12         {
    13             reveredNum = reveredNum* 10 + x % 10;1
    14              x /= 10; 
    15         }
    16         
    17         return i == reveredNum ;    
    18     } 
    19 } 
    pp
  • 相关阅读:
    多行文字垂直居中效果(利用flex)
    Switch
    Scanner
    Method
    Recursion递归
    for
    if
    dowhile
    while
    DataType 数据类型
  • 原文地址:https://www.cnblogs.com/wangxiaoyong/p/8647189.html
Copyright © 2011-2022 走看看