$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)));
}
Pass two array strings. Each selects one string element. Make the absolute value of its length the largest. find the maximum absolute value
Guess you like
Origin http://43.154.161.224:23101/article/api/json?id=325913496&siteId=291194637
Recommended
Ranking