这似乎微不足道,但我感到困惑的是,我还没有能够找到解决方案。我想做的是:
Input -> 14025
Output -> 10245
Input -> 171
Output -> 117
等等......
答案 0 :(得分:5)
$input = 140205;
$temp = str_split($input);
sort($temp);
// find the 1st non-zero digit
$f = current(array_filter($temp));
// remove it from the array
unset($temp[array_search($f, $temp)]);
echo $output = $f . join($temp); // 100245
答案 1 :(得分:0)
有点冗长,但与之前的回答类似
function smallestNumberFromDigits($string) {
//split string to digits
$array = str_split($string);
//sort the digits
sort($array);
//find the first digit larger than 0 and place it to the begining of array
foreach($array as $i => $digit) {
if($digit > 0) {
$tmp = $array[0];
$array[0] = $digit;
$array[$i] = $tmp;
break;
}
}
//return the imploded string back
return implode("", $array);
}