Skip to content

Commit 5b42e9e

Browse files
committed
2019年10月11日
1 parent e62fe6a commit 5b42e9e

File tree

3 files changed

+67
-2
lines changed

3 files changed

+67
-2
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
本库为记录自己日常学习力扣算法的仓库,如有侵权部分,请联系本人删除
2+
3+
QQ:1161645687

algorithms/500/500-keyboard-row.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
/*
3+
给定一个单词列表,只返回可以使用在键盘同一行的字母打印出来的单词。键盘如下图所示。
4+
5+
示例:
6+
输入: ["Hello", "Alaska", "Dad", "Peace"]
7+
输出: ["Alaska", "Dad"] 
8+
9+
注意:
10+
你可以重复使用键盘上同一字符。
11+
你可以假设输入的字符串将只包含字母。
12+
*/
13+
14+
class Solution
15+
{
16+
17+
/**
18+
* @param String[] $words
19+
*
20+
* @return String[]
21+
*/
22+
public function findWords($words)
23+
{
24+
$map = ['a' => 2, 'b' => 3, 'c' => 3, 'd' => 2, 'e' => 1, 'f' => 2, 'g' => 2, 'h' => 2, 'i' => 1, 'j' => 2, 'k' => 2, 'l' => 2, 'm' => 3, 'n' => 3, 'o' => 1, 'p' => 1, 'q' => 1, 'r' => 1, 's' => 2, 't' => 1, 'u' => 1, 'v' => 3, 'w' => 1, 'x' => 3, 'y' => 1, 'z' => 3];
25+
26+
$newWords = [];
27+
foreach ($words as $word) {
28+
$len = strlen($word);
29+
$temp_word = strtolower($word);
30+
31+
// 第一个的map值
32+
$line = $map[$temp_word[0]];
33+
34+
$is_append = true;
35+
for ($i = 0; $i < $len; $i++) {
36+
$temp = $map[$temp_word[$i]];
37+
if ($temp !== $line) {
38+
$is_append = false;
39+
break;
40+
}
41+
}
42+
43+
if ($is_append) {
44+
$newWords[] = $word;
45+
}
46+
}
47+
return $newWords;
48+
}
49+
}
50+
51+
$solution = new Solution();
52+
var_dump($solution->findWords(["Hello", "Alaska", "Dad", "Peace"]));

algorithms/905/905-sort-array-by-parity.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,19 @@ class Solution
2525
*/
2626
public function sortArrayByParity($A)
2727
{
28-
28+
$len = count($A);
29+
$j = 0;
30+
for ($i = 0; $i < $len; $i++) {
31+
if (($A[$i] % 2 === 0)) {
32+
$temp = $A[$j];
33+
$A[$j] = $A[$i];
34+
$A[$i] = $temp;
35+
$j++;
36+
}
37+
}
38+
return $A;
2939
}
3040
}
3141

3242
$solution = new Solution();
33-
var_dump($solution->sortArrayByParity([3, 1, 2, 4]));
43+
var_dump($solution->sortArrayByParity([3, 2, 1, 4]));

0 commit comments

Comments
 (0)