Learning Go by Solving Leetcode Problems — 9. Palindrome Number

Jonathan Duran
4 min readJul 7, 2022

This article is a continuation of my efforts to learn Go by solving easy Leetcode problems. After working through my last article, I am now pretty comfortable with the basics of Go.

Let’s jump right into the next problem — 9. Palindrome Number

Similar to our approach to the Two Sums problem, we’ll make a plan, break it into small steps, and translate those steps into Go code.

🚨 Reminder** We shouldn’t care too much about the “optimal” solution. Consider this exercise a success if our approach leads to a large amount of Go research.

Plan

  • Find a way to iterate over the digits of the input x .
  • Create two counters and point them at each end of x .
  • During each iteration, compare (for equality) the numbers referenced by the counters.
  • If the numbers match, If x is a palindrome, the iteration should continue until the counters meet in the middle of x , If not, then x is not a palindrome.

How to iterate over x ?

Let’s get the obvious out of the way — we can’t take an integer type and iterate over its digits. Instead, we could convert x into a string that we can iterate over. However, there is a problem. Go stores the characters in a string as their byte representation. This means that we have to become familiar with concepts like ASCII, UTF-8, and the Go type — runes .

Let’s think about the range of possible values for x and what that means for its encoding. Leetcode gives us the constraint -231 <= x <= 231 - 1 . Any digit (including the negative sign) is a valid ASCII character. Lucky for us ASCII characters are one byte. When indexing a string (someString[index]) Go returns the decimal code for that character's ASCII symbol. We can use the fmt.Printf("%c", someString[index] command to return the character of the string.

This is pretty much what we need for this step. However, this approach would break had we had to handle non-ASCII characters. In this case, runes would have been more appropriate.

Converting x from an integer to a string.

To make this conversion, we use another package from Go’s standard library called strconv . This package has a convenient method, Itoa() , that converts an integer to an ASCII string.

main.go

Looping over x .

We’ve already seen how to use a for loop in Go in the last article so I’ll skip most of the details. But, one thing to note is that the length of a string (len(someAsciiString)) is the total number of bytes in that string. Since each character in an ASCII string is equal to one byte, the total number of bytes is equal to the number of letters in that string. What I’m trying to get across is that you should feel safe interpreting len(someAsciiString) as the number of characters in that string.

Comparing the characters in x .

First, let's take care of an edge case. If x is a single digit, then it must be a palindrome. We should create an if block to handle this and immediately return true .

Next, instead of using a single indexing variable i in our for loop, we introduce a second one j that will track the tail end of x . We don’t need i, j to loop over the entire length of xString , instead, we can split xString in half and let i iterate it from start to middle while j iterates it from end to middle.

At each iteration, we use i, j to grab the character at those indices and make our comparison.

🚨 Remember, someAsciiString[index] returns a Unicode codepoint not a character. To resolve this we can use the string() method to return the ASCII character from the Unicode codepoint (string(someAsciiString[index] ).

Leetcode Submission

The biggest takeaway for me was learning about how Go handles strings and their characters. I’ll need to go deeper into runes and play around with non-ASCII strings.

Hopefully, this exercise was useful in learning a bit more about Go. As always, give me a follow on Twitter.

Cheers! 👋🏼

--

--