8000 Initial commit. · jonasraoni/leetcode@6a29940 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6a29940

Browse files
committed
Initial commit.
0 parents  commit 6a29940

13 files changed

+185
-0
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 Jonas Raoni Soares da Silva
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# LeetCode
2+
My solutions to exercises at https://leetcode.com.
3+
4+
These are my own implementations, everything should be running fast enough to pass through all tests and also be in the top execution (I try to optimize the memory usage, but my main ambition is speed). As I enjoy small and concise codes, whatever doesn't fit in this rule means that I've suffered xD

containsDuplicate.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//+ Jonas Raoni Soares Silva
2+
//@ http://raoni.org
3+
4+
var containsDuplicate = function (nums) {
5+
const set = new Set();
6+
for (const n of nums) {
7+
if (set.has(n))
8+
return true;
9+
set.add(n);
10+
}
11+
return false;
12+
};

intersect.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//+ Jonas Raoni Soares Silva
2+
//@ http://raoni.org
3+
4+
var intersect = function (nums1, nums2) {
5+
if (nums1.length > nums2.length)
6+
[nums1, nums2] = [nums2, nums1];
7+
const map = new Map();
8+
for (const n of nums1) {
9+
map.set(n, (map.get(n) || 0) + 1);
10+
}
11+
const 6D47 r = [];
12+
for (const n of nums2) {
13+
const count = map.get(n);
14+
if (count) {
15+
r.push(n);
16+
map.set(n, count - 1);
17+
}
18+
}
19+
return r;
20+
};

isValidSudoku.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//+ Jonas Raoni Soares Silva
2+
//@ http://raoni.org
3+
4+
var isValidSudoku = function (board) {
5+
const rows = new Array(9),
6+
boxes = new Array(9),
7+
columns = new Array(9);
8+
for (let i = 9; i--;) {
9+
rows[i] = new Set();
10+
boxes[i] = new Set();
11+
columns[i] = new Set();
12+
}
13+
for (let r = 9; r--;) {
14+
for (let c = 9; c--;) {
15+
const n = board[r][c];
16+
if (n === '.')
17+
continue;
18+
const column = columns[c],
19+
row = rows[r],
20+
box = boxes[Math.floor(r / 3) * 3 + Math.floor(c / 3)];
21+
if (row.has(n) || column.has(n) || box.has(n))
22+
return false;
23+
row.add(n);
24+
column.add(n);
25+
box.add(n);
26+
}
27+
}
28+
return true;
29+
};

leet.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
2+
3+
console.log(twoSum([2, 7, 11, 15], 9));
4+
5+
6+
7+
8+
console.log(isValidSudoku([
9+
["8", "3", ".", ".", "7", ".", ".", ".", "."],
10+
["6", ".", ".", "1", "9", "5", ".", ".", "."],
11+
[".", "9", "8", ".", ".", ".", ".", "6", "."],
12+
["8", ".", ".", ".", "6", ".", ".", ".", "3"],
13+
["4", ".", ".", "8", ".", "3", ".", ".", "1"],
14+
["7", ".", ".", ".", "2", ".", ".", ".", "6"],
15+
[".", "6", ".", ".", ".", ".", "2", "8", "."],
16+
[".", ".", ".", "4", "1", "9", ".", ".", "5"],
17+
[".", ".", ".", ".", "8", ".", ".", "7", "9"]
18+
]));

maxProfit.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//+ Jonas Raoni Soares Silva
2+
//@ http://raoni.org
3+
4+
var maxProfit = function (prices) {
5+
let total = 0;
6+
for (let i = prices.length; --i;) {
7+
if (prices[i] >= prices[i - 1])
8+
total += prices[i] - prices[i - 1];
9+
}
10+
return total;
11+
};

moveZeroes.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//+ Jonas Raoni Soares Silva
2+
//@ http://raoni.org
3+
4+
var moveZeroes = function (nums) {
5+
let zeroes = 0;
6+
for (let i = -1, l = nums.length; ++i < l;) {
7+
if (!nums[i])
8+
++zeroes;
9+
else if (zeroes) {
10+
nums[i - zeroes] = nums[i];
11+
nums[i] = 0;
12+
}
13+
}
14+
return nums;
15+
};

plusOne.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//+ Jonas Raoni Soares Silva
2+
//@ http://raoni.org
3+
4+
var plusOne = function (digits) {
5+
for (let i = digits.length; i--;) {
6+
if (++digits[i] < 10)
7+
return digits;
8+
digits[i] = 0;
9+
}
10+
digits.unshift(1);
11+
return digits;
12+
};

removeDuplicates.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//+ Jonas Raoni Soares Silva
2+
//@ http://raoni.org
3+
4+
var removeDuplicates = function (nums) {
5+
for (let i = nums.length; --i >= 0;) {
6+
if (nums[i] === nums[i - 1]) {
7+
nums.splice(i, 1);
8+
}
9+
}
10+
return nums.length;
11+
};

0 commit comments

Comments
 (0)
0