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