Skip to content

Commit 87d0c54

Browse files
committed
feat(*):base on deno
1 parent 929cee9 commit 87d0c54

File tree

47 files changed

+1246
-1181
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+1246
-1181
lines changed

.prettierrc

-4
This file was deleted.

algorithms/0001.two-sum/index.test.ts

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import {
2+
assertEquals,
3+
assertArrayContains,
4+
} from "https://deno.land/std/testing/asserts.ts";
5+
6+
Deno.test("0001.two-sum", () => {
7+
const x = 1 + 2;
8+
assertEquals(x, 3);
9+
assertArrayContains([1, 2, 3, 4, 5, 6], [3], "Expected 3 to be in the array");
10+
});

algorithms/0001.two-sum/index.ts

+28-41
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,19 @@
11
/**
22
* # 1. Two Sum
3-
*
3+
*
44
* Given an array of integers, return **indices** of the two numbers such that they add up to a specific target.
5-
*
5+
*
66
* You may assume that each input would have ***exactly*** one solution, and you may not use the same element twice.
7-
*
7+
*
88
* ## Example
9-
*
9+
*
1010
* ```bash
1111
* Given nums = [2, 7, 11, 15], target = 9,
12-
*
12+
*
1313
* Because nums[0] + nums[1] = 2 + 7 = 9,
1414
* return [0, 1].
1515
* ```
1616
*/
17-
type Submission = (nums: number[], target: number) => number[]
1817

1918
/**
2019
* 嵌套循环遍历
@@ -25,18 +24,15 @@ type Submission = (nums: number[], target: number) => number[]
2524
* @runtime 120 ms < 40.065%
2625
* @memory N/A
2726
*/
28-
export const twoSum: Submission = (
29-
nums: number[],
30-
target: number
31-
): number[] => {
27+
export const twoSum = (nums: number[], target: number): number[] => {
3228
for (let i: number = 0; i < nums.length; i++) {
3329
for (let j = i + 1; j < nums.length; j++) {
3430
if (nums[i] + nums[j] == target) {
35-
return [i, j]
31+
return [i, j];
3632
}
3733
}
3834
}
39-
}
35+
};
4036

4137
/**
4238
* 哈希存储
@@ -45,19 +41,16 @@ export const twoSum: Submission = (
4541
* @runtime 80 ms < 59.00%
4642
* @memory N/A
4743
*/
48-
export const twoSum1: Submission = (
49-
nums: number[],
50-
target: number
51-
): number[] => {
52-
const map: any = {}
53-
nums.forEach((i, k) => (map[i] = k))
44+
export const twoSum1 = (nums: number[], target: number): number[] => {
45+
const map: any = {};
46+
nums.forEach((i, k) => (map[i] = k));
5447
for (let i = 0; i < nums.length; i++) {
55-
const x = target - nums[i]
48+
const x = target - nums[i];
5649
if (x in map && map[x] != i) {
57-
return [i, map[x]]
50+
return [i, map[x]];
5851
}
5952
}
60-
}
53+
};
6154

6255
/**
6356
* 哈希存储
@@ -66,22 +59,19 @@ export const twoSum1: Submission = (
6659
* @runtime 56 ms < 100.00%
6760
* @memory N/A
6861
*/
69-
export const twoSum2: Submission = (
70-
nums: number[],
71-
target: number
72-
): number[] => {
73-
const map: any = {}
74-
const length: number = nums.length
62+
export const twoSum2 = (nums: number[], target: number): number[] => {
63+
const map: any = {};
64+
const length: number = nums.length;
7565
for (let i = 0; i < length; i++) {
76-
map[nums[i]] = i
66+
map[nums[i]] = i;
7767
}
7868
for (let i = 0; i < length; i++) {
79-
const x = target - nums[i]
69+
const x = target - nums[i];
8070
if (x in map && map[x] != i) {
81-
return [i, map[x]]
71+
return [i, map[x]];
8272
}
8373
}
84-
}
74+
};
8575

