Skip to content

Commit 76271d6

Browse files
authored
Merge pull request #19 from openset/master
Add: OnlyNumbersV2
2 parents 32def54 + b2812a0 commit 76271d6

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

package/Other/OnlyNumbers.php

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,41 @@
1414
* @param array $container
1515
* @return mixed
1616
*/
17-
function OnlyNumbers(array $container){
17+
function OnlyNumbers(array $container)
18+
{
1819
$count = count($container);
19-
if( $count <= 1 ) {
20+
if ($count <= 1) {
2021
return $container;
2122
}
2223
$exist = [];
2324
for ($i = 0; $i < $count; $i++) {
24-
if(isset($exist[$container[$i]])){
25+
if (isset($exist[$container[$i]])) {
2526
unset($exist[$container[$i]]);
2627
continue;
2728
}
2829
$exist[$container[$i]] = true;
2930
}
3031
return !empty($exist) ? array_keys($exist)[0] : false;
3132
}
32-
var_dump(OnlyNumbers([11,22,22,11,5,63,13,5,63,18,89,13,89]));
33+
34+
var_dump(OnlyNumbers([11, 22, 22, 11, 5, 63, 13, 5, 63, 18, 89, 13, 89]));
35+
36+
37+
// +----------------------------------------------------------------------
38+
// | 方法二
39+
// +----------------------------------------------------------------------
40+
/**
41+
* @author Openset <[email protected]>
42+
* @link https://github.com/openset
43+
* @date 2017/9/7
44+
* @param array $container
45+
* @return null
46+
*/
47+
function OnlyNumbersV2(array $container)
48+
{
49+
$res = array_flip(array_count_values($container));
50+
51+
return isset($res[1]) ? $res[1] : null;
52+
}
53+
54+
var_dump(OnlyNumbersV2([11, 22, 22, 11, 5, 63, 13, 5, 63, 18, 89, 13, 89]));

package/Other/StealingApples.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@
5858
for ($r = 1, $n = 5; ; $r++) {
5959
$t = $r;
6060
for ($i = 0; $i <= $n; $i++) {
61-
if ($t % 5 == 1) {
62-
$t = $t - round($t / 5) - 1;
61+
if ($t % $n == 1) {
62+
$t = $t - round($t / $n) - 1;
6363
} else {
6464
continue 2;
6565
}

0 commit comments

Comments
 (0)