Skip to content

Commit 4ac5dae

Browse files
authored
Merge branch 'master' into master
2 parents 659b34c + e856802 commit 4ac5dae

15 files changed

+548
-108
lines changed

problems/0001.两数之和.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,31 @@ func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
224224
}
225225
```
226226

227+
PHP:
228+
```php
229+
class Solution {
230+
/**
231+
* @param Integer[] $nums
232+
* @param Integer $target
233+
* @return Integer[]
234+
*/
235+
function twoSum($nums, $target) {
236+
if (count($nums) == 0) {
237+
return [];
238+
}
239+
$table = [];
240+
for ($i = 0; $i < count($nums); $i++) {
241+
$temp = $target - $nums[$i];
242+
if (isset($table[$temp])) {
243+
return [$table[$temp], $i];
244+
}
245+
$table[$nums[$i]] = $i;
246+
}
247+
return [];
248+
}
249+
}
250+
```
251+
227252
-----------------------
228253
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
229254
* B站视频:[代码随想录](https://space.bilibili.com/525438321)

problems/0015.三数之和.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,46 @@ function threeSum(array $nums): array
393393
}
394394
```
395395

396+
PHP:
397+
```php
398+
class Solution {
399+
/**
400+
* @param Integer[] $nums
401+
* @return Integer[][]
402+
*/
403+
function threeSum($nums) {
404+
$res = [];
405+
sort($nums);
406+
for ($i = 0; $i < count($nums); $i++) {
407+
if ($nums[$i] > 0) {
408+
return $res;
409+
}
410+
if ($i > 0 && $nums[$i] == $nums[$i - 1]) {
411+
continue;
412+
}
413+
$left = $i + 1;
414+
$right = count($nums) - 1;
415+
while ($left < $right) {
416+
$sum = $nums[$i] + $nums[$left] + $nums[$right];
417+
if ($sum < 0) {
418+
$left++;
419+
}
420+
else if ($sum > 0) {
421+
$right--;
422+
}
423+
else {
424+
$res[] = [$nums[$i], $nums[$left], $nums[$right]];
425+
while ($left < $right && $nums[$left] == $nums[$left + 1]) $left++;
426+
while ($left < $right && $nums[$right] == $nums[$right - 1]) $right--;
427+
$left++;
428+
$right--;
429+
}
430+
}
431+
}
432+
return $res;
433+
}
434+
}
435+
```
396436

397437

398438
-----------------------

problems/0018.四数之和.md

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,49 @@ var fourSum = function(nums, target) {
310310
};
311311
```
312312

313+
PHP:
314+
```php
315+
class Solution {
316+
/**
317+
* @param Integer[] $nums
318+
* @param Integer $target
319+
* @return Integer[][]
320+
*/
321+
function fourSum($nums, $target) {
322+
$res = [];
323+
sort($nums);
324+
for ($i = 0; $i < count($nums); $i++) {
325+
if ($i > 0 && $nums[$i] == $nums[$i - 1]) {
326+
continue;
327+
}
328+
for ($j = $i + 1; $j < count($nums); $j++) {
329+
if ($j > $i + 1 && $nums[$j] == $nums[$j - 1]) {
330+
continue;
331+
}
332+
$left = $j + 1;
333+
$right = count($nums) - 1;
334+
while ($left < $right) {
335+
$sum = $nums[$i] + $nums[$j] + $nums[$left] + $nums[$right];
336+
if ($sum < $target) {
337+
$left++;
338+
}
339+
else if ($sum > $target) {
340+
$right--;
341+
}
342+
else {
343+
$res[] = [$nums[$i], $nums[$j], $nums[$left], $nums[$right]];
344+
while ($left < $right && $nums[$left] == $nums[$left+1]) $left++;
345+
while ($left < $right && $nums[$right] == $nums[$right-1]) $right--;
346+
$left++;
347+
$right--;
348+
}
349+
}
350+
}
351+
}
352+
return $res;
353+
}
354+
}
355+
```
313356

314357
-----------------------
315358
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)

problems/0077.组合优化.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,8 +242,59 @@ var combine = function(n, k) {
242242
};
243243
```
244244

245+
C:
246+
```c
247+
int* path;
248+
int pathTop;
249+
int** ans;
250+
int ansTop;
251+
252+
void backtracking(int n, int k,int startIndex) {
253+
//当path中元素个数为k个时,我们需要将path数组放入ans二维数组中
254+
if(pathTop == k) {
255+
//path数组为我们动态申请,若直接将其地址放入二维数组,path数组中的值会随着我们回溯而逐渐变化
256+
//因此创建新的数组存储path中的值
257+
int* temp = (int*)malloc(sizeof(int) * k);
258+
int i;
259+
for(i = 0; i < k; i++) {
260+
temp[i] = path[i];
261+
}
262+
ans[ansTop++] = temp;
263+
return ;
264+
}
265+
266+
int j;
267+
for(j = startIndex; j <= n- (k - pathTop) + 1;j++) {
268+
//将当前结点放入path数组
269+
path[pathTop++] = j;
270+
//进行递归
271+
backtracking(n, k, j + 1);
272+
//进行回溯,将数组最上层结点弹出
273+
pathTop--;
274+
}
275+
}
245276

277+
int** combine(int n, int k, int* returnSize, int** returnColumnSizes){
278+
//path数组存储符合条件的结果
279+
path = (int*)malloc(sizeof(int) * k);
280+
//ans二维数组存储符合条件的结果数组的集合。(数组足够大,避免极端情况)
281+
ans = (int**)malloc(sizeof(int*) * 10000);
282+
pathTop = ansTop = 0;
246283

284+
//回溯算法
285+
backtracking(n, k, 1);
286+
//最后的返回大小为ans数组大小
287+
*returnSize = ansTop;
288+
//returnColumnSizes数组存储ans二维数组对应下标中一维数组的长度(都为k)
289+
*returnColumnSizes = (int*)malloc(sizeof(int) *(*returnSize));
290+
int i;
291+
for(i = 0; i < *returnSize; i++) {
292+
(*returnColumnSizes)[i] = k;
293+
}
294+
//返回ans二维数组
295+
return ans;
296+
}
297+
```
247298
248299
-----------------------
249300
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)

0 commit comments

Comments
 (0)