Skip to content

Commit c3a7127

Browse files
committed
add minimal documentation
1 parent 48aaed4 commit c3a7127

File tree

5 files changed

+64
-7
lines changed

5 files changed

+64
-7
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,6 @@ dist
128128
.yarn/build-state.yml
129129
.yarn/install-state.gz
130130
.pnp.*
131+
132+
# Deno test coverage
133+
cov_profile/

deno.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
{
22
"name": "@taro/leetcode",
3-
"version": "0.0.1",
3+
"version": "0.0.2",
44
"license": "MIT",
55
"exports": "./src/mod.ts",
6-
"tasks": {
7-
"dev": "deno run --watch main.ts"
6+
"publish": {
7+
"include": [ "src/", "README.md", "LICENSE", "deno.json" ]
88
},
99
"imports": {
1010
"@std/assert": "jsr:@std/assert@1"
1111
},
1212
"fmt": {
13-
"include": [ "src/", "tests/" ]
13+
"include": [ "src/", "tests/", "benchmarks/" ]
1414
},
1515
"lint": {
16-
"include": [ "src/", "tests/" ]
16+
"include": [ "src/", "tests/", "benchmarks/" ]
1717
}
1818
}

src/easy.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import type { ListNode } from "./utils.ts";
2+
23
/**
4+
* https://leetcode.com/problems/two-sum/
5+
*
36
* Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
47
* You may assume that each input would have exactly one solution, and you may not use the same element twice.
58
*
@@ -46,6 +49,11 @@ export function twoSum(nums: number[], target: number): number[] {
4649
return [];
4750
}
4851

52+
/**
53+
* https://leetcode.com/problems/palindrome-number/
54+
* @param x
55+
* @returns
56+
*/
4957
export function palindromeNumber(x: number): boolean {
5058
const num = x;
5159
if (x < 0) return false;
@@ -59,6 +67,11 @@ export function palindromeNumber(x: number): boolean {
5967
return num === ans;
6068
}
6169

70+
/**
71+
* https://leetcode.com/problems/roman-to-integer/
72+
* @param s
73+
* @returns
74+
*/
6275
export function romanToInteger(s: string): number {
6376
const multipliers: { [key: string]: number } = {
6477
IV: 4,
@@ -95,6 +108,11 @@ export function romanToInteger(s: string): number {
95108
}, 0);
96109
}
97110

111+
/**
112+
* https://leetcode.com/problems/longest-common-prefix/
113+
* @param strs
114+
* @returns
115+
*/
98116
export function longestCommonPrefix(strs: string[]): string {
99117
let prefix = strs[0];
100118

@@ -111,6 +129,11 @@ export function longestCommonPrefix(strs: string[]): string {
111129
return prefix;
112130
}
113131

132+
/**
133+
* https://leetcode.com/problems/valid-parentheses/
134+
* @param s
135+
* @returns
136+
*/
114137
export function validParentheses(s: string): boolean {
115138
const bracketsMap: { [key: string]: string } = {
116139
")": "(",
@@ -132,6 +155,12 @@ export function validParentheses(s: string): boolean {
132155
return !openBracketsStack.length;
133156
}
134157

158+
/**
159+
* https://leetcode.com/problems/merge-two-sorted-lists/
160+
* @param list1
161+
* @param list2
162+
* @returns
163+
*/
135164
export function mergeTwoSortedLists(
136165
list1: ListNode | null,
137166
list2: ListNode | null,
@@ -148,6 +177,11 @@ export function mergeTwoSortedLists(
148177
}
149178
}
150179

180+
/**
181+
* https://leetcode.com/problems/remove-duplicates-from-sorted-array/
182+
* @param nums
183+
* @returns
184+
*/
151185
export function removeDuplicatesFromSortedArray(nums: number[]): number {
152186
let i = 0;
153187
for (let j = 0; j < nums.length; j++) {
@@ -159,12 +193,24 @@ export function removeDuplicatesFromSortedArray(nums: number[]): number {
159193
return i;
160194
}
161195

196+
/**
197+
* https://leetcode.com/problems/remove-element/
198+
* @param nums
199+
* @param val
200+
* @returns
201+
*/
162202
export function removeElement(nums: number[], val: number): number {
163203
let j = 0;
164204
for (const n of nums) if (n !== val) nums[j++] = n;
165205
return j;
166206
}
167207

208+
/**
209+
* https://leetcode.com/problems/find-the-index-of-the-first-occurrence-in-a-string/
210+
* @param haystack
211+
* @param needle
212+
* @returns
213+
*/
168214
export function findTheIndexOfTheFirstOccurrenceInAString(
169215
haystack: string,
170216
needle: string,
@@ -188,6 +234,12 @@ export function findTheIndexOfTheFirstOccurrenceInAString(
188234
return result;
189235
}
190236

237+
/**
238+
* https://leetcode.com/problems/search-insert-position/
239+
* @param nums
240+
* @param target
241+
* @returns
242+
*/
191243
export function searchInsertPosition(nums: number[], target: number): number {
192244
let min = 0;
193245
let max = nums.length - 1;

src/utils.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
/**
2+
* Definition for a singly linked list.
3+
*/
14
export class ListNode {
25
val: number;
36
next: ListNode | null;

tests/test_easy.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import {
22
assert,
3-
assertAlmostEquals,
43
assertArrayIncludes,
54
assertEquals,
65
assertFalse,
@@ -79,7 +78,7 @@ Deno.test(function removeElementTest() {
7978

8079
nums = [0, 1, 2, 2, 3, 0, 4, 2];
8180
assertEquals(easy.removeElement(nums, 2), 5);
82-
assertArrayIncludes(nums.slice(0, 5), [0, 1, 4, 0, 3])
81+
assertArrayIncludes(nums.slice(0, 5), [0, 1, 4, 0, 3]);
8382
});
8483

8584
Deno.test(function findTheIndexOfTheFirstOccurrenceInAStringTest() {

0 commit comments

Comments
 (0)