Nuggets team number online, help you Offer impromptu! Click on theCheck the details

preface

The first time to participate in the Nuggets punch card activity, among other things, mainly rush to reward. 4.12 Start to rush to achieve the small goal of question 14!! This is problem number seven

Topic describes

Title link: Palindromes

For the title description, I mainly use the screenshot leetcode, so the title is as follows

Thought analysis

So the idea is to just go ahead and convert it to a string and then invert it and see if it’s equal, and then try it out

A happy mood, and then see the steps below write not to convert to a string to solve

So let’s think about how to determine if it’s a palindrome when it’s a number, because it’s an integer. 1: negative is definitely not a palindrome. 2:0 to 9 is definitely a palindrome. The ones digit 0 is not a palindrome.

/ / 0-9 directly returns true if (x > = 0 && x < 10) {return true} / / < 0 or bits of 0 directly returns false if (x < 0 | | 10 x % = = = 0)} {return falseCopy the code

The next step is to consider integers greater than 10 (and the ones place is not zero) and reverse them, so go straight ahead. Reverse operation,(math.ceil rounded up, math.floor () rounded off decimal, math.round rounded)

while(y! =0){ rvNum = y%10+rvNum*10 y=Math.floor(y/10) }Copy the code

I’m going to submit it and I’m going to pass it and I’m going to compare it to the way I just flipped the string and it takes a lot more memory and a lot more time

AC code

/** * @param {number} x * @return {boolean} */ // 1. Var isPalindrome = function(x) {let y=x return y.tostring ().split('').reverse().join('') === x.tostring ()}; Var / / 2. Integer method isPalindrome = function (x) {if (x > = 0 && x < 10) {return true} the if (x < 0 | | 10 x % = = = 0)} {return false let let y = x  rvNum=0 while(y! =0){ rvNum = y%10+rvNum*10 y=Math.floor(y/10) } return rvNum===x };Copy the code

conclusion

Do more, think more. Problem 7 algorithm complete, persistence is victory!!

Left left left

→ Algorithm series link ←

Write write write

You can order it here! You can order it here! You can order it here!