通过两个数组字符串。各选取一个字符串元素。使其长度绝对值最大。求最大绝对值

 $s1 = ["hoqq", "bbllkw", "oox", "ejjuyyy", "plmiis", "xxxzgpsssa", "xxwwkktt", "znnnnfqknaz", "qqquuhii", "dvvvwz"];
        $s2 = ["cccooommaaqqoxii", "gggqaffhhh", "tttoowwwmmww"];
        $this->revTest(mxdiflg($s1, $s2), 13);
        $s1 = ["ejjjjmmtthh", "zxxuueeg", "aanlljrrrxx", "dqqqaaabbb", "oocccffuucccjjjkkkjyyyeehh"];
        $s2 = ["bbbaaayddqbbrrrv"];
        $this->revTest(mxdiflg($s1, $s2), 10);
        $s1 = ["ccct", "tkkeeeyy", "ggiikffsszzoo", "nnngssddu", "rrllccqqqqwuuurdd", "kkbbddaakkk"];
        $s2 = ["tttxxxxxxgiiyyy", "ooorcvvj", "yzzzhhhfffaaavvvpp", "jjvvvqqllgaaannn", "tttooo", "qmmzzbhhbb"];
        $this->revTest(mxdiflg($s1, $s2), 14) ;
我的写法:是通过冒泡将$a1按字符串倒序排。$a2正序排。
function mxdiflg($a1, $a2) {
    for ($i = 0; $i < count($a1); $i++) {
        for ($a = $i + 1; $a < count($a1); $a++) {
            if (strlen($a1[$i]) < strlen($a1[$a])) {
                $store = $a1[$i];
                $a1[$i] = $a1[$a];
                $a1[$a] = $store;
            }
        }
    }
    for ($i = 0; $i < count($a2); $i++) {
        for ($a = $i + 1; $a < count($a2); $a++) {
            if (strlen($a2[$i]) > strlen($a2[$a])) {
                $store = $a2[$i];
                $a2[$i] = $a2[$a];
                $a2[$a] = $store;
            }
        }
    }
    return max(abs(strlen($a1[0])-strlen($a2[0])),abs(strlen($a1[count($a1) - 1])-strlen($a2[count($a2)-1])));
}
感觉比较好的写法:
function mxdiflg($a1, $a2) {
    if (empty($a1) || empty($a2)) { return -1; }
    $a2 = array_map('strlen', $a2);
    $a1 = array_map('strlen', $a1);
    
    return max(abs(min($a2) - max($a1)), abs(max($a2)-min($a1)));
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值