Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1fc5e95

Browse files
committed
chapter 4: stacks
1 parent 6592091 commit 1fc5e95

14 files changed

+686
-1
lines changed

‎src/04-stack/01-using-stack-class.js‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// src/04-stack/01-using-stack-class.js
2+
3+
const Stack = require('./stack');
4+
// import { Stack } from './stack'; // or './stack.js' if you are using ES modules
5+
6+
const stack = new Stack();
7+
8+
console.log(stack.isEmpty()); // true
9+
10+
stack.push({action: 'typing', text: 'S'});
11+
stack.push({action: 'typing', text: 't'});
12+
13+
console.log(stack.peek()); // { action: 'typing', text: 't' }
14+
15+
console.log(stack.size); // 2
16+
17+
stack.push({action: 'typing', text: 'a'});
18+
stack.push({action: 'typing', text: 'c'});
19+
stack.push({action: 'typing', text: 'k'});
20+
21+
console.log(stack.size); // 5
22+
console.log(stack.isEmpty()); // false
23+
24+
// removing two elements from the stack
25+
stack.pop();
26+
stack.pop();
27+
28+
console.log(stack.size); // 3
29+
console.log(stack.peek()); // { action: 'typing', text: 'a' }
30+
31+
// toString
32+
console.log(stack.toString());
33+
34+
// to see the output of this file use the command: node src/04-stack/01-using-stack-class.js

