Compare commits
7 Commits
78b50806e3
...
77824baa7c
| Author | SHA1 | Date |
|---|---|---|
|
|
77824baa7c | 1 year ago |
|
|
807a5ae9cf | 1 year ago |
|
|
656b5c6eb5 | 1 year ago |
|
|
a54b5b8996 | 1 year ago |
|
|
8dcb175459 | 1 year ago |
|
|
35c77a3a23 | 1 year ago |
|
|
f16f797381 | 1 year ago |
@ -0,0 +1,26 @@
|
|||||||
|
import { describe, it, expect } from "vitest";
|
||||||
|
import { backspaceCompare } from "./backspaceCompare.ts";
|
||||||
|
|
||||||
|
describe("backspaceCompare", () => {
|
||||||
|
|
||||||
|
it("ab#c, ad#c => true", () => {
|
||||||
|
expect(backspaceCompare("ab#c", "ad#c")).toBeTruthy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("ab##, c#d# => true", () => {
|
||||||
|
expect(backspaceCompare("ab##", "c#d#")).toBeTruthy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("a#c, b => false", () => {
|
||||||
|
expect(backspaceCompare("a#c", "b")).toBeFalsy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("bxj##tw, bxj###tw => false", () => {
|
||||||
|
expect(backspaceCompare("bxj##tw", "bxj###tw")).toBeFalsy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("nzp#o#g, b#nzp#o#g => true", () => {
|
||||||
|
expect(backspaceCompare("nzp#o#g", "b#nzp#o#g")).toBeTruthy();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
@ -0,0 +1,31 @@
|
|||||||
|
function findNotSkipedIndex(s: string, i: number) {
|
||||||
|
let skipCount = 0;
|
||||||
|
while (i >= 0 && (skipCount > 0 || s[i] === '#')) {
|
||||||
|
if (s[i] === '#') {
|
||||||
|
skipCount += 1;
|
||||||
|
i -= 1;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
skipCount -= 1;
|
||||||
|
i -= 1;
|
||||||
|
}
|
||||||
|
return i;
|
||||||
|
};
|
||||||
|
|
||||||
|
export function backspaceCompare(s: string, t: string): boolean {
|
||||||
|
let sI = s.length;
|
||||||
|
let tI = t.length;
|
||||||
|
|
||||||
|
while (sI > 0 && tI > 0) {
|
||||||
|
sI = findNotSkipedIndex(s, sI - 1);
|
||||||
|
tI = findNotSkipedIndex(t, tI - 1);
|
||||||
|
|
||||||
|
|
||||||
|
if (sI >= 0 && tI >= 0 && s[sI] !== t[tI]) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return findNotSkipedIndex(s, sI - 1) === findNotSkipedIndex(t, tI - 1);
|
||||||
|
};
|
||||||
@ -0,0 +1,3 @@
|
|||||||
|
import { backspaceCompare } from "./backspaceCompare";
|
||||||
|
|
||||||
|
export default backspaceCompare;
|
||||||
@ -0,0 +1,3 @@
|
|||||||
|
import { intersectionSortedArray } from "./intersectionSortedArray";
|
||||||
|
|
||||||
|
export default intersectionSortedArray;
|
||||||
@ -0,0 +1,13 @@
|
|||||||
|
import { describe, it, expect } from "vitest";
|
||||||
|
import { intersectionSortedArray } from "./intersectionSortedArray.ts";
|
||||||
|
|
||||||
|
describe("intersectionSortedArray", () => {
|
||||||
|
|
||||||
|
it("[1,2,3,3,4,5,6] [3,3,5] => [3,3,5]", () => {
|
||||||
|
expect(intersectionSortedArray([1, 2, 3, 3, 4, 5, 6], [3, 3, 5])).toEqual([3, 3, 5]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("[1,2,3,4,5,6] [3,5] => [3,5]", () => {
|
||||||
|
expect(intersectionSortedArray([1, 2, 3, 4, 5, 6], [3, 5])).toEqual([3, 5]);
|
||||||
|
});
|
||||||
|
});
|
||||||
@ -0,0 +1,20 @@
|
|||||||
|
export function intersectionSortedArray(a: number[], b: number[]): number[] {
|
||||||
|
let res: number[] = [];
|
||||||
|
let aI = 0;
|
||||||
|
let bI = 0;
|
||||||
|
|
||||||
|
while (aI < a.length && bI < b.length) {
|
||||||
|
let aValue = a[aI];
|
||||||
|
let bValue = b[bI];
|
||||||
|
|
||||||
|
if (aValue === bValue) {
|
||||||
|
res.push(aValue);
|
||||||
|
aI++;
|
||||||
|
bI++;
|
||||||
|
} else {
|
||||||
|
aValue < bValue ? aI++ : bI++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return res;
|
||||||
|
};
|
||||||
@ -0,0 +1,3 @@
|
|||||||
|
import { isPalindrome } from "./isPalindrome";
|
||||||
|
|
||||||
|
export default isPalindrome;
|
||||||
@ -0,0 +1,20 @@
|
|||||||
|
import { describe, it, expect } from "vitest";
|
||||||
|
import { isPalindrome } from "./isPalindrome.ts";
|
||||||
|
|
||||||
|
describe("isPalindrome", () => {
|
||||||
|
it("A man, a plan, a canal: Panama => true", () => {
|
||||||
|
expect(isPalindrome("A man, a plan, a canal: Panama")).toBeTruthy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it("race a car => false", () => {
|
||||||
|
expect(isPalindrome("race a car")).toBeFalsy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it(" _ => true", () => {
|
||||||
|
expect(isPalindrome(" ")).toBeTruthy();
|
||||||
|
});
|
||||||
|
|
||||||
|
it(" 0P => false", () => {
|
||||||
|
expect(isPalindrome("0P")).toBeFalsy();
|
||||||
|
});
|
||||||
|
});
|
||||||
@ -0,0 +1,30 @@
|
|||||||
|
function isLetter(c: string) {
|
||||||
|
if (/\d/.test(c)) return true;
|
||||||
|
return c.toLowerCase() != c.toUpperCase();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
export function isPalindrome(s: string): boolean {
|
||||||
|
let L = 0;
|
||||||
|
let R = s.length - 1;
|
||||||
|
|
||||||
|
while (L <= R) {
|
||||||
|
let lChar = s[L].toLowerCase();
|
||||||
|
let rChar = s[R].toLowerCase();
|
||||||
|
|
||||||
|
if (!isLetter(lChar)) {
|
||||||
|
L++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (!isLetter(rChar)) {
|
||||||
|
R--;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (lChar !== rChar) return false;
|
||||||
|
L++;
|
||||||
|
R--;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
};
|
||||||
@ -0,0 +1,3 @@
|
|||||||
|
import { maxArea } from "./maxArea";
|
||||||
|
|
||||||
|
export default maxArea;
|
||||||
@ -0,0 +1,13 @@
|
|||||||
|
import { describe, it, expect } from "vitest";
|
||||||
|
import { maxArea } from "./maxArea.ts";
|
||||||
|
|
||||||
|
describe("maxArea", () => {
|
||||||
|
|
||||||
|
it("[1,8,6,2,5,4,8,3,7] => 49", () => {
|
||||||
|
expect(maxArea([1, 8, 6, 2, 5, 4, 8, 3, 7])).toBe(49);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("[1,1] => 1", () => {
|
||||||
|
expect(maxArea([1, 1])).toBe(1);
|
||||||
|
});
|
||||||
|
});
|
||||||
@ -0,0 +1,15 @@
|
|||||||
|
export function maxArea(height: number[]): number {
|
||||||
|
let L = 0;
|
||||||
|
let R = height.length - 1;
|
||||||
|
let res = 0;
|
||||||
|
|
||||||
|
while (L <= R) {
|
||||||
|
let hL = height[L];
|
||||||
|
let hR = height[R];
|
||||||
|
let currArea = Math.min(hL, hR) * (R - L);
|
||||||
|
if (currArea > res) res = currArea;
|
||||||
|
hR > hL ? L++ : R--;
|
||||||
|
}
|
||||||
|
|
||||||
|
return res;
|
||||||
|
};
|
||||||
@ -0,0 +1,3 @@
|
|||||||
|
import { sortedSquares } from "./sortedSquares";
|
||||||
|
|
||||||
|
export default sortedSquares;
|
||||||
@ -0,0 +1,12 @@
|
|||||||
|
import { describe, it, expect } from "vitest";
|
||||||
|
import { sortedSquares } from "./sortedSquares.ts";
|
||||||
|
|
||||||
|
describe("sortedSquares", () => {
|
||||||
|
it("[-4,-1,0,3,10] => [0,1,9,16,100]", () => {
|
||||||
|
expect(sortedSquares([-4, -1, 0, 3, 10])).toEqual([0, 1, 9, 16, 100]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("[-7,-3,2,3,11] => [4,9,9,49,121]", () => {
|
||||||
|
expect(sortedSquares([-7, -3, 2, 3, 11])).toEqual([4, 9, 9, 49, 121]);
|
||||||
|
});
|
||||||
|
});
|
||||||
@ -0,0 +1,39 @@
|
|||||||
|
// export function sortedSquares(nums: number[]): number[] {
|
||||||
|
// const result: number[] = [];
|
||||||
|
// let L = 0;
|
||||||
|
// let R = nums.length - 1;
|
||||||
|
|
||||||
|
// while (result.length !== nums.length) {
|
||||||
|
// let leftValue = Math.pow(nums[L], 2);
|
||||||
|
// let rightValue = Math.pow(nums[R], 2);
|
||||||
|
|
||||||
|
// if (leftValue > rightValue) {
|
||||||
|
// result.push(leftValue);
|
||||||
|
// L++;
|
||||||
|
// } else {
|
||||||
|
// result.push(rightValue);
|
||||||
|
// R--
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// return result.reverse();
|
||||||
|
// };
|
||||||
|
|
||||||
|
export function sortedSquares(nums: number[]): number[] {
|
||||||
|
const result: number[] = Array(nums.length).fill(0);
|
||||||
|
let L = 0;
|
||||||
|
let R = nums.length - 1;
|
||||||
|
let indexResult = result.length - 1;
|
||||||
|
|
||||||
|
while (L <= R) {
|
||||||
|
let leftValue = Math.pow(nums[L], 2);
|
||||||
|
let rightValue = Math.pow(nums[R], 2);
|
||||||
|
let resultValue = leftValue > rightValue ? leftValue : rightValue;
|
||||||
|
leftValue > rightValue ? L++ : R--;
|
||||||
|
|
||||||
|
result[indexResult] = resultValue;
|
||||||
|
indexResult--;
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
};
|
||||||
@ -0,0 +1,3 @@
|
|||||||
|
import { twoSum } from "./twoSum";
|
||||||
|
|
||||||
|
export default twoSum;
|
||||||
@ -0,0 +1,21 @@
|
|||||||
|
import { describe, it, expect } from "vitest";
|
||||||
|
import { twoSum } from "./twoSum.ts";
|
||||||
|
|
||||||
|
describe("twoSum", () => {
|
||||||
|
|
||||||
|
it("[2,7,11,15] 9 => [1,2]", () => {
|
||||||
|
expect(twoSum([2, 7, 11, 15], 9)).toEqual([1, 2]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("[2,3,4] 6 => [1,3]", () => {
|
||||||
|
expect(twoSum([2, 3, 4], 6)).toEqual([1, 3]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("[-1, 0] -1 => [1, 2]", () => {
|
||||||
|
expect(twoSum([-1, 0], -1)).toEqual([1, 2]);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("[5,25,75] 100 => [2,3]", () => {
|
||||||
|
expect(twoSum([5, 25, 75], 100)).toEqual([2, 3]);
|
||||||
|
})
|
||||||
|
});
|
||||||
@ -0,0 +1,12 @@
|
|||||||
|
export function twoSum(numbers: number[], target: number): number[] {
|
||||||
|
let L = 0;
|
||||||
|
let R = numbers.length - 1;
|
||||||
|
|
||||||
|
while (L <= R) {
|
||||||
|
const curSum = numbers[L] + numbers[R];
|
||||||
|
if (curSum === target) return [L + 1, R + 1];
|
||||||
|
curSum > target ? R-- : L++;
|
||||||
|
}
|
||||||
|
|
||||||
|
return [-1, -1];
|
||||||
|
};
|
||||||
Loading…
Reference in New Issue