Skip to content

Commit 014ed72

Browse files
author
beck chen
committed
more array and stirngs
1 parent a05e259 commit 014ed72

File tree

4 files changed

+151
-0
lines changed

4 files changed

+151
-0
lines changed
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number[][]}
4+
*/
5+
6+
var threeSum = function (nums) {
7+
if (!nums) { return []; }
8+
nums.sort((num1, num2) => num1 - num2);
9+
const combinations = [];
10+
for (let pointerIndex = 0; pointerIndex < nums.length - 2; pointerIndex++) {
11+
if (nums[pointerIndex] > 0 || nums[pointerIndex - 1] === nums[pointerIndex]) {
12+
continue;
13+
}
14+
15+
twoSum(nums, pointerIndex, combinations);
16+
}
17+
18+
return combinations;
19+
};
20+
21+
function twoSum(nums, threeSumIndex, combinations) {
22+
let startIndex = threeSumIndex + 1, endIndex = nums.length - 1;
23+
while (endIndex > startIndex) {
24+
const sum = nums[startIndex] + nums[endIndex] + nums[threeSumIndex];
25+
if (sum < 0) {
26+
startIndex++;
27+
} else if (sum > 0) {
28+
endIndex--;
29+
} else {
30+
combinations.push([nums[threeSumIndex], nums[startIndex], nums[endIndex]]);
31+
startIndex++;
32+
endIndex--;
33+
34+
while (startIndex < endIndex && nums[startIndex] === nums[startIndex - 1]) {
35+
startIndex++;
36+
}
37+
38+
while (startIndex < endIndex && nums[endIndex] === nums[endIndex + 1]) {
39+
endIndex--;
40+
}
41+
}
42+
}
43+
};
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} target
4+
* @return {number}
5+
*/
6+
var threeSumClosest = function (nums, target) {
7+
let closestDiff = Number.MAX_SAFE_INTEGER;
8+
nums.sort((num1, num2) => num1 - num2);
9+
10+
for (let ii = 0; ii < nums.length; ii++) {
11+
let startIndex = ii + 1, endIndex = nums.length - 1;
12+
while (startIndex < endIndex) {
13+
const currentSum = nums[ii] + nums[startIndex] + nums[endIndex];
14+
const currentDiff = target - currentSum;
15+
16+
if (currentDiff === 0) {
17+
return currentSum;
18+
}
19+
20+
if (Math.abs(currentDiff) < Math.abs(closestDiff)) {
21+
closestDiff = currentDiff;
22+
}
23+
24+
if (currentSum > target) {
25+
endIndex--;
26+
} else {
27+
startIndex++;
28+
}
29+
}
30+
}
31+
32+
return target - closestDiff;
33+
};
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
* @param {string} s
3+
* @param {string} t
4+
* @return {string}
5+
*/
6+
var minWindow = function (str, target) {
7+
if (str.length === 0 || target.length === 0) { return ''; }
8+
9+
const targetCharMap = {};
10+
for (let char of target) {
11+
targetCharMap[char] = targetCharMap[char] === undefined ? 1 : targetCharMap[char] + 1;
12+
}
13+
14+
const currentCharMap = {};
15+
let requiredCharNum = Object.keys(targetCharMap).length;
16+
let satisfiedCharNum = 0;
17+
let startIndex = 0, endIndex = 0;
18+
let globalMinWindowLength = Number.MAX_SAFE_INTEGER;
19+
let globalMinWindow = [];
20+
21+
while (endIndex < str.length) {
22+
const endIndexChar = str[endIndex];
23+
currentCharMap[endIndexChar] = currentCharMap[endIndexChar] === undefined ? 1 : currentCharMap[endIndexChar] + 1;
24+
25+
if (currentCharMap[endIndexChar] === targetCharMap[endIndexChar]) {
26+
satisfiedCharNum++;
27+
}
28+
29+
while (satisfiedCharNum === requiredCharNum) {
30+
const currentWindowLength = endIndex - startIndex + 1;
31+
if (currentWindowLength < globalMinWindowLength) {
32+
globalMinWindowLength = currentWindowLength;
33+
globalMinWindow = [startIndex, endIndex]
34+
}
35+
36+
// before moving startIndex to the right, we
37+
// need to reduce the current char count of
38+
// the character that startIndex is at, since
39+
// we're moving our window away from it
40+
const startIndexChar = str[startIndex];
41+
currentCharMap[startIndexChar]--;
42+
43+
// if startIndex's character is one of the target
44+
// characters, we check to see if
45+
if (currentCharMap[startIndexChar] < targetCharMap[startIndexChar]) {
46+
satisfiedCharNum--;
47+
}
48+
49+
startIndex++;
50+
}
51+
52+
endIndex++;
53+
}
54+
55+
return str.slice(globalMinWindow[0], globalMinWindow[1] + 1)
56+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @param {string} haystack
3+
* @param {string} needle
4+
* @return {number}
5+
*/
6+
var strStr = function (haystack, needle) {
7+
if (needle.length === 0) { return 0; }
8+
9+
for (let ii = 0; ii < haystack.length; ii++) {
10+
if (needle[0] === haystack[ii]) {
11+
const haystackSubstr = haystack.slice(ii, ii + needle.length);
12+
if (needle === haystackSubstr) {
13+
return ii;
14+
}
15+
}
16+
}
17+
18+
return -1;
19+
};

0 commit comments

Comments
 (0)