Skip to content

Commit f113073

Browse files
committed
week01
1 parent c027ff8 commit f113073

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

Week01/1-twoSum.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
class Solution {
3+
4+
/**
5+
* @param Integer[] $nums
6+
* @param Integer $target
7+
* @return Integer[]
8+
*/
9+
function twoSum($nums, $target) {
10+
# 1.创建一个新数组,判断$target-$nums[$i]是否在新数组中
11+
# 时间复杂度O(n),空间复杂度O(n):所需的额外空间取决于哈希表中存储的元素数量,该表中存储了 n 个元素。
12+
$arr = [];
13+
for ($i=0; $i<count($nums); $i++) {
14+
$temp = $target - $nums[$i];
15+
if (array_key_exists($temp, $arr)) {
16+
return [$arr[$temp], $i];
17+
}
18+
$arr[$nums[$i]] = $i;
19+
}
20+
return false;
21+
}
22+
}

0 commit comments

Comments
 (0)