I use js to brush leetcode | day 6 | palindrome number

Time:2021-9-25

I use js to brush leetcode | day 6 | palindrome number

Number of palindromes:

Note: the complexity problem is not considered at this stage

Starting address: http://www.brandhuang.com/article/1583842055249

Question:

Determine whether an integer is a palindrome. An integer is a palindrome when it reads the same backward as forward.

Coud you solve it without converting the integer to a string?

Chinese Title:

Determine whether an integer is a palindrome number. Palindromes are integers that are read in the same positive order (from left to right) and reverse order (from right to left).

be careful:

Can you solve this problem without converting integers to strings?

Example:


Input: 121
Output: true

Input: -121
Output: false

Input: 10
Output: false

Personal analysis:

  1. See the title"Palindrome number", our first reaction is likely to be to convert numbers into strings, then into arrays, and finally use the of arraysreverseMethod, and then convert it into numbers to judge whether the original numbers and the new numbers are equal, that isAnswer one
  2. We can analyze itPalindrome numberThe characteristics can be found that he is aNumber of symmetric structuresTherefore, traverse from both ends to the middle. If the two characters traversed are not equal, it indicates that they are notPalindrome number, return directlyfalse, otherwise, traverse until the intersection traversed from both ends. So there isAnswer two
  3. howeverFinally, there is another question:"Can you solve this problem without converting integers to strings?"
  4. The method of not converting to stringDidn't do it!, after reading the answers of the great gods, make a simple analysisAnswer three
  5. The main idea isTurn the number over and compare it with the input value, if equal, it is the palindrome number, otherwise it is not the palindrome number.
  6. Declare asVariable to store the inputx, declare arevVariable to store the single digits of the input number.
  7. When the single digit of the input value is moved to the variablerevAfter, the number of digits of the input value is one less, so there is a new valuesThe value becomess / 10Math.floor(s / 10)It is rounded for easy calculation.
  8. revThe value of is incremented by one bit each cycle, so the newrevBecomeThe Rev in the previous step is multiplied by 10Then add the in this stepSingle digit of S
  9. untilsWhen it becomes 0, the cycle stops andCompare the original input value with the value in reverse order. If it is equal, it is the palindrome number, otherwise it is not the palindrome number
  10. obtainAnswer three

Answer:

1、 Answer one

var isPalindrome = function(x) {
    return x === Number(x.toString().split('').reverse().join(''))
};

2、 Answer two

var isPalindrome = function(x) {
    let xStr = x.toString()
    let i = 0; //  head
    Let J = xstr.length - 1 // tail
    while(i < j){
         if(xStr[i] !== xStr[j]) {
             return false;
         } else{
            i ++;
            j --;
        }
    }
    return true
};

3、 Answer three

var isPalindrome = function(x) {
    let s = x;
    let rev = 0
    while (s > 0) {
        rev = rev * 10 + s % 10
        s = Math.floor(s / 10)
    }
    return x === rev
};

other:

More questionsJavaScriptAnalyze and click the link to access the corresponding answer: https://leetcode.com

If you are interested in scanning code or searching “Chongqing cubs after 90” on wechat, let’s learn the front end together.

I use js to brush leetcode | day 6 | palindrome number