Skip to content

Commit 042e3da

Browse files
committed
merge javascript leetcode exercises into the repository
1 parent b4330d9 commit 042e3da

10 files changed

Lines changed: 372 additions & 0 deletions

javascript/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Learning Javascript
2+
3+
A collection of leetcode solutions in JavaScript.
4+
The emphasis is NOT on efficient solutions, but on learning the basics of JavaScript.
5+
The code was run locally using nodejs.
6+
7+
## Getting started
8+
9+
System dependencies:
10+
```bash
11+
sudo apt install nodejs npm
12+
```
13+
14+
```bash
15+
git clone ... # this repo
16+
cd leetcode_js
17+
npm run hello # run the hello_world example
18+
npm test # execute unit tests
19+
```
20+
21+
## Learning resources
22+
- More info on [unit testing in nodejs](https://nodejs.org/api/test.html)
23+
- [W3 schools JavaScript tutorial](https://www.w3schools.com/js/default.asp)
24+
- [W3 schools JavaScript language reference](https://www.w3schools.com/jsref/default.asp)

javascript/package-lock.json

Lines changed: 118 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

javascript/package.json

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"name": "leetcode_javascript",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "src/hello_world.js",
6+
"scripts": {
7+
"hello": "node src/hello_world.js",
8+
"test": "node --test"
9+
},
10+
"repository": {
11+
"type": "git",
12+
"url": "git+https://github.com/mnikander/leetcode_javascript.git"
13+
},
14+
"keywords": [],
15+
"author": "",
16+
"license": "ISC",
17+
"bugs": {
18+
"url": "https://github.com/mnikander/leetcode_javascript/issues"
19+
},
20+
"homepage": "https://github.com/mnikander/leetcode_javascript#readme",
21+
"dependencies": {
22+
"node-assert": "^0.0.1",
23+
"node-test": "^1.4.6"
24+
}
25+
}

javascript/src/0001-two-sum.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {number[]} nums
3+
* @param {number} target
4+
* @return {number[]}
5+
*/
6+
var twoSum = function(nums, target) {
7+
for(let i = 0; i < nums.length; i++) {
8+
for (let j = i+1; j < nums.length; j++) {
9+
let sum = nums[i] + nums[j];
10+
if (sum == target) {
11+
return [i, j];
12+
}
13+
}
14+
}
15+
};
16+
17+
let r1 = twoSum([2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41], 42);
18+
console.log(r1[0] + " " + r1[1]);
19+
20+
let r2 = twoSum([2, 1, 11, 7, 5, 3], 4)
21+
console.log(r2[0] + " " + r2[1]);

javascript/src/0009-palindrome.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/**
2+
* @param {number} x
3+
* @return {boolean}
4+
*/
5+
var isPalindrome = function(x) {
6+
let str = "" + x;
7+
for (let i = 0; i < str.length / 2; i++) {
8+
if (str[i] != str[str.length - i - 1]) {
9+
return false;
10+
}
11+
}
12+
return true;
13+
};
14+
15+
console.log(isPalindrome(22));
16+
console.log(isPalindrome(242));
17+
console.log(isPalindrome(13));
18+
console.log(isPalindrome(133));
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @param {string} s
3+
* @return {number}
4+
*/
5+
var romanToInt = function(s) {
6+
let unigrams = {M: 1000, D: 500, C: 100, L: 50, X: 10, V: 5, I: 1};
7+
let bigrams = {CM: 900, CD: 400, XC: 90, XL: 40, IX: 9, IV: 4};
8+
9+
let sum = 0;
10+
let i = 0;
11+
12+
while (i < s.length) {
13+
let oneChar = s[i];
14+
let twoChars = "";
15+
if (i < s.length - 1) {
16+
twoChars = s[i] + s[i+1];
17+
}
18+
19+
if (bigrams[twoChars] != undefined) {
20+
sum = sum + bigrams[twoChars];
21+
i = i + 2;
22+
}
23+
else {
24+
sum = sum + unigrams[oneChar];
25+
i = i + 1;
26+
}
27+
}
28+
29+
// console.log(s + " = " + sum);
30+
return sum;
31+
};
32+
33+
module.exports = { romanToInt };
34+
35+
// Notes:
36+
// - since the input consists of valid roman numerals, we don't need to worry about things like VV == X or LL == C
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
isMatch = (leftChar, rightChar) => {
2+
return (leftChar == '(' && rightChar == ')')
3+
|| (leftChar == '[' && rightChar == ']')
4+
|| (leftChar == '{' && rightChar == '}');
5+
}
6+
7+
isOpening = (parenthesis) => {
8+
return parenthesis == '(' || parenthesis == '[' || parenthesis == '{';
9+
}
10+
11+
/**
12+
* @param {string} s
13+
* @return {boolean}
14+
*/
15+
var isValid = function(s) {
16+
const stack = [];
17+
18+
for (let i = 0; i < s.length; i++)
19+
{
20+
let parenthesis = s.charAt(i);
21+
if(isOpening(parenthesis)) {
22+
stack.push(parenthesis);
23+
}
24+
else if (stack.length == 0) {
25+
return false;
26+
}
27+
else {
28+
let predecessor = stack.pop();
29+
if(!isMatch(predecessor, parenthesis)) {
30+
return false;
31+
}
32+
}
33+
}
34+
return stack.length == 0;
35+
};
36+
37+
module.exports = { isValid };

javascript/src/hello_world.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log('Hello, Node.js');
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
const test = require('node:test');
2+
const assert = require('node:assert');
3+
const { romanToInt } = require('../src/0013-roman-to-integer')
4+
5+
test('roman to integer, 1-20', (t) => {
6+
assert.strictEqual(romanToInt("I") , 1);
7+
assert.strictEqual(romanToInt("II") , 2);
8+
assert.strictEqual(romanToInt("III") , 3);
9+
assert.strictEqual(romanToInt("IV") , 4);
10+
assert.strictEqual(romanToInt("V") , 5);
11+
assert.strictEqual(romanToInt("VI") , 6);
12+
assert.strictEqual(romanToInt("VII") , 7);
13+
assert.strictEqual(romanToInt("VIII") , 8);
14+
assert.strictEqual(romanToInt("IX") , 9);
15+
assert.strictEqual(romanToInt("X") , 10);
16+
assert.strictEqual(romanToInt("XI") , 11);
17+
assert.strictEqual(romanToInt("XII") , 12);
18+
assert.strictEqual(romanToInt("XIII") , 13);
19+
assert.strictEqual(romanToInt("XIV") , 14);
20+
assert.strictEqual(romanToInt("XV") , 15);
21+
assert.strictEqual(romanToInt("XVI") , 16);
22+
assert.strictEqual(romanToInt("XVII") , 17);
23+
assert.strictEqual(romanToInt("XVIII"), 18);
24+
assert.strictEqual(romanToInt("XIX") , 19);
25+
assert.strictEqual(romanToInt("XX") , 20);
26+
});
27+
28+
test('roman to integer, unigrams', (t) => {
29+
assert.strictEqual(romanToInt("I"), 1);
30+
assert.strictEqual(romanToInt("V"), 5); // no repitition or prefixing of V is allowed
31+
assert.strictEqual(romanToInt("X"), 10);
32+
assert.strictEqual(romanToInt("L"), 50); // no repitition or prefixing of L is allowed
33+
assert.strictEqual(romanToInt("C"), 100);
34+
assert.strictEqual(romanToInt("D"), 500); // no repitition or prefixing of D is allowed
35+
assert.strictEqual(romanToInt("M"), 1000);
36+
});
37+
38+
test('roman to integer, bigrams', (t) => {
39+
assert.strictEqual(romanToInt("IV"), 4);
40+
assert.strictEqual(romanToInt("IX"), 9);
41+
assert.strictEqual(romanToInt("XL"), 40);
42+
assert.strictEqual(romanToInt("XC"), 90);
43+
assert.strictEqual(romanToInt("CD"), 400);
44+
assert.strictEqual(romanToInt("CM"), 900);
45+
});
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
const test = require('node:test');
2+
const assert = require('node:assert');
3+
const { isValid } = require('../src/0020-valid-parentheses')
4+
5+
test('valid parenthesis, empty', (t) => {
6+
assert.strictEqual(isValid(""), true);
7+
});
8+
9+
test('valid parenthesis, simple parenthesis', (t) => {
10+
assert.strictEqual(isValid("()"), true);
11+
});
12+
13+
test('valid parenthesis, each kind', (t) => {
14+
assert.strictEqual(isValid("()[]{}"), true);
15+
});
16+
17+
test('valid parenthesis, nested', (t) => {
18+
assert.strictEqual(isValid("([])"), true);
19+
});
20+
21+
test('valid parenthesis, deeply nested', (t) => {
22+
assert.strictEqual(isValid("([](){()})"), true);
23+
});
24+
25+
test('valid parenthesis, simple unclosed', (t) => {
26+
assert.strictEqual(isValid("("), false);
27+
});
28+
29+
test('valid parenthesis, nested unclosed', (t) => {
30+
assert.strictEqual(isValid("{(}"), false);
31+
});
32+
33+
test('valid parenthesis, mismatch', (t) => {
34+
assert.strictEqual(isValid("(]"), false);
35+
});
36+
37+
test('valid parenthesis, nested mismatch', (t) => {
38+
assert.strictEqual(isValid("([](])"), false);
39+
});
40+
41+
test('valid parenthesis, reversed order', (t) => {
42+
assert.strictEqual(isValid(")("), false);
43+
});
44+
45+
test('valid parenthesis, interleaved order', (t) => {
46+
assert.strictEqual(isValid("([)]"), false);
47+
});

0 commit comments

Comments
 (0)