# 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

### 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 arrays`reverse`Method, and then convert it into numbers to judge whether the original numbers and the new numbers are equal, that is`Answer one`
2. We can analyze it`Palindrome number`The characteristics can be found that he is a`Number of symmetric structures`Therefore, traverse from both ends to the middle. If the two characters traversed are not equal, it indicates that they are not`Palindrome number`, return directly`false`, otherwise, traverse until the intersection traversed from both ends. So there is`Answer two`
3. `however`Finally, there is another question:`"Can you solve this problem without converting integers to strings?"`
4. The method of not converting to string`Didn't do it!`, after reading the answers of the great gods, make a simple analysis`Answer three`
5. The main idea is`Turn 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 a`s`Variable to store the input`x`, declare a`rev`Variable to store the single digits of the input number.
7. When the single digit of the input value is moved to the variable`rev`After, the number of digits of the input value is one less, so there is a new value`s`The value becomes`s / 10``Math.floor(s / 10)`It is rounded for easy calculation.
8. `rev`The value of is incremented by one bit each cycle, so the new`rev`Become`The Rev in the previous step is multiplied by 10`Then add the in this step`Single digit of S`
9. until`s`When it becomes 0, the cycle stops and`Compare 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. obtain`Answer three`

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

``````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
};``````

``````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 questions`JavaScript`Analyze 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. ## Explain singleton methods and singleton classes in Ruby

singleton method Ruby allows you to add methods to a single object. This method that works only for a single object is called a singleton method Sample code ? 1 2 3 4 5 6 7 8 9 10 11 12 str = “just a regular string”   def str.title?   self.upcase == self end   […]