dongqigu0429 2017-01-05 17:18
浏览 32
已采纳

检查阵列的一半是否是回文

for my school project, i need to write an algorithm which is gonna check if both halfs of array are palindromes, and if array has odd values, i should skip one in the middle. For example:

[1,2,1,6,4,2,5] should check if [1,2,1] is palindrome, and if [4,2,5] is palindrome, skipping the middle value cause its odd.

This is what i've wrote so far:

$somearray = ['1','1','1','1','1','2','1','1','2','1','1'];

$array_size = count($tablica);

function check_palindrome($arr, $start, $end)
{
    for($i=$start; $i < $end; $i++){
        if($arr[$i] != $arr[$start+$end-$i])
        {
            return false;
        }
    }
    return true;
}

if($array_size%2 == 0){
    $start1 = 0;
    $start2 = $array_size/2;
    $end1 = $array_size/2 - 1;
    $end2 = $array_size - 1;
} else {
    $start1 = 0;
    $start2 = round($array_size/2);
    $end1 = round($array_size/2, 0, PHP_ROUND_HALF_DOWN)-1;
    $end2 = $array_size -1;
}

$foo1 = check_palindrome($somearray, $start1, $end1);
$foo2 = check_palindrome($somearray, $start2, $end2);

if($foo1 && $foo2){
    echo 'Halfs are palindromes';
} else {
    echo 'Halfs are not palindromes';
}

And it works, but first most important part - my algorithm checks if first half is palindrome, then calling function twice i check second half - my teacher doesn't like that and tells me i should write it in one function, but i really don't have an idea how.

My second problem is how to improve my for loop, because for now it check half like this:

A=[1,2,4,2,1]

1=1, 2=2, 2=2, 1=1 if its true, but it should stop after checking 1=1 and 2=2

it's happening because of this statement in for loop: $i < $end but also don't know how to improve it :/

I really need your help. Thanks.

  • 写回答

1条回答 默认 最新

  • douyue6520 2017-01-05 18:03
    关注

    You can make your check_palindrome function more efficient by cutting the number of comparisons in half. For example, as it's currently written it would do the following for a 3-character string.

    check index 0 against index 3
    check index 1 against index 1
    check index 3 against index 0
    

    In an odd-length string, the middle character is by definition equal to itself, so there's no need to check it. And by the time you've reached the middle of the string on the left side, you've checked against all the characters on the right side.

    A more efficient function would be:

    function check_palindrome($arr, $start, $end)
    {
        $length = ($end - $start + 1)/2;
        for($i=0; $i < length; $i++){
            if($arr[$start+$i] != $arr[$end-$i])
            {
                return false;
            }
        }
        return true;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)