LeetCode arrays and matrices

Mondo Technology Updated on 2024-01-31

Given an array a[0,1,..n-1], please construct an array b[0,1,..n-1], where element b[i]=a[0]a[1].a[i-1]a[i+1]..a[n-1]。You can't use division. (Note: b[0] = a[1] *a[2] *a[n-1], b[n-1] = a[0] *a[1] *a[n-2];.))

Ideas: Assumptions:

left[i] = a[0]*.a[i-1]right[i] = a[i+1]*.a[n-1]
So:

b[i] = left[i] *right[i]
It can be known:

left[i+1] = left[i] *a[i]right[i] = right[i+1] *a[i+1]
b[0] has no left, and b[n-1] has no right.

import j**a.util.arrays;public class solution int temp = 1; for(int i = n-2; i >= 0; i--)return b; }
Enter an array of integers and implement a function to adjust the order of the numbers in the array so that all odd numbers are in the first half of the array and all even numbers are in the second half of the array, and the relative positions between odd and odd, even and even numbers remain the same.

Idea: You need a helper array to hold the data.

public class solution int i = 0; for(int k = 0; k < n; k++)for(int k = 0; k < n; k++)
Enter a matrix and print out each number in clockwise order from the outside to the inside, for example, if you enter a matrix like this: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 then print out the numbers 1, 2, 3, 4, 8, 12, 16, 15, 14, 13, 9, 5, 6, 7, 11, 10

Idea: Define four boundaries. Note: Exit should be decided after each move.

import j**a.util.arraylist;public class solution top++;if(top>down) break;Move down for(int i = top; i <= down; i++)right--;if(left>right) break;Move for for (int i = right;.) i>= left; i--)down--;if(top>down) break;Move up for(int i = down; i>= top; i--)left++;if(left>right) break; }return result; }
283.Given an array of nums, write a function that moves all zeros to the end of the array, while maintaining the relative order of the non-zero elements.

Input: [0,1,0,3,12]Output: [1,3,12,0,0].
Description:

You must work on the original array, you can't copy additional arrays. Minimize the number of operations. Ideas:

Move the non-0 number to the front, and then change the back to 0

class solution }for(int i = index; i < nums.length; i++)
566.In MATLAB, there is a very useful function, Reshape, which reshapes a matrix into a new matrix of a different size, but retains its original data.

Give a matrix represented by a two-dimensional array, and two positive integers, r and c, representing the number of rows and columns of the matrix you want to reconstruct, respectively.

The refactored matrix needs to populate all elements of the original matrix in the same row traversal order.

If a reshape operation with the given parameters is feasible and reasonable, a new reshape matrix is output;Otherwise, the original matrix is output.

Input: nums = [[1,2], 3,4]]r = 1, c = 4Output: [1,2,3,4]] Explanation: The result of row traversal of nums is [1,2,3,4]. The new matrix is a 1 * 4 matrix, and the new matrix is populated with the previous element values one by one, one by one. 
Ideas:

Traverse.

class solution return result; }
485.Given a binary array, count the maximum number of consecutive 1s in it.

Input: [1,1,0,1,1,1]Output: 3Explanation: The first two digits and the last three digits are consecutive 1s, so the maximum number of consecutive 1s is 3
Ideas:

Two variables are used to store the current number of contiguous numbers and the other for the maximum number of contiguous numbers.

class solution else cur = 0; }return max; }
Write an efficient algorithm to search for a target value in the m x n matrix. The matrix has the following properties:

The elements in each row are arranged in ascending order from left to right.

The elements of each column are arranged in ascending order from top to bottom.

Given target = 5, true is returned. Given target = 20, false is returned.

Idea: Start from the position of [0][n-1], if the target value is greater than this value, the number of rows will be added, and if the target value is less than this value, the number of columns will be subtracted.

class solution return false; }
378.Given an n x n matrix, where each row and column element is sorted in ascending order, find the k-th smallest element in the matrix.

Note that it's the k-th minor element after sorting, not the k-th different element.

matrix = [ 1, 5, 9], 10, 11, 13], 12, 13, 15]], k = 8, returns 13. 
Ideas:

Binary search. Find by counting the quantity.

Find the leftmost border that makes it hold.

class solution if(cnt >= k) h = mid; else l = mid + 1; }return l; }
645.The set s contains integers from 1 to n. Unfortunately, due to a data error, one element in the collection was copied to the value of another element in the collection, resulting in the collection losing an integer and one element being duplicated.

Given an array of nums, it represents the result of an error in the set s. Your task is to first find the recurring integers, then find the missing integers and return them as arrays.

Input: nums = [1,2,2,4]Output: [2,3].
Ideas:

Sorting, looking for duplicates first.

The missing is by comparing whether the difference between the two values before and after is greater than 1

There are two special cases, the missing first and the missing last.

class solution looks for the missing value if(nums[i] -nums[i-1] >1) } to determine whether the missing value is the last if(nums[nums.com.length-1] != nums.length) return new int;
Input: [1,3,4,2,2]Output: 2
Description:

