Creates the target array in the given order

















You are given two integer arrays nums and index. You need to create the target array according to the following rules:

The target array target is initially empty.
Read nums[I] and index[I] from left to right, insert nums[I] at index[I].
Repeat the previous step until there are no elements to read in either NUMS or index.
Return the target array.
They make sure that the insertion position is always there.

Example:
Input: nums = [0,1,2,3,4], index = [0,1,2,2,1]
Output:,4,1,3,2 [0]
Explanation:
nums index target
0 0 [0]
1 1 [0, 1]
2 2 [0]
3 2 [0,1,3,2]
4 1 [0,4,1,3,2]








This question examines the basic operations on arrays.










5178. The four factor

















Given an array of integers, nums, return the sum of the integers that have exactly four factors.

If there are no integers in the array, 0 is returned.

Example:
Input: nums = [21,4,7]
Output: 32
Explanation:
21 has four factors: 1, 3, 7, and 21
4 has three factors: 1, 2, 4
7 has two factors: 1 and 7
The answer is just the sum of all the factors of 21.








This question mainly investigates the following two points:

  • Use the Set data structure in JavaScript for de-duplication

  • Use the square root operation to reduce the number of unnecessary cycles










Check the valid paths in the grid

















I give you an M x N grid. Each cell in the grid represents a street. A grid[I][J] street can be:

1 represents the street connecting the left cell to the right cell.
2 represents the street connecting the upper and lower cells.
3 represents the street connecting the left cell to the lower cell.
4 represents the street connecting the right cell to the lower cell.
5 represents the street connecting the left cell to the upper cell.
6 represents the street connecting the right cell to the upper cell.

Example:
Grid = [[2,4,3],[6,5,2]]
Output: true,
Explanation: As shown in the figure, you can start from (0, 0), access all cells in the grid and reach (M-1, n-1).








This problem focuses on the search algorithm in graph theory.

Before deciding which search method to use, you need to comb through the pathfinding method.

First of all, for any street, we need to know which directions it can move in (up, right, down and left) :

Next, we need to determine whether the next street is connected to the current street:

It is a pity that the DFS used in this problem will cause stack explosion in the last few test cases, so here is the writing method of BFS, which needs to be filled with details in the search process:

  • Determine which directions the street can move in

  • Determine if the next street is beyond the boundary

  • To determine whether the next street has been accessed, you can save the extra space required to record the status by setting the original value to a negative value

  • Determine whether the current street is connected to the next street










1392. The longest happy prefix

















A happy prefix is a string that is both a non-empty prefix and a suffix (excluding the original string itself) in the original string.

Given a string s, please return its longest happy prefix.
An empty string is returned if no prefix exists that satisfies the problem.

Example:
Enter: s = ‘level’
Output: ‘l’
Explanation: There are 4 prefixes (‘ L ‘, ‘le’, ‘Lev ‘, ‘leve’) and 4 suffixes (‘ L ‘, ‘el’, ‘vel’, ‘evel’), not including S itself. The longest string that is both a prefix and a suffix is ‘l’.








This is a Hard problem, but when I violently solve AC, my heart is like this:

Aside from the joy of violent solution, this problem can use the two-pointer technique to solve the partial matching table of strings.

KMP string matching algorithm partial matching table (maximum common prefix and suffix array).

Once you have a partial match table, you know the length of the longest common prefix and suffix for the entire string, and you can figure out the happiest prefix.










Highlights from the past






  • LeetCode Tour for front End Engineers — Week 180

  • LeetCode Tour for front End Engineers – Week 179

  • LeetCode Tour for Front End Engineers – Week 178

  • Front End Engineer’s LeetCode Journey — Week 177

  • JavaScript AC solutions to problems on LeetCode