Skip to content

Commit 524dad1

Browse files
committed
fix types
1 parent d46ea9c commit 524dad1

File tree

17 files changed

+162
-179
lines changed

17 files changed

+162
-179
lines changed

.github/workflows/ci.yml

Whitespace-only changes.

typescript/0017_letter_combinations_of_a_phone_number/0017_letter_combinations_of_a_phone_number.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ const letterCombinations = function (digits: string) {
3030
return;
3131
}
3232

33+
// @ts-ignore
3334
const letters = numbers[digits[index]];
3435

3536
for (let i = 0; i < letters.length; i++) {

typescript/0024_swap_nodes_in_pairs/0024_swap_nodes_in_pairs.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
1-
class ListNode {
2-
val: number;
3-
next: ListNode | null;
4-
constructor(val: number) {
5-
this.val = val;
6-
this.next = null;
7-
}
8-
}
9-
101
const swapPairs = function (head: ListNode) {
11-
let newHead = new ListNode(-1);
2+
let newHead = new ListNode({ value: -1 });
123
newHead.next = head;
134
let current: ListNode | null = newHead;
145
let stack: ListNode[] = [];

typescript/0053_maximum_subarray/0053_maximum_subarray.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
* Given an integer array nums,
33
* find the contiguous subarray (containing at least one number)
44
* which has the largest sum and return its sum.
5-
*/
5+
*/
66

77
/**
88
* @param {number[]} nums
99
* @return {number}
1010
*/
11-
var maxSubArray = function(nums) {
11+
var maxSubArray = function (nums: number[]) {
1212
let sum = 0;
1313
let max = -Infinity;
1414

typescript/0066_plus_one/0066_plus_one.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* You may assume the integer does not contain any leading zero, except the number 0 itself.
77
*/
88

9-
var plusOne = function(digits) {
9+
var plusOne = function (digits: number[]) {
1010
for (let i = digits.length - 1; i >= 0; i--) {
1111
digits[i] += 1;
1212

typescript/0139_word_break/0139_word_break.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55
* Note:
66
* The same word in the dictionary may be reused multiple times in the segmentation.
77
* You may assume the dictionary does not contain duplicate words.
8-
*/
8+
*/
99

1010
/**
1111
* @param {string} s
1212
* @param {string[]} wordDict
1313
* @return {boolean}
1414
*/
15-
var wordBreak = function(s, wordDict) {
16-
wordDict = new Set(wordDict);
17-
isWordBreak = new Set().add(0);
15+
var wordBreak = function (s: string, wordDict: string[]) {
16+
const wordSet = new Set(wordDict);
17+
const isWordBreak = new Set().add(0);
1818

1919
for (let i = 0; i < s.length + 1; i++) {
2020
for (let j = 0; j < i; j++) {
@@ -24,7 +24,7 @@ var wordBreak = function(s, wordDict) {
2424
continue;
2525
}
2626

27-
if (wordDict.has(s.substring(j, i))) {
27+
if (wordSet.has(s.substring(j, i))) {
2828
isWordBreak.add(i);
2929
break;
3030
}

typescript/0146_lru_cache/0146_lru_cache.ts

Lines changed: 70 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -2,97 +2,89 @@
22
* @param {number} capacity
33
*/
44

5-
var Node = function(key, val) {
6-
this.prev = null;
7-
this.next = null;
8-
this.val = val;
9-
this.key = key;
10-
};
5+
class LRUCache {
6+
capacity: number;
7+
count: number;
8+
head: ListNode;
9+
tail: ListNode;
10+
hash: { [key: string]: any };
1111

12-
var LRUCache = function(capacity) {
13-
this.capacity = capacity;
14-
this.count = 0;
15-
this.head = new Node(-1, -1);
16-
this.tail = new Node(-1, -1);
17-
this.head.next = this.tail;
18-
this.tail.prev = this.head;
19-
this.hash = {};
20-
};
21-
22-
/**
23-
* @param {number} key
24-
* @return {number}
25-
*/
26-
LRUCache.prototype.get = function(key) {
27-
if (this.hash[key]) {
28-
const currentNode = this.hash[key];
29-
const tempPrev = currentNode.prev;
30-
const tempNext = currentNode.next;
31-
const tempPrevTail = this.tail.prev;
32-
const tempPrevNext = this.tail.next;
33-
tempPrev.next = tempNext;
34-
tempNext.prev = tempPrev;
35-
this.tail.prev.next = currentNode;
36-
currentNode.prev = tempPrevTail;
37-
currentNode.next = this.tail;
38-
this.tail.prev = currentNode;
39-
return this.hash[key].value;
12+
constructor(capacity: number) {
13+
this.capacity = capacity;
14+
this.count = 0;
15+
this.head = new ListNode({ key: -1, value: -1 });
16+
this.tail = new ListNode({ key: -1, value: -1 });
17+
this.head.next = this.tail;
18+
this.tail.prev = this.head;
19+
this.hash = {};
4020
}
4121

42-
return -1;
43-
};
22+
get(key: number): number {
23+
if (this.hash[key]) {
24+
const currentNode = this.hash[key];
25+
const tempPrev = currentNode.prev;
26+
const tempNext = currentNode.next;
27+
const tempPrevTail = this.tail.prev;
28+
//const tempPrevNext = this.tail.next;
29+
tempPrev.next = tempNext;
30+
tempNext.prev = tempPrev;
31+
//this.tail.prev.next = currentNode;
32+
currentNode.prev = tempPrevTail;
33+
currentNode.next = this.tail;
34+
this.tail.prev = currentNode;
35+
return this.hash[key].value;
36+
}
4437

45-
/**
46-
* @param {number} key
47-
* @param {number} value
48-
* @return {void}
49-
*/
50-
LRUCache.prototype.put = function(key, value) {
51-
let node = new Node(key, value);
52-
if (this.hash[key]) {
53-
node = this.tail.prev;
54-
node.val = value;
55-
} else if (this.count === this.capacity) {
56-
const temp = this.head.next.next;
57-
delete this.hash[this.head.next.key];
58-
this.head.next = temp;
59-
temp.prev = this.head;
60-
} else {
61-
this.count++;
38+
return -1;
6239
}
63-
const temp = this.tail.prev;
64-
temp.next = node;
65-
node.prev = temp;
66-
node.next = this.tail;
67-
this.tail.prev = node;
68-
this.hash[key] = node;
69-
this.print();
70-
};
7140

72-
LRUCache.prototype.moveToHead = function() {};
41+
// put(key: number, value: number) {
42+
// let node = new ListNode({ key, value });
43+
// if (this.hash[key]) {
44+
// node = this.tail.prev;
45+
// node.val = value;
46+
// } else if (this.count === this.capacity) {
47+
// const temp = this.head.next.next;
48+
// delete this.hash[this.head.next.key];
49+
// this.head.next = temp;
50+
// temp.prev = this.head;
51+
// } else {
52+
// this.count++;
53+
// }
54+
// const temp = this.tail.prev;
55+
// temp.next = node;
56+
// node.prev = temp;
57+
// node.next = this.tail;
58+
// this.tail.prev = node;
59+
// this.hash[key] = node;
60+
// this.print();
61+
// }
62+
}
7363

74-
LRUCache.prototype.print = function(message = "") {
75-
let currentNode = this.head;
76-
console.log("---- init ---");
77-
while (currentNode !== null) {
78-
console.log(`${message}: [${currentNode.key}, ${currentNode.val}]`);
79-
currentNode = currentNode.next;
80-
}
81-
console.log("---- end ---");
82-
};
64+
// LRUCache.prototype.moveToHead = function () {};
65+
66+
// LRUCache.prototype.print = function (message = "") {
67+
// let currentNode = this.head;
68+
// console.log("---- init ---");
69+
// while (currentNode !== null) {
70+
// console.log(`${message}: [${currentNode.key}, ${currentNode.val}]`);
71+
// currentNode = currentNode.next;
72+
// }
73+
// console.log("---- end ---");
74+
// };
8375
/**
8476
* Your LRUCache object will be instantiated and called as such:
8577
* var obj = Object.create(LRUCache).createNew(capacity)
8678
* var param_1 = obj.get(key)
8779
* obj.put(key,value)
8880
*/
8981

90-
const cache = new LRUCache(2);
91-
cache.put(2, 1);
92-
cache.put(2, 2);
93-
cache.get(2);
94-
cache.put(4, 1);
95-
cache.put(1, 1);
96-
cache.get(2);
82+
// const cache = new LRUCache(2);
83+
// cache.put(2, 1);
84+
// cache.put(2, 2);
85+
// cache.get(2);
86+
// cache.put(4, 1);
87+
// cache.put(1, 1);
88+
// cache.get(2);
9789
// ["LRUCache","put","put","get","put","put","get"]
9890
// [[2],[2,1],[2,2],[2],[1,1],[4,1],[2]]
Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,36 @@
11
/**
22
* initialize your data structure here.
33
*/
4-
var MinStack = function() {
5-
this.stack = [];
6-
this.mins = [];
7-
};
4+
class MinStack {
5+
stack: number[];
6+
mins: number[];
87

9-
/**
10-
* @param {number} x
11-
* @return {void}
12-
*/
13-
MinStack.prototype.push = function(x) {
14-
this.stack.push(x);
15-
const min = this.getMin();
16-
if (min === undefined || x <= min) {
17-
this.mins.push(x);
8+
constructor() {
9+
this.stack = [];
10+
this.mins = [];
1811
}
19-
};
2012

21-
/**
22-
* @return {void}
23-
*/
24-
MinStack.prototype.pop = function() {
25-
const val = this.stack.pop();
26-
const min = this.getMin();
13+
push(x: number) {
14+
this.stack.push(x);
15+
const min = this.getMin();
16+
if (min === undefined || x <= min) {
17+
this.mins.push(x);
18+
}
19+
}
20+
pop() {
21+
const val = this.stack.pop();
22+
const min = this.getMin();
2723

28-
if (min !== undefined && val <= min) {
29-
this.mins.pop();
24+
if (min !== undefined && val !== undefined && val <= min) {
25+
this.mins.pop();
26+
}
3027
}
31-
};
3228

33-
/**
34-
* @return {number}
35-
*/
36-
MinStack.prototype.top = function() {
37-
return this.stack[this.stack.length - 1];
38-
};
29+
top() {
30+
return this.stack[this.stack.length - 1];
31+
}
3932

40-
/**
41-
* @return {number}
42-
*/
43-
MinStack.prototype.getMin = function() {
44-
return this.mins[this.mins.length - 1];
45-
};
33+
getMin() {
34+
return this.mins[this.mins.length - 1];
35+
}
36+
}

typescript/0198_house_robber/0198_house_robber.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
* @param {number[]} nums
33
* @return {number}
44
*/
5-
const rob = function(nums) {
5+
const rob = function (nums: number[]) {
66
let inclusive = 0;
77
let exclusive = 0;
88

9-
nums.forEach(current => {
9+
nums.forEach((current) => {
1010
let temp = inclusive;
1111
inclusive = Math.max(exclusive + current, inclusive);
1212
exclusive = temp;

typescript/0280_wiggle_sort/0280_wiggle_sort.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,15 @@
55
* Example:
66
* Input: nums = [3,5,2,1,6,4]
77
* Output: One possible answer is [3,5,1,6,2,4]
8-
*/
9-
10-
/**
11-
* @param {number[]} nums
12-
* @return {void} Do not return anything, modify nums in-place instead.
138
*/
149

15-
var swap = (nums, i) => {
10+
var swap = (nums: number[], i: number) => {
1611
nums[i] ^= nums[i + 1];
1712
nums[i + 1] ^= nums[i];
1813
nums[i] ^= nums[i + 1];
1914
};
2015

21-
var wiggleSort = function(nums) {
16+
var wiggleSort = function (nums: number[]) {
2217
for (let i = 0; i < nums.length - 1; i++) {
2318
if (i % 2 === 0 && nums[i] > nums[i + 1]) {
2419
swap(nums, i);

0 commit comments

Comments
 (0)