You can't change the original array (assuming the array is read-only). Only additional o(1) space can be used. The time complexity is less than o(n2). There is only one repeating number in the array, but it may be repeated more than once. Ideas:

If the number of numbers less than or equal to mid is greater than mid, then the number of duplicates is in [l,mid], which may also be mid.

Here, the dichotomous search is based on the number of statistics, which is similar to question 5.

class solution if(cnt>m) h = m; else l = m+1; }return l; }
667.Given two integers n and k, you need to implement an array that contains n different integers from 1 to n, while satisfying the following conditions:

If this array is [a1, a2, a3, .].an], then the array [|a1 - a2|, a2 - a3|, a3 - a4|, an-1 - an|] should have and only k different integers;

If there are multiple answers, you can simply implement and return any of them.

Input: n = 3, k = 1Output: [1, 2, 3]Explanation: [1, 2, 3] contains 3 different integers ranging from 1 to 3, and [1, 1] has and only 1 different integer : 1Input: n = 3, k = 2Output: [1, 3, 2]Explanation: [1, 3, 2] contains 3 different integers in the range of 1-3, and [2, 1] has and only 2 different integers: 1 and 2
Ideas:

Let the first k+1 elements construct k different differences, the sequence is: 1, k+1, 2, k, 3, k-1, .,k/2, k/2+1.

class solution for(int i = k+1; i < n; i++)return result; }
697.Given a non-null array of integers containing only non-negative numbers, the degree of the array is defined as the maximum frequency of any element in the exponential group.

Your task is to find the shortest continuous subarray of degrees that has the same size as nums, and return its length.

Input: [1,2,2,3,1,4,2]Output: 6
Idea: Use three hashmaps, one to store the number of occurrences of each value, one to store the leftmost coordinate of a number, and one to store the rightmost coordinate of a number.

class solution return true; }
565.Array a, which is of n length starting from 0 and contains all integers from 0 to n - 1. Find the largest set s and return its size, where s[i] = and follow the following rules.

Suppose the element with index i a[i] is the first element of s, the next element of s should be a[a[i]], followed by a[a[a[i]]], and so on, and so on, until s appears a duplicate element.

Input: a = [5,4,0,3,1,6,2]Output: 4 Interpretation: a[0] = 5, a[1] = 4, a[2] = 0, a[3] = 3, a[4] = 1, a[5] = 6, a[6] = 2One of the longest s[k]:s[0] = =
Ideas:

Because each generated chain is cyclical, the element that has been visited is marked and the element is not accessed again the next time to avoid redundancy.

class solution max = math.max(max,cnt); return max; }
769.The array arr is [0, 1, ., .].arr.length - 1], we split this array into several "chunks" and sort the chunks separately. The joins are then made so that the result of the joins is the same as the original array sorted in ascending order.

What is the maximum number of chunks we can divide the array into?

Input: arr = [4,3,2,1,0]Output: 1Explanation: Dividing an array into 2 or more chunks does not yield the desired result. For example, dividing into [4, 3], 2, 1, 0] results in [3, 4, 0, 1, 2], which is not an ordered array. Input: arr = [1,0,2,3,4]Output: 4Explanation: We can split it into two pieces, e.g. [1, 0], 2, 3, 4]. However, splitting into [1, 0], 2], 3], 4] gives the maximum number of blocks. 
Ideas:

Records the number of elements that can be sorted in their original position or in their own position.

class solution return result; }
4.Given two positively ordered (from smallest to largest) arrays of size m and n, nums1 and nums2.

Find the median of these two positive ordinal arrays, and require the time complexity of the algorithm to be o(log(m + n)).

You can assume that nums1 and nums2 won't be null at the same time.

Idea: Array partitioning.

Divide two arrays into two parts, with the former having a maximum value smaller than the latter minimum value and equal lengths.

to find the median.

class solution int m = nums1.length; int n = nums2.length; int left = 0, right = m, ansi = -1;median1: maximum of the previous part median2: minimum of the latter part int median1 = 0, median2 = 0; while (left <= right) else }return (m + n) %2 == 0 ? median1 + median2) / 2.0 : median1; }
189.Given an array, move the elements in the array k positions to the right, where k is a non-negative number.

Idea: Flip on the spot.

class solution private void reverse(int nums, int start, int end) }
48.A two-dimensional matrix given n n represents an image. Rotate the image 90 degrees clockwise.

You have to rotate the image in place, which means you need to modify the input 2D matrix directly. Please do not use another matrix to rotate the image.

Idea: Similar to printing an image clockwise.

Rotate each layer from the outside to the inside.

class solution }private void rotate2(int[matrix, int left, int right, int top, int down)
41.Given an unsorted array of integers, find the smallest positive integer that doesn't appear in it.

Input: [3,4,-1,1]Output: 2Input: [7,8,9,11,12]Output: 1
Your algorithm should have a time complexity of o(n) and only use extra space at the constant level.

Idea: Swap in situ and correspond the values to the corresponding base of the array.

class solution }for (int i = 0; i < n; i++)return n + 1; }

Related Pages