/** * @param {number[][]} matrix * @return {number[][]} */ var updateMatrix = function(matrix) { // BFS let q = []; let hash = []; let [m, n] = [matrix.length, matrix[0].length]; const dir = [[1, 0], [-1, 0], [0, 1], [0, -1]];
for (let i = 0; i < m; i++) { hash[i] = []; for (let j = 0; j < n; j++) { if (matrix[i][j] === 0) { q.push({x: i, y: j, step: 0}); hash[i][j] = 0; } } }
while (q.length) { let item = q.shift(); let {x, y, step} = item;
for (let i = 0; i < 4; i++) { let _x = x + dir[i][0]; let _y = y + dir[i][1]; if (_x < 0 || _x >= m || _y < 0 || _y >= n) continue; if (hash[_x][_y] !== undefined) continue; hash[_x][_y] = step + 1; q.push({x: _x, y: _y, step: step + 1}); } }
return hash; };
3Sum
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
Note: The solution set must not contain duplicate triplets.
For example, given array S = [-1, 0, 1, 2, -1, -4],
// remove the duplication while (nums[end] === nums[end - 1]) end-- end-- } } }
return ans }
3Sum Closest
Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.
For example, given array S = {-1 2 1 -4}, and target = 1.
The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
functionbinarySearch(a, target) { var start = 0 , end = a.length - 1;
while(start <= end) { var mid = ~~((start + end) >> 1); if (a[mid] >= target) end = mid - 1; else start = mid + 1; }
return start; }
var threeSumClosest = function(nums, target) { nums.sort(function(a, b) { return a - b; });
var len = nums.length; var ans = Infinity;
for (var i = 0; i < len; i++) for (var j = i + 1; j < len; j++) { var a = target - nums[i] - nums[j]; var pos = binarySearch(nums, a); for (var k = Math.max(0, pos - 1); k <= Math.min(pos + 0, len - 1); k++) { if (k === i || k === j) continue;
var sum = nums[i] + nums[j] + nums[k]; if (Math.abs(sum - target) < Math.abs(ans - target)) ans = sum; }
}
return ans; };
4Sum
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.
Note: The solution set must not contain duplicate quadruplets.
For example, given array S = [1, 0, -1, 0, -2, 2], and target = 0.
A solution set is: [ [-1, 0, 0, 1], [-2, -1, 1, 2], [-2, 0, 0, 2] ]
for (var i = 0; i < len; i++) for (var j = i + 1; j < len; j++) { var a = nums[i] , b = nums[j] , sum = target - a - b;
if (!hash[sum]) continue;
for (var k = 0, _len = hash[sum].length; k < _len; k++) { var item = hash[sum][k];
if (item[0] === i || item[1] === i || item[0] === j || item[1] === j) continue;
var c = nums[item[0]] , d = nums[item[1]];
var tmp = [a, b, c, d].sort(function(a, b) { return a - b; });
var str = tmp.join(','); if (!hashSet[str]) { hashSet[str] = true; ans.push(tmp); } } }
return ans; };
4Sum II
Given four lists A, B, C, D of integer values, compute how many tuples (i, j, k, l) there are such that A[i] + B[j] + C[k] + D[l] is zero.
To make problem a bit easier, all A, B, C, D have same length of N where 0 ≤ N ≤ 500. All integers are in the range of -228 to 228 - 1 and the result is guaranteed to be at most 231 - 1.
Example:
Input: A = [ 1, 2] B = [-2,-1] C = [-1, 2] D = [ 0, 2]
/** * @param {number[]} A * @param {number[]} B * @param {number[]} C * @param {number[]} D * @return {number} */ var fourSumCount = function(A, B, C, D) { let p = newMap(); for (let i = 0, lenA = A.length; i < lenA; i++) for (let j = 0, lenB = B.length; j < lenB; j++) { let sum = A[i] + B[j]; let count = ~~p.get(sum); p.set(sum, count + 1); }
let ans = 0; for (let i = 0, lenC = C.length; i < lenC; i++) for (let j = 0, lenD = D.length; j < lenD; j++) { let sum = C[i] + D[j]; ans += ~~p.get(-sum); }
return ans; };
Add and Search Word – Data structure design
Design a data structure that supports the following two operations:
1 2
void addWord(word) bool search(word)
search(word) can search a literal word or a regular expression string containing only letters a-z or .. A . means it can represent any one letter.
/** * @param {string} word * @return {void} * Adds a word into the data structure. */ WordDictionary.prototype.addWord = function(word) { var node = this.startNode;
for (var i = 0, len = word.length; i < len; i++) { var item = word.charCodeAt(i) - 97; if (!node.nodes[item]) { node.nodes[item] = new Node(); } node = node.nodes[item]; }
node.endFlag = true; };
/** * @param {string} word * @return {boolean} * Returns if the word is in the data structure. A word could * contain the dot character '.' to represent any one letter. */ WordDictionary.prototype.search = function(word) { var node = this.startNode; var isFound = false;
dfs(node, 0);
functiondfs(node, index) { if (isFound) return;
if (index === word.length) { isFound = node.endFlag; return; }
if (word[index] === '.') { for (var i = 0; i < 26; i++) { if (node.nodes[i]) dfs(node.nodes[i], index + 1); } } else { var item = word.charCodeAt(index) - 97; if (node.nodes[item]) dfs(node.nodes[item], index + 1); } }
return isFound; };
/** * Your WordDictionary object will be instantiated and called as such: * var wordDictionary = new WordDictionary(); * wordDictionary.addWord("word"); * wordDictionary.search("pattern"); */