-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path14.最长公共前缀.php
95 lines (84 loc) · 1.77 KB
/
14.最长公共前缀.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php
/*
* @lc app=leetcode.cn id=14 lang=php
*
* [14] 最长公共前缀
*
* https://leetcode-cn.com/problems/longest-common-prefix/description/
*
* algorithms
* Easy (39.43%)
* Likes: 1582
* Dislikes: 0
* Total Accepted: 507.3K
* Total Submissions: 1.3M
* Testcase Example: '["flower","flow","flight"]'
*
* 编写一个函数来查找字符串数组中的最长公共前缀。
*
* 如果不存在公共前缀,返回空字符串 ""。
*
*
*
* 示例 1:
*
*
* 输入:strs = ["flower","flow","flight"]
* 输出:"fl"
*
*
* 示例 2:
*
*
* 输入:strs = ["dog","racecar","car"]
* 输出:""
* 解释:输入不存在公共前缀。
*
*
*
* 提示:
*
*
* 0
* 0
* strs[i] 仅由小写英文字母组成
*
*
*/
// @lc code=start
class Solution
{
/**
* @param String[] $strs
* @return String
*/
public function longestCommonPrefix($strs)
{
$count = count($strs);
if ($count === 1) {
return join($strs);
}
$minLength = -1;
$commonStr = [];
foreach ($strs as $str) {
if ($minLength < 0) {
$minLength = strlen($str);
continue;
}
$minLength = strlen($str) < $minLength ? strlen($str) : $minLength;
}
for ($i = 0; $i < $minLength; $i ++) {
for ($j = 0; $j < $count - 1; $j ++) {
// 第 i 位字符
if (substr($strs[$j], $i, 1) !== substr($strs[$j + 1], $i, 1)) {
return join($commonStr);
}
if ($j === $count - 2) {
$commonStr[] = substr($strs[$j], $i, 1);
}
}
}
return join($commonStr);
}
}
// @lc code=end