mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 16:54:50 +08:00
Merge pull request #1256 from pzppzz/master
更新了0015三数之和javascript版本,去除多余代码
This commit is contained in:
@ -313,54 +313,36 @@ func threeSum(nums []int)[][]int{
|
|||||||
javaScript:
|
javaScript:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
/**
|
|
||||||
* @param {number[]} nums
|
|
||||||
* @return {number[][]}
|
|
||||||
*/
|
|
||||||
|
|
||||||
// 循环内不考虑去重
|
|
||||||
var threeSum = function(nums) {
|
var threeSum = function(nums) {
|
||||||
const len = nums.length;
|
const res = [], len = nums.length
|
||||||
if(len < 3) return [];
|
// 将数组排序
|
||||||
nums.sort((a, b) => a - b);
|
nums.sort((a, b) => a - b)
|
||||||
const resSet = new Set();
|
for (let i = 0; i < len; i++) {
|
||||||
for(let i = 0; i < len - 2; i++) {
|
let l = i + 1, r = len - 1, iNum = nums[i]
|
||||||
if(nums[i] > 0) break;
|
// 数组排过序,如果第一个数大于0直接返回res
|
||||||
let l = i + 1, r = len - 1;
|
if (iNum > 0) return res
|
||||||
|
// 去重
|
||||||
|
if (iNum == nums[i - 1]) continue
|
||||||
while(l < r) {
|
while(l < r) {
|
||||||
const sum = nums[i] + nums[l] + nums[r];
|
let lNum = nums[l], rNum = nums[r], threeSum = iNum + lNum + rNum
|
||||||
if(sum < 0) { l++; continue };
|
// 三数之和小于0,则左指针向右移动
|
||||||
if(sum > 0) { r--; continue };
|
if (threeSum < 0) l++
|
||||||
resSet.add(`${nums[i]},${nums[l]},${nums[r]}`);
|
else if (threeSum > 0) r--
|
||||||
l++;
|
else {
|
||||||
r--;
|
res.push([iNum, lNum, rNum])
|
||||||
|
// 去重
|
||||||
|
while(l < r && nums[l] == nums[l + 1]){
|
||||||
|
l++
|
||||||
|
}
|
||||||
|
while(l < r && nums[r] == nums[r - 1]) {
|
||||||
|
r--
|
||||||
|
}
|
||||||
|
l++
|
||||||
|
r--
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Array.from(resSet).map(i => i.split(","));
|
|
||||||
};
|
|
||||||
|
|
||||||
// 去重优化
|
|
||||||
var threeSum = function(nums) {
|
|
||||||
const len = nums.length;
|
|
||||||
if(len < 3) return [];
|
|
||||||
nums.sort((a, b) => a - b);
|
|
||||||
const res = [];
|
|
||||||
for(let i = 0; i < len - 2; i++) {
|
|
||||||
if(nums[i] > 0) break;
|
|
||||||
// a去重
|
|
||||||
if(i > 0 && nums[i] === nums[i - 1]) continue;
|
|
||||||
let l = i + 1, r = len - 1;
|
|
||||||
while(l < r) {
|
|
||||||
const sum = nums[i] + nums[l] + nums[r];
|
|
||||||
if(sum < 0) { l++; continue };
|
|
||||||
if(sum > 0) { r--; continue };
|
|
||||||
res.push([nums[i], nums[l], nums[r]])
|
|
||||||
// b c 去重
|
|
||||||
while(l < r && nums[l] === nums[++l]);
|
|
||||||
while(l < r && nums[r] === nums[--r]);
|
|
||||||
}
|
}
|
||||||
}
|
return res
|
||||||
return res;
|
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
TypeScript:
|
TypeScript:
|
||||||
|
@ -454,31 +454,36 @@ var partition = function(s) {
|
|||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
function partition(s: string): string[][] {
|
function partition(s: string): string[][] {
|
||||||
function isPalindromeStr(s: string, left: number, right: number): boolean {
|
const res: string[][] = []
|
||||||
while (left < right) {
|
const path: string[] = []
|
||||||
if (s[left++] !== s[right--]) {
|
const isHuiwen = (
|
||||||
return false;
|
str: string,
|
||||||
|
startIndex: number,
|
||||||
|
endIndex: number
|
||||||
|
): boolean => {
|
||||||
|
for (; startIndex < endIndex; startIndex++, endIndex--) {
|
||||||
|
if (str[startIndex] !== str[endIndex]) {
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return true;
|
return true
|
||||||
}
|
}
|
||||||
function backTracking(s: string, startIndex: number, route: string[]): void {
|
const rec = (str: string, index: number): void => {
|
||||||
let length: number = s.length;
|
if (index >= str.length) {
|
||||||
if (length === startIndex) {
|
res.push([...path])
|
||||||
resArr.push(route.slice());
|
return
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
for (let i = startIndex; i < length; i++) {
|
for (let i = index; i < str.length; i++) {
|
||||||
if (isPalindromeStr(s, startIndex, i)) {
|
if (!isHuiwen(str, index, i)) {
|
||||||
route.push(s.slice(startIndex, i + 1));
|
continue
|
||||||
backTracking(s, i + 1, route);
|
}
|
||||||
route.pop();
|
path.push(str.substring(index, i + 1))
|
||||||
|
rec(str, i + 1)
|
||||||
|
path.pop()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
rec(s, 0)
|
||||||
const resArr: string[][] = [];
|
return res
|
||||||
backTracking(s, 0, []);
|
|
||||||
return resArr;
|
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user