In C++, use to_string(A) function to easily convert int to string.:)


#1

In C++, use to_string(A) function to easily convert int to string.:slight_smile:


#2

The problem statement says that you can’t use extra space. So, if you’re converting the number to a string, and then checking if this string is a palindrome, then your answer is probably right (and it’s easier to do this way), but you are not doing what was asked.


#3

@felipe-guimaraes what is the problem if we convert it to string? while reversing the integer we are taking an extra space “rev” which will take the same amout of space as A. So why is it wrong to take a string variable ? Can you explain?


#4

@ravi-keshri: When you take another string it takes up as much space as the number of digits in the integer. A string is basically an array of character bytes, we need to avoid creating arrays whenever asked to solve problems in constant space because the size of the array can vary according to the input.

On the other hand, creating another integer, which you say is “rev”, will take only as much space as needed by int and your solution would fall under constant space.


#5

Got it. Thanks for your response!!