Skip to content

Commit 7c13e2c

Browse files
committed
https://leetcode.cn/problems/design-a-stack-with-increment-operation/
1 parent 0e8c66d commit 7c13e2c

File tree

5 files changed

+64
-23
lines changed

5 files changed

+64
-23
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ leetcode 测试
1010

1111
##### 包含的内容如下
1212

13+
https://leetcode.cn/problems/design-a-stack-with-increment-operation/
14+
1315
https://leetcode.cn/problems/ugly-number/
1416

1517
https://leetcode.cn/problems/get-kth-magic-number-lcci/
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
export default class CustomStack {
2+
#stack = Array<[number, number]>();
3+
constructor(public capacity: number) {}
4+
5+
push(x: number): void {
6+
if (this.#stack.length >= this.capacity) {
7+
return;
8+
}
9+
this.#stack.push([x, 0]);
10+
}
11+
12+
pop(): number {
13+
if (this.#stack.length === 0) return -1;
14+
const last = this.#stack[this.#stack.length - 1];
15+
this.#stack.pop();
16+
if (this.#stack.length) {
17+
this.increment(this.#stack.length, last[1]);
18+
}
19+
return last[0] + last[1];
20+
}
21+
22+
increment(k: number, val: number): void {
23+
if (this.#stack.length === 0) return;
24+
// console.log(this,k,val)
25+
if (this.#stack.length < k) {
26+
k = this.#stack.length;
27+
}
28+
// console.log(this,k,val)
29+
this.#stack[k - 1][1] += val;
30+
}
31+
}

design-circular-deque/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ export default interface MyCircularDeque<T = any> {
88
deleteLast: () => boolean;
99
getRear: () => T | number;
1010
isFull: () => boolean;
11-
capcity: number;
11+
capacity: number;
1212
}
1313

1414
// deno-lint-ignore no-explicit-any
1515
export default function MyCircularDeque<T = any>(
16-
capcity = Infinity,
16+
capacity = Infinity,
1717
): MyCircularDeque<T> {
1818
// console.log('MyCircularDeque', k)
19-
if (capcity < 1) throw Error("k greater than or equal one");
19+
if (capacity < 1) throw Error("k greater than or equal one");
2020
const storage = new Map<bigint, T>();
2121
let min = BigInt(0);
2222
let max = BigInt(0);
@@ -113,7 +113,7 @@ export default function MyCircularDeque<T = any>(
113113
return r as T;
114114
}
115115
function isFull(): boolean {
116-
return storage.size >= capcity;
116+
return storage.size >= capacity;
117117
}
118118
return {
119119
isEmpty,
@@ -124,6 +124,6 @@ export default function MyCircularDeque<T = any>(
124124
deleteLast,
125125
getRear,
126126
isFull,
127-
capcity: capcity,
127+
capacity: capacity,
128128
};
129129
}

design-circular-queue/index.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ type MyCircularQueue<T = any> = {
77
enQueue: (value: T) => boolean;
88
Rear: () => T | number;
99
isFull: () => boolean;
10-
capcity: number;
10+
capacity: number;
1111
};
1212
// deno-lint-ignore no-explicit-any
1313
function MyCircularQueue<T = any>(
1414
// deno-lint-ignore no-inferrable-types
15-
capcity: number = Infinity,
15+
capacity: number = Infinity,
1616
): MyCircularQueue<T> {
1717
// console.log('MyCircularDeque', k)
18-
if (capcity < 1) {
18+
if (capacity < 1) {
1919
throw Error("k greater than or equal one");
2020
}
2121
const storage = new Map<bigint, T>();
@@ -123,7 +123,15 @@ function MyCircularQueue<T = any>(
123123
return r as T;
124124
}
125125
function isFull(): boolean {
126-
return storage.size >= capcity;
126+
return storage.size >= capacity;
127127
}
128-
return { isEmpty, Front, deQueue, enQueue, Rear, isFull, capcity: capcity };
128+
return {
129+
isEmpty,
130+
Front,
131+
deQueue,
132+
enQueue,
133+
Rear,
134+
isFull,
135+
capacity: capacity,
136+
};
129137
}

ugly-number/index.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
function isUgly(n: number): boolean {
2-
if (n <= 0) {
3-
return false;
4-
}
5-
6-
for (const factor of [2, 3, 5]) {
7-
while (n % factor === 0) {
8-
n /= factor;
9-
}
10-
}
11-
return n == 1;
12-
}
13-
export default isUgly;
1+
function isUgly(n: number): boolean {
2+
if (n <= 0) {
3+
return false;
4+
}
5+
6+
for (const factor of [2, 3, 5]) {
7+
while (n % factor === 0) {
8+
n /= factor;
9+
}
10+
}
11+
return n == 1;
12+
}
13+
export default isUgly;

0 commit comments

Comments
 (0)