PHP (3) "Judging palindrome number" algorithm problem

Source: Internet
Author: User

The original title: Determine whether an integer is a palindrome number. A palindrome number is an integer that reads both the order (left-to-right) and the reverse (right-to-left).



classTest { Public functionA3 ($number)        {            $num=$number;
The input of a string, Str_split split into an array, to determine whether the first of the arrays is "-", if it is negative, it is not a palindrome number, but also can be added to a 10 can be divisible by the judgment$num _arr=Str_split($num); if($num _arr[0] = = '-') { return0; } Else {
Defines the back_num as a zero-order, same-split array$back _num=Strrev($number); $back _arr=Str_split($back _num); $length=Count($back _arr);
Determine if $num_arr is the same as the $back_arr in the array for($i= 0;$i<$length;$i++) { if($num _arr[$i] !=$back _arr[$i]) { return0; } } Echo $number; return $number; } } }

If it is a palindrome number, the output, no, returns false.

This I feel is the easiest way to understand, but also the most simple and rude, the above code can also use Array_map this function, but I feel the whole idea of the same.

Of course, there is another way of thinking, palindrome number this thing, it is a string of characteristics of the number, if an integer is a palindrome number, then its first half and the second half is the same, so we can actually traverse the input integer length of half can be, the specific look at the code

classTest { Public functionA5 ($number) {
The ritual. Just like the one above.$num=$number; $num _arr=Str_split($num); if($num _arr[0] = = '-') { return0; } Else { $length=Count($num _arr);
Defines $mid as half the length of the current array, if the length is odd, rounding down$mid= Floor($length/2); for($i= 0;$i<$mid;$i++) {
If Num_arr, the first and length-1-i are the same, is a palindrome number, output palindrome numberif($num _arr[$i] !=$num _arr[$length-$i]) return false; } Echo $num; return $num; } } }

PHP (3) "Judging palindrome number" algorithm problem

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

11.11 Big Sale for Cloud

Get Unbeatable Offers with up to 90% Off,Oct.24-Nov.13 (UTC+8)

Get It Now >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.