Skip to content

Commit 567b2ed

Browse files
authored
Merge branch 'master' into parenthesesGenerator
2 parents 8f994de + 2475de5 commit 567b2ed

9 files changed

Lines changed: 290 additions & 4 deletions

File tree

‎Backtracking/Algorithms_ToDo.md‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
# Algorithms ToDo
22

33
+ Valid Sudoku [LeetCode #36](https://leetcode.com/problems/valid-sudoku/)
4-
+ Regular Expression Matching [LeetCode #10](https://leetcode.com/problems/regular-expression-matching/)
54
+ Letter Combination of a Phone Number [LeetCode #17](https://leetcode.com/problems/letter-combinations-of-a-phone-number/)
6-
+ Combination Sum [LeetCode #39](https://leetcode.com/problems/combination-sum/)
75
+ Wildcard Matching [LeetCode #44](https://leetcode.com/problems/wildcard-matching/)
8-
+ N-Queens [LeetCode #51](https://leetcode.com/problems/n-queens/)
9-
+ Restore IP Addresses [LeetCode #93](https://leetcode.com/problems/restore-ip-addresses/)
6+
+ N-Queens [LeetCode #51](https://leetcode.com/problems/n-queens/)

‎Backtracking/CombinationSum.js‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/**
2+
* @param {number[]} candidates
3+
* @param {number} target
4+
* @return {number[][]}
5+
*/
6+
var combinationSum = function(candidates, target) {
7+
var result = [];
8+
9+
if (candidates == null || candidates.length === 0) {
10+
return result;
11+
};
12+
13+
var current = [];
14+
candidates.sort();
15+
16+
combinationSumHelper(candidates, target, 0, current, result);
17+
18+
return result;
19+
};
20+
21+
function combinationSumHelper(candidates, target, j, curr, result){
22+
if(target === 0){
23+
var temp = curr.slice();
24+
result.push(temp);
25+
return;
26+
}
27+
28+
for(var i=j; i<candidates.length; i++){
29+
if(target < candidates[i]) {
30+
return;
31+
}
32+
curr.push(candidates[i]);
33+
combinationSumHelper(candidates, target - candidates[i], i, curr, result);
34+
curr.pop();
35+
}
36+
}
37+
38+
const candidates = [2,3,6,7];
39+
const target = 7;
40+
console.log(combinationSum(candidates, target));
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* @param {string} s
3+
* @param {string} p
4+
* @return {boolean}
5+
*/
6+
7+
var isMatch = function(s, p) {
8+
if (p === "") return s === "";
9+
var first_match = (s!="" && (p[0] == s[0] || p[0] == '.'));
10+
11+
if (p.length >= 2 && p[1] == '*'){
12+
return (isMatch(s, p.substring(2)) ||
13+
(first_match && isMatch(s.substring(1), p)));
14+
} else {
15+
return first_match && isMatch(s.substring(1), p.substring(1));
16+
}
17+
};

‎Backtracking/nqueens.js‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
var solveNQueens = function(n) {
2+
const result = [];
3+
solveNQueen(result, n);
4+
return result;
5+
};
6+
7+
function solveNQueen(result, size, board = [], row = 0) {
8+
if (row === size) {
9+
buildResult(result, board, size);
10+
return;
11+
}
12+
13+
for(let col = 0; col < size; col++) {
14+
if (safeToPlaceQuen(board, row, col, size)) {
15+
board.push(col);
16+
solveNQueen(result, size, board, row + 1);
17+
board.pop();
18+
}
19+
}
20+
}
21+
22+
function safeToPlaceQuen(board, row1, col1, size) {
23+
for( let row2 = 0; row2 < row1; row2++) {
24+
const col2 = board[row2];
25+
26+
if (col1 === col2) {
27+
return false;
28+
}
29+
30+
const colDist = Math.abs(col1 - col2);
31+
const rowDist = row1 - row2;
32+
33+
if (colDist === rowDist) {
34+
return false;
35+
}
36+
}
37+
38+
return true;
39+
}
40+
41+
function buildResult(result, board, size) {
42+
const boardWithQueens = board.map(col =>
43+
'.'.repeat(col) + 'Q' + '.'.repeat(size - col - 1)
44+
)
45+
46+
result.push(boardWithQueens);
47+
}
48+
49+
50+
51+
//sample
52+
console.log(solveNQueens(4));
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
var restoreIpAddresses = function(ques) {
2+
3+
if (ques.length > 12 || ques.length < 4) return [];
4+
const result = [];
5+
restoreIpAddresses_(ques, [], result);
6+
return result;
7+
8+
};
9+
10+
const restoreIpAddresses_ = (ques, recAns, result) => {
11+
12+
if (recAns.length === 4 && !ques.length) {
13+
result.push(Array.from(recAns).join('.'));
14+
return;
15+
}
16+
17+
for (let i = 1; i < 4; i++) {
18+
19+
if(ques.length < i) continue;
20+
21+
const str = ques.slice(0,i);
22+
if ((str.length > 1 && str[0] === '0') || Number(str) > 255) continue;
23+
24+
recAns.push(str);
25+
restoreIpAddresses_(ques.slice(i), recAns, result);
26+
recAns.pop();
27+
28+
}
29+
}
30+
31+
//test Input.
32+
var ans=restoreIpAddresses("25525511135");
33+
console.log(ans);
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* Yields all elements of a matrix in a row-traversing order.
3+
* @param {number[][]} matrix
4+
* @yield {number} the next element in the matrix
5+
*/
6+
function* traverse(matrix) {
7+
for (const row of matrix) {
8+
for (const cell of row) {
9+
yield cell;
10+
}
11+
}
12+
}
13+
14+
/**
15+
* Reshapes a matrix into a new one with different size but keep its original data.
16+
*
17+
* Takes a matrix represented by a two-dimensional array, and two positive integers r and c representing the row number and column number of the wanted reshaped matrix, respectively.
18+
* The reshaped matrix is filled with all the elements of the original matrix in the same row-traversing order as they were.
19+
*
20+
* If the 'reshape' operation with given parameters isn't possible and legal, outputs the original matrix.
21+
*
22+
* @param {number[][]} nums
23+
* @param {number} r
24+
* @param {number} c
25+
* @return {number[][]}
26+
*/
27+
var matrixReshape = function (nums, r, c) {
28+
if (nums.length * nums[0].length !== r * c) {
29+
// Both matrices don't hold the same number of elements.
30+
return nums;
31+
}
32+
33+
const traversal = traverse(nums);
34+
return Array.from({ length: r }, () =>
35+
Array.from({ length: c }, () => traversal.next().value)
36+
);
37+
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//Language: Javascript
2+
//Author: Sarah Sakhri
3+
//Github: https://github.com/sarahsakhri
4+
5+
let x = [22, 35, 40, 5, 777, 8, 9, 9, 223, 46, 34];
6+
7+
console.log("---- Original array -----");
8+
console.log(x);
9+
10+
function bubbleSort(array) {
11+
12+
for (let i = 0; i < array.length; i++) {
13+
14+
for (let j = 0; j < (array.length - i - 1); j++) {
15+
16+
if (array[j] > array[j + 1]) {
17+
18+
let temp = array[j];
19+
array[j] = array[j + 1];
20+
array[j + 1] = temp;
21+
}
22+
}
23+
}
24+
}
25+
26+
bubbleSort(x);
27+
28+
console.log("---- Bubble sorted array -----");
29+
console.log(x);
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
//Language: Javascript
2+
//Author: Sarah Sakhri
3+
//Github: https://github.com/sarahsakhri
4+
5+
let x = [66, 0, 19, -45, 26, 56, 15, 21, -98];
6+
console.log("---- Original array -----");
7+
console.log(x);
8+
9+
10+
function selectionSort(arr) {
11+
for (i = 0; i < arr.length; i++) {
12+
let min = i;
13+
for (let j = i + 1; j < arr.length; j++) {
14+
if (arr[j] < arr[min]) {
15+
min = j;
16+
}
17+
}
18+
19+
if (i !== min) {
20+
let temp = arr[i];
21+
arr[i] = arr[min];
22+
arr[min] = temp;
23+
}
24+
}
25+
26+
return arr;
27+
}
28+
29+
selectionSort(x);
30+
31+
console.log("---- Selection sorted array -----");
32+
console.log(x);
33+
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* @param {string} digits
3+
* @return {string[]}
4+
*/
5+
var letterCombinations = function(digits) {
6+
let sanitizeInput = function(unsanitizedDigits) {
7+
let regex = /(\W|_|[0-1a-zA-Z])/g;
8+
let sanitized = unsanitizedDigits.replace(regex, "");
9+
return sanitized;
10+
};
11+
const mapping = {
12+
2: ["a", "b", "c"],
13+
3: ["d", "e", "f"],
14+
4: ["g", "h", "i"],
15+
5: ["j", "k", "l"],
16+
6: ["m", "k", "o"],
17+
7: ["p", "q", "r", "s"],
18+
8: ["t", "u", "v"],
19+
9: ["w", "x", "y", "z"],
20+
};
21+
let sanitizedDigits = sanitizeInput(digits);
22+
if (sanitizedDigits.length > 12) {
23+
return "1,388,674 combinations should be more than enough for you. Provide 12 or fewer digits.";
24+
}
25+
if (sanitizedDigits.length < 1) {
26+
return "Nothing to return.";
27+
}
28+
let finalArray = [];
29+
30+
const backtrackRecursive = function(combination, nextDigits) {
31+
if (nextDigits.length === 0) {
32+
finalArray.push(combination);
33+
} else {
34+
for (let i = 0; i < mapping[nextDigits[0]].length; i += 1) {
35+
backtrackRecursive(
36+
combination + mapping[nextDigits[0]][i],
37+
nextDigits.slice(1)
38+
);
39+
}
40+
}
41+
};
42+
43+
if (sanitizedDigits) {
44+
backtrackRecursive("", sanitizedDigits);
45+
}
46+
47+
return finalArray;
48+
};

0 commit comments

Comments
 (0)