‎src/04-stack/01-using-stack-class.ts‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// src/04-stack/01-using-stack-class.ts
2+
3+
import Stack from './stack';
4+
5+
enum Action {
6+
TYPE = 'typing'
7+
}
8+
9+
interface EditorAction {
10+
action: Action;
11+
text: string;
12+
}
13+
14+
const stack = new Stack<EditorAction>();
15+
16+
console.log(stack.isEmpty()); // true
17+
18+
console.log(stack.peek()); // undefined
19+
20+
stack.push({action: Action.TYPE, text: 'S'});
21+
stack.push({action: Action.TYPE, text: 't'});
22+
23+
console.log(stack.peek()); // { action: 'typing', text: 't' }
24+
25+
console.log(stack.size); // 2
26+
27+
stack.push({action: Action.TYPE, text: 'a'});
28+
stack.push({action: Action.TYPE, text: 'c'});
29+
stack.push({action: Action.TYPE, text: 'k'});
30+
31+
console.log(stack.size); // 5
32+
console.log(stack.isEmpty()); // false
33+
34+
// removing two elements from the stack
35+
stack.pop();
36+
stack.pop();
37+
38+
console.log(stack.size); // 3
39+
console.log(stack.peek()); // { action: 'typing', text: 'a' }
40+
41+
// toString
42+
console.log(stack.toString());
43+
44+
// to see the output of this file use the command: npx ts-node src/04-stack/01-using-stack-class.ts
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// src/04-stack/02-using-stack-object-class.js
2+
3+
const Stack = require('./stack-object');
4+
// import { Stack } from './stack-object'; // or './stack-object.js' if you are using ES modules
5+
6+
const stack = new Stack();
7+
8+
console.log(stack.isEmpty()); // true
9+
10+
console.log(stack.peek()); // undefined
11+
12+
stack.push({action: 'typing', text: 'S'});
13+
stack.push({action: 'typing', text: 't'});
14+
15+
// internal object representation
16+
// #items = {
17+
// 0: { action: 'typing', text: 'S' },
18+
// 1: { action: 'typing', text: 't' }}
19+
// };
20+
// #count = 2;
21+
22+
console.log(stack.peek()); // { action: 'typing', text: 't' }
23+
24+
console.log(stack.size); // 2
25+
26+
stack.push({action: 'typing', text: 'a'});
27+
stack.push({action: 'typing', text: 'c'});
28+
stack.push({action: 'typing', text: 'k'});
29+
30+
console.log(stack.size); // 5
31+
console.log(stack.isEmpty()); // false
32+
33+
// removing two elements from the stack
34+
stack.pop();
35+
stack.pop();
36+
37+
console.log(stack.size); // 3
38+
console.log(stack.peek()); // { action: 'typing', text: 'a' }
39+
40+
// toString
41+
console.log(stack);
42+
43+
44+
// to see the output of this file use the command: node src/04-stack/02-using-stack-object-class.js
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// src/04-stack/02-using-stack-object-class.ts
2+
3+
import Stack from './stack-object';
4+
5+
interface EditorAction {
6+
action: string;
7+
text: string;
8+
}
9+
10+
const stack = new Stack<EditorAction>();
11+
12+
console.log(stack.isEmpty()); // true
13+
14+
console.log(stack.peek()); // undefined
15+
16+
stack.push({action: 'typing', text: 'S'});
17+
stack.push({action: 'typing', text: 't'});
18+
19+
// internal object representation
20+
// #items = {
21+
// 0: { action: 'typing', text: 'S' },
22+
// 1: { action: 'typing', text: 't' }}
23+
// };
24+
// #count = 2;
25+
26+
console.log(stack.peek()); // { action: 'typing', text: 't' }
27+
28+
console.log(stack.size); // 2
29+
30+
stack.push({action: 'typing', text: 'a'});
31+
stack.push({action: 'typing', text: 'c'});
32+
stack.push({action: 'typing', text: 'k'});
33+
34+
console.log(stack.size); // 5
35+
console.log(stack.isEmpty()); // false
36+
37+
// removing two elements from the stack
38+
stack.pop();
39+
stack.pop();
40+
41+
console.log(stack.size); // 3
42+
console.log(stack.peek()); // { action: 'typing', text: 'a' }
43+
44+
// toString
45+
console.log(stack);
46+
47+
48+
// to see the output of this file use the command: npx ts-node src/04-stack/02-using-stack-object-class.js
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// src/04-stack/03-base-converter-examples.js
2+
3+
const decimalToBinary = require('./decimal-to-binary');
4+
const converters = require('./decimal-to-base');
5+
6+
// Decimal to binary
7+
console.log(decimalToBinary(0)); // 0
8+
console.log(decimalToBinary(1)); // 1
9+
console.log(decimalToBinary(2)); // 10
10+
console.log(decimalToBinary(13)); // 1101
11+
console.log(decimalToBinary(233)); // 11101001
12+
console.log(decimalToBinary(10)); // 1010
13+
console.log(decimalToBinary(1000)); // 1111101000
14+
15+
// Decimal to base
16+
console.log(converters.decimalToBase(100345, 2)); // 11000011111111001
17+
console.log(converters.decimalToBase(100345, 8)); // 303771
18+
console.log(converters.decimalToBase(100345, 16)); // 187F9
19+
console.log(converters.decimalToBase(100345, 35)); // 2BW0
20+
21+
// Decimal to base 64
22+
console.log(converters.decimalToBase64(100345, 64)); // Yf5=
23+
24+
// to see the output of this file use the command: node src/04-stack/03-base-converter-examples.js

