Skip to content

Commit 1324d9d

Browse files
some more problems
1 parent a954789 commit 1324d9d

File tree

10 files changed

+342
-17
lines changed

10 files changed

+342
-17
lines changed

carousel/carousel.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
```html
2+
<div class="carousel slide" id="myCarousel" data-ride="carousel">
3+
<ol class="carousel-indicators">
4+
<li class="carousel-indicator active" data-slide-to="0" data-target="#myCarousel"></li>
5+
<li class="carousel-indicator" data-slide-to="1" data-target="#myCarousel"></li>
6+
<li class="carousel-indicator" data-slide-to="2" data-target="#myCarousel"></li>
7+
</ol>
8+
9+
<div class="carousel-images-wrapper">
10+
<div class="item active">
11+
<img src="sweden.jpg" alt="View of Stockholm Sweden"/>
12+
</div>
13+
<div class="item">
14+
<img src="norway.jpg" alt="Norway image"/>
15+
</div>
16+
<div class="item">
17+
<img src="denmark.jpg" alt="Denmark image"/>
18+
</div>
19+
</div>
20+
21+
<a class="carousel-control" href="#myCarousel" data-slide="prev">
22+
<span class="icon-left"></span>
23+
<span class="control-text">Previous</span>
24+
</a>
25+
<a class="carousel-control" href="#myCarousel" data-slide="next">
26+
<span class="icon-right"></span>
27+
<span class="control-text">Next</span>
28+
</a>
29+
</div>
30+
```

exercises/leetcode/groupAnagrams.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
* Given an array of strings, group anagrams together.
3+
4+
Example:
5+
6+
Input: ["eat", "tea", "tan", "ate", "nat", "bat"],
7+
Output:
8+
[
9+
["ate","eat","tea"],
10+
["nat","tan"],
11+
["bat"]
12+
]
13+
Note:
14+
15+
All inputs will be in lowercase.
16+
The order of your output does not matter.
17+
*/
18+
19+
20+
const groupAnagrams = strs => {
21+
// create a map with key as sorted char and value as the array of anagram word sets
22+
const anagramMap = new Map();
23+
24+
for (let s of strs) {
25+
let sortedChar = s.split('').sort().join('');
26+
27+
if (!anagramMap.has(sortedChar)) {
28+
anagramMap.set(sortedChar, []);
29+
}
30+
31+
anagramMap.get(sortedChar).push(s);
32+
}
33+
34+
return Array.from(anagramMap.values());
35+
}

exercises/leetcode/setZeroes.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/**
2+
* Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in-place.
3+
4+
Example 1:
5+
6+
Input:
7+
[
8+
[1,1,1],
9+
[1,0,1],
10+
[1,1,1]
11+
]
12+
Output:
13+
[
14+
[1,0,1],
15+
[0,0,0],
16+
[1,0,1]
17+
]
18+
Example 2:
19+
20+
Input:
21+
[
22+
[0,1,2,0],
23+
[3,4,5,2],
24+
[1,3,1,5]
25+
]
26+
Output:
27+
[
28+
[0,0,0,0],
29+
[0,4,5,0],
30+
[0,3,1,0]
31+
]
32+
Follow up:
33+
34+
A straight forward solution using O(mn) space is probably a bad idea.
35+
A simple improvement uses O(m + n) space, but still not the best solution.
36+
Could you devise a constant space solution?
37+
* @param {*} matrix
38+
*/
39+
40+
const setZeroes = matrix => {
41+
let isCol = false;
42+
const rows = matrix.length;
43+
const columns = matrix[0].length;
44+
45+
for (let i = 0; i < rows; i++) {
46+
if (matrix[i][0] === 0) {
47+
isCol = true;
48+
}
49+
50+
for (let j = 1; j < columns; j++) {
51+
if (matrix[i][j] === 0) {
52+
matrix[i][0] = 0;
53+
matrix[0][j] = 0;
54+
}
55+
}
56+
}
57+
58+
for (let i = 0; i < rows; i++) {
59+
for (let j = 0; j < columns; j++) {
60+
if (matrix[i][0] === 0 || matrix[0][j] === 0) {
61+
matrix[i][j] = 0;
62+
}
63+
}
64+
}
65+
66+
if (matrix[0][0] === 0) {
67+
for (let i = 0; i < rows; i++) {
68+
matrix[i][0] = 0;
69+
}
70+
}
71+
72+
if (isCol) {
73+
for (let j = 0; j < columns; j++) {
74+
matrix[0][j] = 0;
75+
}
76+
}
77+
}

