xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

LeetCode 9. Palindrome Number All In One

LeetCode 9. Palindrome Number All In One

LeetCode 回文数字 bug

shit 💩 bug

"use strict";

/**
 *
 * @author xgqfrms
 * @license MIT
 * @copyright xgqfrms
 * @created 2020-07-23
 * @modified
 *
 * @description 9. Palindrome Number
 * @difficulty Easy
 * @complexity O(n)
 * @augments
 * @example
 * @link https://leetcode.com/problems/palindrome-number/
 * @link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/log10
 * @solutions
 *
 */

const log = console.log;


/**
 * @param {number} x
 * @return {boolean}
 */
var isPalindrome = function(num) {  
  if(num < 0) {
    return false;
  } else {
    const len = Math.ceil(Math.log10(num + 1));
    if (len <= 1) {
      return true;
    } else {
      const temp = DigitsArrayToNumber(NumberToDigitsArray(num));
      if(num === temp) {
        return true;
      } else {
        return false;
      }
    }
  }
};

function DigitsArrayToNumber(arr)  {
  let result = 0;
  let len = arr.length;
  if(len > 2) {
    while (len > 0) {
      result += arr[len - 1] * Math.pow(10, len - 1);
      len--;
    }
  } else {
    result = arr[0]*10 +arr[1];
  }
  return result;
}

function NumberToDigitsArray(num)  {
  const result = [];
  while (num > 0) {
    result.push(num % 10);
    num = parseInt(num / 10);
  }
  return result;
}




const test = isPalindrome(123);
const test1 = isPalindrome(-123);
const test2 = isPalindrome(10);
const test3 = isPalindrome(11);

log(`test`, test)
log(`test1`, test1)
log(`test2`, test2)
log(`test3`, test3)


refs


const isPalindrome = (str) => {
  let rs = ``;
  let len = str.length;
  // 逆序字符串
  while(len--){
    rs += str[len];
  }
  return str === rs;
}

https://www.cnblogs.com/xgqfrms/p/12960447.html

https://leetcode.com/problems/palindrome-number/

https://repl.it/@xgqfrms/leetcode-9-Palindrome-Number

https://leetcode.com/problems/palindrome-number/discuss/752073/9.-Palindrome-Number



©xgqfrms 2012-2020

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2020-07-24 12:29  xgqfrms  阅读(153)  评论(1编辑  收藏  举报