8676
/**
8777
* 哈希遍历
@@ -90,17 +80,14 @@ export const twoSum2: Submission = (
9080
* @runtime 52 ms < 100%
9181
* @memory N/A
9282
*/
93-
export const twoSum3: Submission = (
94-
nums: number[],
95-
target: number
96-
): number[] => {
97-
const map: { number: number } | any = {}
98-
const length = nums.length
83+
export const twoSum3 = (nums: number[], target: number): number[] => {
84+
const map: { number: number } | any = {};
85+
const length = nums.length;
9986
for (let i = 0; i < length; i++) {
100-
const x = target - nums[i]
87+
const x = target - nums[i];
10188
if (x in map && map[x] != i) {
102-
return [map[x], i]
89+
return [map[x], i];
10390
}
104-
map[nums[i]] = i
91+
map[nums[i]] = i;
10592
}
106-
}
93+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
2+
3+
import { lengthOfLongestSubstring } from "./index.ts";
4+
5+
Deno.test({
6+
name: `0003 Longest Substring Without Repeating Characters
7+
Input: "abcabcbb"
8+
Output: 3
9+
`,
10+
fn(): void {
11+
const result = lengthOfLongestSubstring("abcabcbb");
12+
assertEquals(result, 3);
13+
},
14+
});
15+
16+
Deno.test({
17+
name: `0003 Longest Substring Without Repeating Characters
18+
Input: "bbbbb"
19+
Output: 1
20+
`,
21+
fn(): void {
22+
const result = lengthOfLongestSubstring("bbbbb");
23+
assertEquals(result, 1);
24+
},
25+
});
26+
27+
Deno.test({
28+
name: `0003 Longest Substring Without Repeating Characters
29+
Input: "pwwkew"
30+
Output: 3
31+
`,
32+
fn(): void {
33+
const result = lengthOfLongestSubstring("pwwkew");
34+
assertEquals(result, 3);
35+
},
36+
});

algorithms/0003.longest-substring-without-repeating-characters/index.ts

+13-14
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
* Note that the answer must be a substring, "pwke" is a subsequence and not a substring.
2525
* ```
2626
*/
27-
type Submission = (s: string) => number
2827

2928
/**
3029
*
@@ -33,27 +32,27 @@ type Submission = (s: string) => number
3332
* @runtime 116 ms > 55.08%
3433
* @memory N/A
3534
*/
36-
export const lengthOfLongestSubstring: Submission = (s: string): number => {
37-
let maxSub: string = ''
38-
let currentSub: string = ''
35+
export const lengthOfLongestSubstring = (s: string): number => {
36+
let maxSub: string = "";
37+
let currentSub: string = "";
3938

40-
const arr: string[] = s.split('')
39+
const arr: string[] = s.split("");
4140
arr.forEach((s: string) => {
4241
if (currentSub.includes(s)) {
4342
// 存在
4443
if (currentSub.length >= maxSub.length) {
45-
maxSub = currentSub
44+
maxSub = currentSub;
4645
}
47-
let [lStr, rStr] = currentSub.split(s)
48-
currentSub = rStr || ''
49-
currentSub += s
46+
let [lStr, rStr] = currentSub.split(s);
47+
currentSub = rStr || "";
48+
currentSub += s;
5049
} else {
5150
// 不存在
52-
currentSub += s
51+
currentSub += s;
5352
if (currentSub.length >= maxSub.length) {
54-
maxSub = currentSub
53+
maxSub = currentSub;
5554
}
5655
}
57-
})
58-
return maxSub.length
59-
}
56+
});
57+
return maxSub.length;
58+
};

algorithms/0004.median-of-two-sorted-arrays/READEME.md

-28
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { assertEquals } from "https://deno.land/std/testing/asserts.ts";
2+
3+
import { findMedianSortedArrays } from "./index.ts";
4+
5+
Deno.test("0004.median-of-two-sorted-arrays", () => {
6+
const result = findMedianSortedArrays([1, 3], [2]);
7+
assertEquals(result, 2.0);
8+
});
9+
10+
Deno.test("0004.median-of-two-sorted-arrays", () => {
11+
const result = findMedianSortedArrays([1, 2], [3, 4]);
12+
assertEquals(result, 2.5);
13+
});
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,28 @@
11
/**
22
* # 4. Median of Tow Sorted Arrays
3-
*
3+
*
44
* There are two sorted arrays **nums1** and **nums2** of size m and n respectively.
5-
*
5+
*
66
* Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)).
7-
*
7+
*
88
* You may assume **nums1** and **nums2** cannot be both empty.
9-
*
9+
*
1010
* ## Example
11-
*
11+
*
1212
* ```bash
1313
* nums1 = [1, 3]
1414
* nums2 = [2]
15-
*
15+
*
1616
* The median is 2.0
1717
* ```
18-
*
18+
*
1919
* ```bash
2020
* nums1 = [1, 2]
2121
* nums2 = [3, 4]
22-
*
22+
*
2323
* The median is (2 + 3)/2 = 2.5
2424
* ```
2525
*/
26-
type Submission = (nums1: number[], nums2: number[]) => number
2726

2827
/**
2928
* 递归
@@ -32,46 +31,46 @@ type Submission = (nums1: number[], nums2: number[]) => number
3231
* @runtime 168 ms > 19.48%
3332
* @memory 15.8 MB <
3433
*/
35-
export const findMedianSortedArrays: Submission = (
34+
export const findMedianSortedArrays = (
3635
nums1: number[],
37-
nums2: number[]
36+
nums2: number[],
3837
): number => {
3938
const findKth = (nums1: number[], nums2: number[], k: number): number => {
40-
let m: number = nums1.length
41-
let n: number = nums2.length
39+
let m: number = nums1.length;
40+
let n: number = nums2.length;
4241
if (m > n) {
43-
return findKth(nums2, nums1, k)
42+
return findKth(nums2, nums1, k);
4443
}
4544
if (m === 0) {
46-
return nums2[k - 1]
45+
return nums2[k - 1];
4746
}
4847
if (k === 1) {
49-
return Math.min(nums1[0], nums2[0])
48+
return Math.min(nums1[0], nums2[0]);
5049
}
51-
let pa = Math.floor(k / 2) < m ? Math.floor(k / 2) : m
52-
let pb = k - pa
50+
let pa = Math.floor(k / 2) < m ? Math.floor(k / 2) : m;
51+
let pb = k - pa;
5352
if (nums1[pa - 1] < nums2[pb - 1]) {
54-
let t1 = nums1.slice(pa)
55-
return findKth(t1, nums2, k - pa)
53+
let t1 = nums1.slice(pa);
54+
return findKth(t1, nums2, k - pa);
5655
} else if (nums1[pa - 1] > nums2[pb - 1]) {
57-
let t2 = nums2.slice(pb)
56+
let t2 = nums2.slice(pb);
5857
//nums2.splice(0,pb);
59-
return findKth(nums1, t2, k - pb)
58+
return findKth(nums1, t2, k - pb);
6059
} else {
61-
return nums1[pa - 1]
60+
return nums1[pa - 1];
6261
}
63-
}
62+
};
6463

65-
let m = nums1.length
66-
let n = nums2.length
67-
let tol = m + n
64+
let m = nums1.length;
65+
let n = nums2.length;
66+
let tol = m + n;
6867
if (tol / 2 - Math.floor(tol / 2) > 0.1) {
69-
return findKth(nums1, nums2, Math.floor(tol / 2) + 1)
68+
return findKth(nums1, nums2, Math.floor(tol / 2) + 1);
7069
} else {
7170
return (
7271
(findKth(nums1, nums2, Math.floor(tol / 2)) +
7372
findKth(nums1, nums2, Math.floor(tol / 2) + 1)) /
7473
2
75-
)
74+
);
7675
}
77-
}
76+
};

0 commit comments

Comments
 (0)