Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc47bafa

Browse files
committed
添加 0001.两数之和.md php版本
1 parent0f38eb9 commitc47bafa

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

‎problems/0001.两数之和.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,23 @@ var twoSum = function (nums, target) {
187187
};
188188
```
189189

190-
190+
php
191+
192+
```php
193+
function twoSum(array $nums, int $target): array
194+
{
195+
for ($i = 0; $i < count($nums);$i++) {
196+
// 计算剩下的数
197+
$residue = $target - $nums[$i];
198+
// 匹配的index,有则返回index, 无则返回false
199+
$match_index = array_search($residue, $nums);
200+
if ($match_index !== false && $match_index != $i) {
201+
return array($i, $match_index);
202+
}
203+
}
204+
return [];
205+
}
206+
```
191207

192208

193209
-----------------------

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp