Welcome to my blog

Common Topics

Rotate the image

Title source

Matrix = Array. From (new Array(m), (item) => new Array(n).fill(0)) x: y-> y: But the value of x is the flashbacks (0, 2) - > (1, 2) - > (2, 2) = > (2, 0) - > (2, 1) - > (2, 2) 3 - > 6-9 > = > 9 - > - > 3 = > (0, 2) = = = (2, 2) So it should be x: y-> y: (n-x-1)
const rotate = function (matrix) { const n = matrix.length; const matrix_new = new Array(n).fill(0).map(() => new Array(n).fill(0)); for (let i = 0; i < n; i++) { for (let j = 0; j < n; j++) { matrix_new[j][n - i - 1] = matrix[i][j]; } } for (let i = 0; i < n; i++) { for (let j = 0; j < n; j++) { matrix[i][j] = matrix_new[i][j]; }}}

The product of an array other than itself

Title source

Ideas: iterate over each item in the array, and then iterate over yourself once to find the product. But hahaha, I'm out of time. The n by n algorithm is too complex to pass. Think about it is not too simple. So when you're interviewing for a job, if you think a question is too easy, be sure to think about what you're doing. Initializes two empty arrays L and R. For a given index I, L[I] represents the product of all the numbers to the left of I and R[I] represents the product of all the numbers to the right of I. > 2. We need two loops to fill the values of the L and R arrays. For array L, L[0] should be 1, because there are no elements to the left of the first element. For other elements: L[I] = L[I -1] * nums[I -1]. Similarly, for the array R, R[length-1] should be 1. Length refers to the size of the input array. Other elements: R[I] = R[I +1] * nums[I +1]. > 3. When R and L arrays are filled, we only need to iterate over the input array, and the value at index I is: L[I] * R[I]. 4. Conclusion is exhausted and dull.
const productExceptSelf = nums => {
    const L = new Array(nums.length)
    const R = new Array(nums.length)
    const result = []
    for (let i = 0; i < L.length; i++) {
        if (i === 0) {
            L[i] = 1
        } else {
            L[i] = L[i - 1] * nums[i - 1]
        }

    }
    for (let i = R.length - 1; i >= 0; i--) {
        if (i === R.length - 1) {
            R[i] = 1
        } else {
            R[i] = R[i + 1] * nums[i + 1]
        }
    }
    for (let i = 0; i < nums.length; i++) {
        result[i] = L[i] * R[i]
    }
    return result
}

The sum of three number

Title source

The sum of two numbers is the sum of the remaining two numbers, and the sum of three numbers is the sum of the remaining two numbers. A pointer to the left of the remaining array is used, and a pointer to the right of the remaining array is used. Nums = [-1, -1, -1, 0, 1, 2] // sort Nums = [-1, -1, -1, 0, 1, 2] // sort Nums = [-1, -1, -1, 0, 1, 2] //
const threeSum = function (nums) { let ans = []; const len = nums.length; if (nums == null || len < 3) return ans; nums.sort((a, b) => a - b); for (let i = 0; i < len; If (nums[I] > 0) break; if (nums[I] > 0) break; If (I bb0 0 &&nums [I] == nums[I - 1]) continue; let L = i + 1; let R = len - 1; while (L < R) { const sum = nums[i] + nums[L] + nums[R]; if (sum == 0) { ans.push([nums[i], nums[L], nums[R]]); while (L < R && nums[L] == nums[L + 1]) L++; / / to the while (L < R && nums = = nums [R] [R - 1)) R -; / / to heavy L++; R--; } else if (sum < 0) L++; else if (sum > 0) R--; } } return ans; }