This is the 11th day of my participation in the genwen Challenge
This series is nothing fancy, just pure Leetcode problem breakdown analysis, not to use a sexy line or a niche solution, but with clear code and simple enough thinking to help you clarify the problem. Let you in the interview no longer afraid of algorithm written test.
118. Multiplection-strings
The label
- string
- medium
The title
Leetcode portal
Let’s just open leetCode.
Given two non-negative integers num1 and num2 represented as strings, return the product of num1 and num2, whose product is also represented as strings.
Example 1:
Input: num1 = "2", num2 = "3" output: "6"Copy the code
Example 2:
Input: num1 = "123", num2 = "456" Output: "56088"Copy the code
The basic idea
Is this question going to be vertical round and round? Sorry, we’re using JS
ES6 has a BigInt. Both time and space are better than 97.71%
Writing implement
var multiply = function(num1, num2) {
return ((BigInt(num1)*BigInt(num2)) + "")};Copy the code
119. Spiral Matrix II (Spiral-Matrix-II)
The label
- 2 d array
- medium
The title
Leetcode portal
Let’s just open leetCode.
Given a positive integer n, generate an n x n square matrix matrix containing all elements from 1 to n^2 in a clockwise spiral order.
Example 1:
Input: n = 3 output: [[1,2,3],[8,9,4],[7,6,5]]Copy the code
Example 2:
Input: n = 1 Output: [[1]]Copy the code
The basic idea
We did the spiral matrix I before, and just to remind you, it’s the same idea.
- build
n * n
The matrix of - Boundary division, set the coordinates of the four angles
- Starting at the upper left corner of the bounding range, the circle of cells is completed, each step curNum++, until the number of cells (n * n) is exhausted
Writing implement
var generateMatrix = function(n) {
const matrix = new Array(n).fill(0).map(() = > new Array(n).fill(0))
let curNum = 1;
// The coordinates of the four corners
let [left, top, right, bottom] = [0.0, n-1, n-1]
// The current number is growing, but not greater than the total number of squares
while (curNum <= n * n) {
// The next step is to walk through the squares
// The top line first
for (let i = left; i <= right; i++) {
// The first line so top is set to 0
matrix[top][i] = curNum
curNum++
}
// The next time you turn it around, the second row will have top 1, so you need top++
top++;
// Start with top and go down to bottom
// Then don't repeat, just turn, careful line
for (let i = top; i <= bottom; i++) {
// This time the column is certain, is the rightmost column, the coordinate right
matrix[i][right] = curNum
curNum++
}
right--;
for (let i = right; i >= left; i--) {
matrix[bottom][i] = curNum
curNum++;
}
bottom--;
for (let i = bottom; i >= top; i--) {
matrix[i][left] = curNum
curNum++;
}
left++;
}
return matrix;
}
console.log(generateMatrix(3))
// [[1, 2, 3], [8, 9, 4], [7, 6, 5]]
Copy the code
In addition, we recommend this series of articles, very simple, on the front of the advanced students are very effective, wall crack recommended!! Core concepts and algorithm disassembly series
If you want to brush the questions with me, you can add me on wechat. Click here to make a friend Or search my wechat account, infinity_9368. You can chat with me and add my secret code “Tianwang Gaidihu”. Verify the message please send me Presious tower shock the rever Monster, I see it through, after adding I will do my best to help you, but pay attention to the way of asking questions, it is suggested to read this article: the wisdom of asking questions