数字
function gcd(int $x, int $y): int
{while($y^=$x^=$y^=$x%=$y);return $x;
}
位运算:异或:gcd(a,b) = gcd(b,a mod b)
字符串
<?phpclass Solution {/*** @param String $str1* @param String $str2* @return String*/function gcdOfStrings($str1, $str2) {if (empty($str1) || empty($str2)) return '';if ($str1.$str2 !== $str2.$str1) {return '';}$xLen = $this->gcd(strlen($str1), strlen($str2));return substr($str1, 0, $xLen);}private function gcd($x, $y){if ($y)return $this->gcd($y, $x%$y);elsereturn $x;}
}
求字符串的最小公约串