‎src/04-stack/decimal-to-base.js‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// src/04-stack/decimal-to-base.js
2+
3+
const Stack = require('./stack');
4+
5+
/**
6+
* Converts a decimal number to a given base
7+
* @param {number} decimalNumber - decimal number to be converted
8+
* @param {number} base - base to convert the decimal number to
9+
* @returns {string} base representation of the decimal number
10+
*/
11+
function decimalToBase(decimalNumber, base) {
12+
13+
if (base < 2 || base > 36) {
14+
throw new Error('Base must be between 2 and 36');
15+
}
16+
17+
const digits = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ'; // Digits for base 36
18+
const remainderStack = new Stack();
19+
let baseString = '';
20+
21+
while (decimalNumber > 0) {
22+
const remainder = Math.floor(decimalNumber % base);
23+
remainderStack.push(remainder);
24+
decimalNumber = Math.floor(decimalNumber / base);
25+
}
26+
27+
while (!remainderStack.isEmpty()) {
28+
baseString += digits[remainderStack.pop()]; // Use digit mapping
29+
}
30+
31+
return baseString;
32+
}
33+
34+
/**
35+
* Converts a decimal number to a given base
36+
* @param {number} decimalNumber - decimal number to be converted (between 2 and 36 or 64)
37+
* @param {number} base - base to convert the decimal number to
38+
* @returns {string} base representation of the decimal number
39+
*/
40+
function decimalToBase64(decimalNumber, base) {
41+
if (base < 2 || (base > 36 && base !== 64)) {
42+
throw new Error('Base must be between 2 and 36 or 64');
43+
}
44+
45+
const digits = base === 64 ?
46+
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' :
47+
'0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
48+
49+
const remainderStack = new Stack();
50+
let baseString = '';
51+
52+
while (decimalNumber > 0) {
53+
const remainder = Math.floor(decimalNumber % base);
54+
remainderStack.push(remainder);
55+
decimalNumber = Math.floor(decimalNumber / base);
56+
}
57+
58+
while (!remainderStack.isEmpty()) {
59+
baseString += digits[remainderStack.pop()];
60+
}
61+
62+
// Handle padding for Base64 (if necessary)
63+
/* Base64 encodes data in groups of 3 bytes (24 bits).
64+
Each group is represented by four Base64 characters (6 bits per character).
65+
If the input data length isn't divisible by 3, padding characters (=) are added to the end of the output
66+
to ensure that the total length is a multiple of 4.
67+
This is important for proper decoding of the Base64 string back to its original data. */
68+
if (base === 64) {
69+
while (baseString.length % 4 !== 0) {
70+
baseString += '=';
71+
}
72+
}
73+
74+
return baseString;
75+
}
76+
77+
module.exports = { decimalToBase, decimalToBase64 };

‎src/04-stack/decimal-to-binary.js‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// src/04-stack/decimal-to-binary.js
2+
3+
const Stack = require('./stack');
4+
5+
/**
6+
* Converts a decimal number to binary
7+
* @param {number} decimalNumber - decimal number to be converted
8+
* @returns {string} binary representation of the decimal number
9+
*/
10+
function decimalToBinary(decimalNumber) {
11+
const remainderStack = new Stack();
12+
let binaryString = '';
13+
14+
if (decimalNumber === 0) {
15+
return '0';
16+
}
17+
18+
while (decimalNumber > 0) { // {1}
19+
const remainder = Math.floor(decimalNumber % 2); // {2}
20+
remainderStack.push(remainder); // {3}
21+
decimalNumber = Math.floor(decimalNumber / 2); // {4}
22+
}
23+
24+
while (!remainderStack.isEmpty()) { // {5}
25+
binaryString += remainderStack.pop().toString();
26+
}
27+
28+
return binaryString;
29+
}
30+
31+
module.exports = decimalToBinary;

‎src/04-stack/leetcode/min-stack.ts‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,62 @@ console.log(minStack.getMin()); // -2
4646
// time complexity: O(1)
4747
// space complexity: O(n)
4848

49+
// optimized solution
50+
class MinStack2 {
51+
stack: number[] = [];
52+
min: number = +Infinity;
53+
54+
push(val: number): void {
55+
this.min = Math.min(val, this.min);
56+
this.stack.push(val);
57+
}
58+
59+
pop(): void {
60+
const val = this.stack.pop();
61+
if (this.min === val) this.min = Math.min(...this.stack);
62+
}
63+
64+
top(): number {
65+
return this.stack[this.stack.length - 1];
66+
}
67+
68+
getMin(): number {
69+
return this.min;
70+
}
71+
}
72+
73+
class MinStack3 {
74+
private stack: number[];
75+
private minStack: number[];
76+
77+
constructor() {
78+
this.stack = [];
79+
this.minStack = [];
80+
}
81+
82+
push(val: number): void {
83+
this.stack.push(val);
84+
85+
if (this.minStack.length === 0) this.minStack.push(val);
86+
else {
87+
const currentMin = this.minStack[this.minStack.length - 1];
88+
89+
this.minStack.push(val < currentMin ? val : currentMin);
90+
}
91+
}
92+
93+
pop(): void {
94+
this.stack.pop();
95+
this.minStack.pop();
96+
}
97+
98+
top(): number {
99+
return this.stack[this.stack.length - 1];
100+
}
101+
102+
getMin(): number {
103+
return this.minStack[this.minStack.length - 1];
104+
}
105+
}
106+
49107
// to see the output of this file use the command: node src/04-stack/leetcode/min-stack.ts

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /