Skip to content

Commit 9ca4c7b

Browse files
committed
add 10 easy problems
1 parent db378ba commit 9ca4c7b

File tree

6 files changed

+280
-4
lines changed

6 files changed

+280
-4
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
# JavaScript
1+
# TypeScript
22

3-
Implementations of LeetCode problem solutions in JavaScript.
3+
Implementations of LeetCode problem solutions in TypeScript.

benchmarks/easy.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,8 @@ import * as easy from "../src/easy.ts";
22

33
Deno.bench("twoSum", () => {
44
easy.twoSum([2, 7, 11, 15], 9)
5+
});
6+
7+
Deno.bench("palindromeNumber", () => {
8+
easy.palindromeNumber(123321);
59
});

src/easy.ts

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { ListNode } from "./utils.ts";
12
/**
23
* Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
34
* You may assume that each input would have exactly one solution, and you may not use the same element twice.
@@ -44,3 +45,173 @@ export function twoSum(nums: number[], target: number): number[] {
4445
}
4546
return [];
4647
}
48+
49+
export function palindromeNumber(x: number): boolean {
50+
const num = x;
51+
if (x < 0) return false;
52+
53+
let ans = 0;
54+
while (x > 0) {
55+
const last_digit = x % 10;
56+
ans = ans * 10 + last_digit;
57+
x = Math.floor(x / 10);
58+
}
59+
return num === ans;
60+
}
61+
62+
export function romanToInteger(s: string): number {
63+
const multipliers: { [key: string]: number } = {
64+
IV: 4,
65+
IX: 9,
66+
XL: 40,
67+
XC: 90,
68+
CD: 400,
69+
CM: 900,
70+
I: 1,
71+
V: 5,
72+
X: 10,
73+
L: 50,
74+
C: 100,
75+
D: 500,
76+
M: 1000,
77+
};
78+
const tokens = Object.keys(multipliers);
79+
function next(): string {
80+
for (const token of tokens) {
81+
if (s.startsWith(token)) {
82+
s = s.substring(token.length);
83+
return token;
84+
}
85+
}
86+
throw new Error(`Unknown token: $(s)`);
87+
}
88+
89+
const chars: string[] = [];
90+
91+
while (s.length > 0) chars.push(next());
92+
93+
return chars.reduce((sum, char) => {
94+
return sum += multipliers[char];
95+
}, 0);
96+
}
97+
98+
export function longestCommonPrefix(strs: string[]): string {
99+
let prefix = strs[0];
100+
101+
for (let i = 1; i < strs.length; i++) {
102+
while (!strs[i].startsWith(prefix)) {
103+
prefix = prefix.slice(0, -1);
104+
}
105+
106+
if (prefix === "") {
107+
return prefix;
108+
}
109+
}
110+
111+
return prefix;
112+
}
113+
114+
export function validParentheses(s: string): boolean {
115+
const bracketsMap: { [key: string]: string } = {
116+
")": "(",
117+
"]": "[",
118+
"}": "{",
119+
};
120+
121+
const openBracketsStack = [];
122+
123+
for (let i = 0; i < s.length; i++) {
124+
const currentBracket = s[i];
125+
126+
if (["(", "[", "{"].includes(currentBracket)) {
127+
openBracketsStack.push(currentBracket);
128+
} else if (openBracketsStack.pop() !== bracketsMap[currentBracket]) {
129+
return false;
130+
}
131+
}
132+
return !openBracketsStack.length;
133+
}
134+
135+
export function mergeTwoSortedLists(
136+
list1: ListNode | null,
137+
list2: ListNode | null,
138+
): ListNode | null {
139+
if (list1 === null) return list2;
140+
if (list2 === null) return list1;
141+
142+
if (list1.val < list2.val) {
143+
list1.next = mergeTwoSortedLists(list1.next, list2);
144+
return list1;
145+
} else {
146+
list2.next = mergeTwoSortedLists(list1, list2.next);
147+
return list2;
148+
}
149+
}
150+
151+
export function removeDuplicatesFromSortedArray(nums: number[]): number {
152+
let i = 0;
153+
for (let j = 0; j < nums.length; j++) {
154+
nums[i++] = nums[j];
155+
while (j < nums.length - 1 && nums[j] === nums[j + 1]) {
156+
j++;
157+
}
158+
}
159+
return i;
160+
}
161+
162+
export function removeElement(nums: number[], val: number): number {
163+
let j = 0;
164+
for (const n of nums) if (n !== val) nums[j++] = n;
165+
return j;
166+
}
167+
168+
export function findTheIndexOfTheFirstOccurrenceInAString(
169+
haystack: string,
170+
needle: string,
171+
): number {
172+
let result = -1;
173+
let matchLoc = 0;
174+
175+
for (let i = 0; i < haystack.length; i++) {
176+
if (haystack[i] == needle[matchLoc]) {
177+
matchLoc++;
178+
if (needle.length == matchLoc) {
179+
result = i - matchLoc + 1;
180+
break;
181+
}
182+
} else {
183+
i -= matchLoc;
184+
matchLoc = 0;
185+
}
186+
}
187+
188+
return result;
189+
}
190+
191+
export function searchInsertPosition(nums: number[], target: number): number {
192+
let min = 0;
193+
let max = nums.length - 1;
194+
195+
while (true) {
196+
const middle = Math.floor((min + max) / 2);
197+
const num = nums[middle];
198+
199+
if (max - min <= 1) {
200+
if (target <= nums[min]) {
201+
return min;
202+
} else if (target <= nums[max]) {
203+
return max;
204+
} else {
205+
return max + 1;
206+
}
207+
}
208+
209+
if (num > target) {
210+
max = middle;
211+
} else if (num < target) {
212+
min = middle;
213+
} else {
214+
return middle;
215+
}
216+
}
217+
}

src/mod.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as easy from "./easy.ts";
22
import * as medium from "./medium.ts";
33
import * as hard from "./hard.ts";
4+
import * as utils from "./utils.ts";
45

5-
export { easy, hard, medium };
6+
export { easy, hard, medium, utils };

src/utils.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export class ListNode {
2+
val: number;
3+
next: ListNode | null;
4+
constructor(val?: number, next?: ListNode | null) {
5+
this.val = val === undefined ? 0 : val;
6+
this.next = next === undefined ? null : next;
7+
}
8+
}

tests/test_easy.ts

Lines changed: 93 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,100 @@
1-
import { assertArrayIncludes } from "@std/assert";
1+
import {
2+
assert,
3+
assertAlmostEquals,
4+
assertArrayIncludes,
5+
assertEquals,
6+
assertFalse,
7+
} from "@std/assert";
28
import * as easy from "../src/easy.ts";
9+
import { ListNode } from "../src/utils.ts";
310

411
Deno.test(function twoSumTest() {
512
assertArrayIncludes(easy.twoSum([2, 7, 11, 15], 9), [0, 1]);
613
assertArrayIncludes(easy.twoSum([3, 2, 4], 6), [1, 2]);
714
assertArrayIncludes(easy.twoSum([3, 3], 6), [0, 1]);
815
});
16+
17+
Deno.test(function palindromeNumberTest() {
18+
assert(easy.palindromeNumber(121));
19+
assertFalse(easy.palindromeNumber(-121));
20+
assertFalse(easy.palindromeNumber(10));
21+
});
22+
23+
Deno.test(function romanToIntegerTest() {
24+
assertEquals(easy.romanToInteger("III"), 3);
25+
assertEquals(easy.romanToInteger("LVIII"), 58);
26+
assertEquals(easy.romanToInteger("MCMXCIV"), 1994);
27+
});
28+
29+
Deno.test(function longestCommonPrefixTest() {
30+
assertEquals(easy.longestCommonPrefix(["flower", "flow", "flight"]), "fl");
31+
assertEquals(easy.longestCommonPrefix(["dog", "racecar", "car"]), "");
32+
});
33+
34+
Deno.test(function validParenthesesTest() {
35+
assert(easy.validParentheses("()"));
36+
assert(easy.validParentheses("()[]{}"));
37+
assertFalse(easy.validParentheses("(]"));
38+
assert(easy.validParentheses("([])"));
39+
});
40+
41+
Deno.test(function mergeTwoSortedListsTest() {
42+
assertEquals(
43+
easy.mergeTwoSortedLists(
44+
new ListNode(1, new ListNode(2, new ListNode(4, null))),
45+
new ListNode(1, new ListNode(3, new ListNode(4))),
46+
),
47+
new ListNode(
48+
1,
49+
new ListNode(
50+
1,
51+
new ListNode(
52+
2,
53+
new ListNode(3, new ListNode(4, new ListNode(4, null))),
54+
),
55+
),
56+
),
57+
);
58+
assertEquals(easy.mergeTwoSortedLists(null, null), null);
59+
assertEquals(
60+
easy.mergeTwoSortedLists(null, new ListNode(0)),
61+
new ListNode(0),
62+
);
63+
});
64+
65+
Deno.test(function removeDuplicatesFromSortedArrayTest() {
66+
let nums: number[] = [1, 1, 2];
67+
assertEquals(easy.removeDuplicatesFromSortedArray(nums), 2);
68+
assertEquals(nums.slice(0, 2), [1, 2]);
69+
70+
nums = [0, 0, 1, 1, 1, 2, 2, 3, 3, 4];
71+
assertEquals(easy.removeDuplicatesFromSortedArray(nums), 5);
72+
assertEquals(nums.slice(0, 5), [0, 1, 2, 3, 4]);
73+
});
74+
75+
Deno.test(function removeElementTest() {
76+
let nums: number[] = [3, 2, 2, 3];
77+
assertEquals(easy.removeElement(nums, 3), 2);
78+
assertArrayIncludes(nums.slice(0, 2), [2, 2]);
79+
80+
nums = [0, 1, 2, 2, 3, 0, 4, 2];
81+
assertEquals(easy.removeElement(nums, 2), 5);
82+
assertArrayIncludes(nums.slice(0, 5), [0, 1, 4, 0, 3])
83+
});
84+
85+
Deno.test(function findTheIndexOfTheFirstOccurrenceInAStringTest() {
86+
assertEquals(
87+
easy.findTheIndexOfTheFirstOccurrenceInAString("sadbutsad", "sad"),
88+
0,
89+
);
90+
assertEquals(
91+
easy.findTheIndexOfTheFirstOccurrenceInAString("leetcode", "leeto"),
92+
-1,
93+
);
94+
});
95+
96+
Deno.test(function searchInsertPositionTest() {
97+
assertEquals(easy.searchInsertPosition([1, 3, 5, 6], 5), 2);
98+
assertEquals(easy.searchInsertPosition([1, 3, 5, 6], 2), 1);
99+
assertEquals(easy.searchInsertPosition([1, 3, 5, 6], 7), 4);
100+
});

0 commit comments

Comments
 (0)