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).



eg



Class test
    {
         Public function a3($number)
        {
            $num = $number;
/ / Enter a string, str_split is divided into an array, to determine whether the first array is "-", if it is a negative number, must not be the number of palindromes, you can also add a rule that can be divisible by 10
            $num_arr = str_split($num);
            If ($num_arr[0] == ‘-‘)
            {
                Return 0;
            }
            Else
            {
/ / Define back_num as $number reverse order, the same split into an array
                $back_num = strrev($number);
                $back_arr = str_split($back_num);
                $length = count($back_arr);
/ / Determine whether $num_arr is the same as the i-th in the array of $back_arr
                For ($i=0; $i < $length; $i++)
                {
                    If ($num_arr[$i] != $back_arr[$i])
                    {
                        Return 0;
                    }
                }
                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


 
Class test
     {
         Public function a5($number)
         {
//Old rules. . Same as above
             $num = $number;
             $num_arr = str_split($num);
             If($num_arr[0] == ‘-‘)
             {
                 Return 0;
             }
             Else
             {
                 $length = count($num_arr);
/ / Define $mid to half the length of the current array, if the length is odd, round down
                 $mid = floor($length/2);
                 For ($i=0; $i < $mid; $i++)
                 {
/ / If num_arr, the i-th and the length-1-i are the same, it is the number of palindromes, the number of output palindromes
                     If ($num_arr[$i] != $num_arr[$length-1-$i])
                         Return false;
                 }
                 Echo $num;
                 Return $num;
             }
         }
     }





PHP (3) "Judging palindrome number" algorithm problem


Related Article

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: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.