/** * @param {number} low * @param {number} high * @return {number[]} */ var sequentialDigits = function(low, high) { let res = [] let lowLen = low.toString().length let highLen = high.toString().length for(let i=lowLen;i<=highLen;i++){ for(let j=1;j<=10-i;j++){ let str = '' let num = j str += num let k = i-1 while(k--){ num++ str += num } let ans = parseInt(str) if(ans>=low && ans<=high){ res.push(ans) } } } return res };
let res = []; let up = 0, down = n - 1, left = 0, right = n - 1; for (let i = 0; i < n; i++) { res[i] = []; } let cur = 1; while (cur <= n * n) { for (let i = left; i <= right; i++) { //左到右 最上添加一行数据-> 上边界改变 res[up][i] = cur++; } up++; for (let i = up; i <= down; i++) { //从上到下 -> 改变右边界 res[i][right] = cur++; } right--; for (let i = right; i >= left; i--) { //从右到左 -> 改变下边界 res[down][i] = cur++; } down--; for (let i = down; i >= up; i--) { //从上到下 -> 改变左边界 res[i][left] = cur++; } left++; } return res; };
/** * @param {number[][]} matrix * @return {number[]} */ var spiralOrder = function (matrix) { let res = [], index = 0; let n = matrix.length, m = matrix[0].length; let up = 0, down = n - 1, left = 0, right = m - 1; while (index < n * m) { for (let i = left; i <= right; i++) { res[index++] = matrix[up][i]; } up++; for (let i = up; i <= down; i++) { res[index++] = matrix[i][right]; } right--; // if(res.length === n*m) break for (let i = right; i >= left; i--) { res[index++] = matrix[down][i]; } down--; for (let i = down; i >= up; i--) { res[index++] = matrix[i][left]; } left++; } // console.log("res:"+res); return res.splice(0, n * m); }; // console.log("return:"+spiralOrder([[1,2,3,4],[5,6,7,8],[9,10,11,12]]));
/** * @param {number[][]} matrix * @return {void} Do not return anything, modify matrix in-place instead. */ var setZeroes = function (matrix) { let n = matrix.length; let m = matrix[0].length; var across = newSet(); var vertical = newSet(); // 遍历每一行 for (let i = 0; i < n; i++) { for (let j = 0; j < m; j++) { if (matrix[i][j] === 0) { //找到0 across.add(i); vertical.add(j); } } } for (let k of across) { for (let j = 0; j < m; j++) { matrix[k][j] = 0; } } for (let k of vertical) { //竖轴置零 for (let j = 0; j < n; j++) { matrix[j][k] = 0; } } return matrix; };
/** * @param {number[][]} matrix * @return {void} Do not return anything, modify matrix in-place instead. */ var setZeroes = function (matrix) { for (let i = 0; i < matrix.length; i++) { for (let j = 0; j < matrix[0].length; j++) { if (Object.is(matrix[i][j], 0)) { // 对行进行操作 for (let k = 0; k < matrix.length; k++) if (!Object.is(matrix[k][j], 0) && k !== i) matrix[k][j] = -0 // 对列进行操作 for (let k = 0; k < matrix[0].length; k++) if (!Object.is(matrix[i][k], 0) && k !== j) matrix[i][k] = -0 } } } return matrix };