1. palindrome

Given an integer x, return true if x is a palindrome integer; Otherwise, return false. Palindromes are integers that read in positive (left to right) and backward (right to left) order. For example, 121 is palindrome and 123 is not.

Input: x = 121 Output: true Input: x = -121 Output: false Description: The value is -121 read from left to right. Read from right to left, 121-. So it's not a palindrome number.Copy the code

Advanced: Can you solve this problem by not converting integers to strings?

Source: LeetCode link: leetcode-cn.com/problems/pa… Copyright belongs to the Collar buckle network. Commercial reprint please contact official authorization, non-commercial reprint please indicate the source.

The most basic algorithm: convert a number to a string -> reverse -> compare with the original character, true if the same, false if different. One of the most error-prone points is how to convert integers to characters, i.e., int to string. The simple explicit conversion of string(x) does not convert X to a string, but to a corresponding character as if it were an ASCII encoding. The solution code is as follows:

import "strconv"

func isPalindrome(x int) bool {
	// Error: this method will assume that x is utF-8 encoding, resulting in unintelligible characters.
	// xStr := string(x)
    // This method should be used to convert an integer to a string
	xStr := strconv.Itoa(x)
	xBytes := []byte(xStr)
	length := len(xStr)
	reverseBytes := make([]byte, length)
	for index, _ := range xBytes {
		reverseBytes[length - index - 1] = xBytes[index]
	}
	for index, v := range xBytes {
		ifv ! = reverseBytes[index] {return false}}return true
}
Copy the code

The second mathematical method is to reverse the number and compare it with the original number. This method may cause the numbers to overflow, so it can be morphed to compare the status and high order of the numbers in order, returning true if they are all the same, and false otherwise. The implementation logic is similar to the third.

The third, namely, the algorithm given by the official force button, takes out the tail half of the numbers, reverses them, and compares them with the remaining numbers, namely

For example, 1221 becomes 12 when the tail 21 is cut off. The original number becomes 12 when the tail is removed. 12=12, it is a palindrome number.

Another example, 12321, cut tail 321, reverse to 123, remove the tail of the original number becomes 12,123! = 12, but 123/10 = 12, so the original number is also palindrome.

func isPalindrome(x int) bool {
	/ / negative
	if x < 0 {
		return false
	}

	// The units digit is a palindrome
	if x/10= =0 {
		return true
	}

	// The units digit is 0. The highest digit cannot be 0, so it must not be a palindrome
	if x%10= =0 {
		return false
	}

	var tailReverse int = 0
	for ; x > tailReverse ; x = x/10 {
		// The next lowest digit
		nextNum := x%10
		tailReverse = tailReverse*10 + nextNum
	}

	if x == tailReverse || x == tailReverse/10 {
		return true
	} else {
		return false}}Copy the code

Go language power buckle brush series of articles | Go theme month

  1. Go language power buckle brush – the sum of two numbers | Go theme month
  2. Go language power buckle brush – add two numbers | Go theme months
  3. Go language power button brush title – the largest string without repeating characters | Go theme month
  4. Go language power button brush questions – find the median of two positive ordinal groups | Go theme months
  5. Go language power button brush – the longest echo sub string | Go theme month
  6. Go language power buckle brush -Z font transformation | Go theme month
  7. Go language force button brush title – integer reversal | Go theme month
  8. Go language power button brush – string conversion integer | Go theme month