Skip to content

Commit 580a0a4

Browse files
committed
566-week 1
1 parent ae7223e commit 580a0a4

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

Week 01/id_566/leetcode_01_566.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
/**
3+
* 两数之和
4+
* Author:show
5+
*/
6+
// 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。
7+
8+
// 你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。
9+
class Solution {
10+
11+
/**
12+
* @param Integer[] $nums
13+
* @param Integer $target
14+
* @return Integer[]
15+
*/
16+
function twoSum($nums, $target) {
17+
$count = count($nums);
18+
for($i=0;$i<$count;$i++)
19+
{
20+
$ele = $target - $nums[$i];
21+
$keys = array_keys($nums,$ele);
22+
if($keys)
23+
{
24+
foreach($keys as $key)
25+
{
26+
if($key!=$i)
27+
{
28+
return [$i,$key];
29+
}
30+
}
31+
}
32+
}
33+
34+
}
35+
}
36+
?>

Week 01/id_566/leetcode_66_566.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
/**
3+
* 66.加一
4+
* Author:show
5+
*/
6+
// 给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一。
7+
8+
// 最高位数字存放在数组的首位, 数组中每个元素只存储单个数字。
9+
10+
// 你可以假设除了整数 0 之外,这个整数不会以零开头。
11+
12+
class Solution {
13+
14+
/**
15+
* @param Integer[] $digits
16+
* @return Integer[]
17+
*/
18+
function plusOne($digits)
19+
{
20+
$length = count($digits);
21+
// echo "length:".$length."\n";
22+
// $tmp += 1;
23+
// $tmp = bcadd($tmp,1);
24+
//leetcode不能使用bcadd
25+
if($length<20)
26+
{
27+
$tmp = implode("",$digits);
28+
$tmp += 1;
29+
$tmp = str_split($tmp,1);
30+
return $tmp;
31+
}else{
32+
for($i=$length-1;$i>=0;$i--)
33+
{
34+
$digits[$i] += 1;
35+
if($digits[$i] > 9)
36+
{
37+
$digits[$i] = 0;
38+
}else{
39+
return $digits;
40+
}
41+
}
42+
array_unshift($digits, 1);
43+
return $digits;
44+
}
45+
}
46+
}
47+
?>

0 commit comments

Comments
 (0)