Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to realize palindromes in java

Shulou Source: shulou.com Published: 2022-06-01 01:11:46 10月03日 Update

This article is about how java implements palindromes. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Topic description

Determines whether an integer is a palindrome. The number of palindromes means that the positive order (from left to right) and the reverse order (from right to left) are the same integers.

Example 1:

Input: 121Exporting: true

Example 2:

Input:-121output: false explanation: read from left to right, for-121c. Read from right to left, 121 -. So it's not a palindrome.

Example 3:

Input: 10 output: false explanation: read from right to left, which is 01. So it's not a palindrome. The idea of solving the problem

Tags: math

If it is a negative number, it must not be a palindrome number. Return false directly.

If it is a positive number, it is calculated in reverse order and compared with the original value.

If it is a palindrome number, equal returns true. If not, it is not equal false.

For example, the reverse order 321 of 123 is not equal, and the reverse order 121 of 121 is equal.

Code class Solution {public boolean isPalindrome (int x) {if (x < 0) return false; int cur = 0; int num = x; while (num! = 0) {cur = cur * 10 + num% 10; num / = 10;} return cur = = x;}}

Thank you for reading! This is the end of the article on "how to achieve palindromes in java". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Tags: Palindromes reverse order examples input output content values integers more articles explanations good practical code ideas mathematics articles schemes labels positive numbers Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Xiaomi MariaDB Huawei vpn