This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function spiralOrder(matrix) { | |
if (!matrix.length || !matrix[0].length) return []; | |
const result = []; | |
const rows = matrix.length, cols = matrix[0].length; | |
let top = 0, bottom = rows - 1, left = 0, right = cols - 1; | |
while (top <= bottom && left <= right) { | |
// Traverse right | |
for (let i = left; i <= right; i++) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function foo(nums, target) { | |
const indices = {}; | |
for (let i = 0; i < nums.length; i++) { | |
const rem = target - nums[i]; | |
if (indices[rem] !== undefined) return [indices[rem], i]; | |
indices[nums[i]] = i; | |
} |