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

Commit 3131e6e

Browse files
Merge pull request youngyangyang04#1026 from xiaofei-2020/hash9
添加(0018.四数之和.md):增加typescript版本
2 parents 266bf4e + 12bf8ae commit 3131e6e

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

‎problems/0018.四数之和.md‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,49 @@ var fourSum = function(nums, target) {
311311
};
312312
```
313313

314+
TypeScript:
315+
316+
```typescript
317+
function fourSum(nums: number[], target: number): number[][] {
318+
nums.sort((a, b) => a - b);
319+
let first: number = 0,
320+
second: number,
321+
third: number,
322+
fourth: number;
323+
let length: number = nums.length;
324+
let resArr: number[][] = [];
325+
for (; first < length; first++) {
326+
if (first > 0 && nums[first] === nums[first - 1]) {
327+
continue;
328+
}
329+
for (second = first + 1; second < length; second++) {
330+
if ((second - first) > 1 && nums[second] === nums[second - 1]) {
331+
continue;
332+
}
333+
third = second + 1;
334+
fourth = length - 1;
335+
while (third < fourth) {
336+
let total: number = nums[first] + nums[second] + nums[third] + nums[fourth];
337+
if (total === target) {
338+
resArr.push([nums[first], nums[second], nums[third], nums[fourth]]);
339+
third++;
340+
fourth--;
341+
while (nums[third] === nums[third - 1]) third++;
342+
while (nums[fourth] === nums[fourth + 1]) fourth--;
343+
} else if (total < target) {
344+
third++;
345+
} else {
346+
fourth--;
347+
}
348+
}
349+
}
350+
}
351+
return resArr;
352+
};
353+
```
354+
314355
PHP:
356+
315357
```php
316358
class Solution {
317359
/**

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /