mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
Merge branch 'youngyangyang04:master' into remote
This commit is contained in:
@ -332,7 +332,43 @@ var threeSum = function(nums) {
|
||||
return res;
|
||||
};
|
||||
```
|
||||
TypeScript:
|
||||
|
||||
```typescript
|
||||
function threeSum(nums: number[]): number[][] {
|
||||
nums.sort((a, b) => a - b);
|
||||
let length = nums.length;
|
||||
let left: number = 0,
|
||||
right: number = length - 1;
|
||||
let resArr: number[][] = [];
|
||||
for (let i = 0; i < length; i++) {
|
||||
if (i > 0 && nums[i] === nums[i - 1]) {
|
||||
continue;
|
||||
}
|
||||
left = i + 1;
|
||||
right = length - 1;
|
||||
while (left < right) {
|
||||
let total: number = nums[i] + nums[left] + nums[right];
|
||||
if (total === 0) {
|
||||
resArr.push([nums[i], nums[left], nums[right]]);
|
||||
left++;
|
||||
right--;
|
||||
while (nums[right] === nums[right + 1]) {
|
||||
right--;
|
||||
}
|
||||
while (nums[left] === nums[left - 1]) {
|
||||
left++;
|
||||
}
|
||||
} else if (total < 0) {
|
||||
left++;
|
||||
} else {
|
||||
right--;
|
||||
}
|
||||
}
|
||||
}
|
||||
return resArr;
|
||||
};
|
||||
```
|
||||
|
||||
ruby:
|
||||
```ruby
|
||||
|
@ -311,7 +311,49 @@ var fourSum = function(nums, target) {
|
||||
};
|
||||
```
|
||||
|
||||
TypeScript:
|
||||
|
||||
```typescript
|
||||
function fourSum(nums: number[], target: number): number[][] {
|
||||
nums.sort((a, b) => a - b);
|
||||
let first: number = 0,
|
||||
second: number,
|
||||
third: number,
|
||||
fourth: number;
|
||||
let length: number = nums.length;
|
||||
let resArr: number[][] = [];
|
||||
for (; first < length; first++) {
|
||||
if (first > 0 && nums[first] === nums[first - 1]) {
|
||||
continue;
|
||||
}
|
||||
for (second = first + 1; second < length; second++) {
|
||||
if ((second - first) > 1 && nums[second] === nums[second - 1]) {
|
||||
continue;
|
||||
}
|
||||
third = second + 1;
|
||||
fourth = length - 1;
|
||||
while (third < fourth) {
|
||||
let total: number = nums[first] + nums[second] + nums[third] + nums[fourth];
|
||||
if (total === target) {
|
||||
resArr.push([nums[first], nums[second], nums[third], nums[fourth]]);
|
||||
third++;
|
||||
fourth--;
|
||||
while (nums[third] === nums[third - 1]) third++;
|
||||
while (nums[fourth] === nums[fourth + 1]) fourth--;
|
||||
} else if (total < target) {
|
||||
third++;
|
||||
} else {
|
||||
fourth--;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return resArr;
|
||||
};
|
||||
```
|
||||
|
||||
PHP:
|
||||
|
||||
```php
|
||||
class Solution {
|
||||
/**
|
||||
|
@ -200,6 +200,27 @@ var reverseString = function(s) {
|
||||
};
|
||||
```
|
||||
|
||||
TypeScript:
|
||||
|
||||
```typescript
|
||||
/**
|
||||
Do not return anything, modify s in-place instead.
|
||||
*/
|
||||
function reverseString(s: string[]): void {
|
||||
let length: number = s.length;
|
||||
let left: number = 0,
|
||||
right: number = length - 1;
|
||||
let tempStr: string;
|
||||
while (left < right) {
|
||||
tempStr = s[left];
|
||||
s[left] = s[right];
|
||||
s[right] = tempStr;
|
||||
left++;
|
||||
right--;
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
Swift:
|
||||
|
||||
```swift
|
||||
|
@ -252,6 +252,28 @@ var reverseStr = function(s, k) {
|
||||
|
||||
```
|
||||
|
||||
TypeScript:
|
||||
|
||||
```typescript
|
||||
function reverseStr(s: string, k: number): string {
|
||||
let left: number, right: number;
|
||||
let arr: string[] = s.split('');
|
||||
let temp: string;
|
||||
for (let i = 0, length = arr.length; i < length; i += 2 * k) {
|
||||
left = i;
|
||||
right = (i + k - 1) >= length ? length - 1 : i + k - 1;
|
||||
while (left < right) {
|
||||
temp = arr[left];
|
||||
arr[left] = arr[right];
|
||||
arr[right] = temp;
|
||||
left++;
|
||||
right--;
|
||||
}
|
||||
}
|
||||
return arr.join('');
|
||||
};
|
||||
```
|
||||
|
||||
Swift:
|
||||
|
||||
```swift
|
||||
|
@ -298,6 +298,33 @@ javaScript:
|
||||
};
|
||||
```
|
||||
|
||||
TypeScript:
|
||||
|
||||
```typescript
|
||||
function replaceSpace(s: string): string {
|
||||
let arr: string[] = s.split('');
|
||||
let spaceNum: number = 0;
|
||||
let oldLength: number = arr.length;
|
||||
for (let i = 0; i < oldLength; i++) {
|
||||
if (arr[i] === ' ') {
|
||||
spaceNum++;
|
||||
}
|
||||
}
|
||||
arr.length = oldLength + 2 * spaceNum;
|
||||
let cur: number = oldLength - 1;
|
||||
for (let i = arr.length - 1; i >= 0; i--, cur--) {
|
||||
if (arr[cur] !== ' ') {
|
||||
arr[i] = arr[cur]
|
||||
} else {
|
||||
arr[i] = '0';
|
||||
arr[--i] = '2';
|
||||
arr[--i] = '%';
|
||||
}
|
||||
}
|
||||
return arr.join('');
|
||||
};
|
||||
```
|
||||
|
||||
Swift:
|
||||
|
||||
```swift
|
||||
|
Reference in New Issue
Block a user