exercises/leetcode/spiralMatrix.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/**
2+
* Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.
3+
4+
Example 1:
5+
6+
Input:
7+
[
8+
[ 1, 2, 3 ],
9+
[ 4, 5, 6 ],
10+
[ 7, 8, 9 ]
11+
]
12+
Output: [1,2,3,6,9,8,7,4,5]
13+
Example 2:
14+
15+
Input:
16+
[
17+
[1, 2, 3, 4],
18+
[5, 6, 7, 8],
19+
[9,10,11,12]
20+
]
21+
Output: [1,2,3,4,8,12,11,10,9,5,6,7]
22+
* @param {*} matrix
23+
*/
24+
const spiralMatrix = matrix => {
25+
const result = [];
26+
27+
if (matrix.length === 0) {
28+
return [];
29+
}
30+
31+
let startRow = 0;
32+
let endRow = matrix.length - 1;
33+
let startColumn = 0;
34+
let endColumn = matrix[0].length - 1;
35+
36+
37+
while (startRow <= endRow && startColumn <= endColumn) {
38+
for (let i = startColumn; i <= endColumn; i++) {
39+
result.push(matrix[startRow][i]);
40+
}
41+
42+
for (let i = startRow + 1; i <= endRow; i++) {
43+
result.push(matrix[i][endColumn]);
44+
}
45+
46+
if (startRow < endRow && startColumn < endColumn) {
47+
for (let i = endColumn - 1; i > startColumn; i--) {
48+
result.push(matrix[endRow][i]);
49+
}
50+
51+
for (let i = endRow; i > startRow; i--) {
52+
result.push(matrix[i][startColumn])
53+
}
54+
}
55+
startRow++;
56+
endRow--;
57+
startColumn++;
58+
endColumn--;
59+
}
60+
61+
return result;
62+
}
63+
64+
/**
65+
* startRow = 0
66+
* endColumn = 3
67+
* endRow = 2
68+
* startColumn = 0
69+
*/
70+
71+
/**
72+
* [
73+
[1, 2, 3, 4],
74+
[5, 6, 7, 8],
75+
[9,10,11,12]
76+
]
77+
* [
78+
[ 1, 2, 3 ],
79+
[ 4, 5, 6 ],
80+
[ 7, 8, 9 ]
81+
]
82+
startRow = 0 => 1 => 2
83+
endRow = 2 => 1 => 0
84+
startColumn = 0 => 1
85+
endColumn = 2 => 1
86+
*/

exercises/leetcode/trapRainWater.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* Given n non-negative integers representing an elevation map where the width of each bar is 1,
3+
* compute how much water it is able to trap after raining.
4+
*/
5+
6+
const maxTrap = height => {
7+
let leftMax = [];
8+
let rightMax = [];
9+
let ans = 0;
10+
11+
if (!height || height.length === 0) {
12+
return 0;
13+
}
14+
15+
leftMax[0] = height[0];
16+
for (let i = 1; i < height.length - 1; i++) {
17+
leftMax[i] = Math.max(height[i], leftMax[i - 1]);
18+
}
19+
20+
rightMax[height.length - 1] = height[height.length - 1];
21+
for (let i = height.length - 2; i >=0; i--) {
22+
rightMax[i] = Math.max(height[i], rightMax[i + 1]);
23+
}
24+
25+
for (let i = 0; i < height.length - 1; i++) {
26+
ans += Math.min(rightMax[i], leftMax[i]) - height[i];
27+
}
28+
29+
return ans;
30+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
3+
4+
An input string is valid if:
5+
6+
Open brackets must be closed by the same type of brackets.
7+
Open brackets must be closed in the correct order.
8+
Note that an empty string is also considered valid.
9+
*/
10+
11+
const validParenthesis = str => {
12+
const bracketMap = new Map();
13+
14+
bracketMap.set(')', '(');
15+
bracketMap.set('}', '{');
16+
bracketMap.set(']', '[');
17+
18+
const strStack = [];
19+
20+
for (let ch of str.split('')) {
21+
// check if the char is a closing bracket
22+
if (bracketMap.has(ch)) {
23+
// find the top element from the stack
24+
let top = strStack.length === 0 ? '#' : strStack.pop();
25+
26+
// if top element is not an opening bracket, expression is invalid
27+
if (top !== bracketMap.get(ch)) {
28+
return false;
29+
}
30+
} else {
31+
// its an opening bracket, push it to the stack
32+
strStack.push(ch);
33+
}
34+
}
35+
36+
return strStack.length === 0;
37+
}

exercises/linkedlist/removeDuplicateLinkedList.js

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,13 @@ class LinkedList {
1515

1616
function removeDuplicates(head) {
1717
let node = head;
18-
while (node) {
19-
if (node.next && checkDuplicates(head, node.next)) {
20-
node.next = node.next.next;
21-
}
22-
node = node.next;
18+
while (node && node.next) {
19+
if (node.next.data === node.data) {
20+
node.next = node.next.next;
21+
} else {
22+
node = node.next;
23+
}
2324
}
2425

2526
return head;
2627
}
27-
28-
function checkDuplicates(head, node) {
29-
let currentNode = head;
30-
while (currentNode !== node) {
31-
if (currentNode.data === node.data) {
32-
return true;
33-
}
34-
currentNode = currentNode.next;
35-
}
36-
37-
return false;
38-
}

exercises/strings/isPalindrome.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
3+
4+
Note: For the purpose of this problem, we define empty string as valid palindrome.
5+
6+
Example 1:
7+
8+
Input: "A man, a plan, a canal: Panama"
9+
Output: true
10+
Example 2:
11+
12+
Input: "race a car"
13+
Output: false
14+
*/
15+
16+
const isPalindrome = s => {
17+
// replace all punctuations and chars other than alpha numeric to check if reversed str is a palindrome
18+
s = s.replace(/[^A-Z0-9+$]/gi, '').toLowerCase();
19+
const rev = s.split('').reverse().join('');
20+
21+
return s === rev;
22+
}

exercises/strings/reverseEachWord.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,9 @@ const reverse = word => {
5555

5656
return result;
5757
}
58+
59+
60+
/**If its function to only reverse words of a sentence and not words individually */
61+
var reverseWords = function(s) {
62+
return s.split(' ').reverse().filter(word => word !== '').join(' ').trim();
63+
};

exercises/strings/reverseString.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,16 @@ function reverse(str) {
3131
function reverse(str) {
3232
return str.split('').reduce((rev, char) => char + rev, '');
3333
}
34+
35+
function reverse(str) {
36+
let left = 0;
37+
let right = str.length - 1;
38+
39+
while (left < right) {
40+
let tmp = str[left];
41+
str[left] = str[right];
42+
str[right] = tmp;
43+
}
44+
45+
return str;
46+
}

0 commit comments

Comments
 (0)