陣列

陣列題型的解題教學,9 題。最基礎的題型,重點在「一次掃描能不能同時維護你需要的狀態」。

Rotate Image 解題教學示意圖|LeetCode 48

Rotate Image 解題教學:3 種解法完整比較|LeetCode 48 Best Practice

You are given an n x n 2D matrix representing an image, rotate the image by 90 degrees (clockwise).

You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.

Rotate Image 解題教學:3 種解法完整比較|LeetCode 48 Best Practice Read More »

Leetcode, 技術文章, , , ,
Spiral Matrix III 解題教學示意圖|LeetCode 885

Spiral Matrix III 解題教學:3 種解法完整比較|LeetCode 885 Best Practice

You start at the cell (rStart, cStart) of an rows x cols grid facing east. The northwest corner is at the first row and column in the grid, and the southeast corner is at the last row and column.

You will walk in a clockwise spiral shape to visit every position in this grid. Whenever you move outside the grid’s boundary, we continue our walk outside the grid (but may return to the grid boundary later.). Eventually, we reach all rows * cols spaces of the grid.

Return an array of coordinates representing the positions of the grid in the order you visited them.

Spiral Matrix III 解題教學:3 種解法完整比較|LeetCode 885 Best Practice Read More »

Leetcode, 技術文章, , , ,
Maximum Product Subarray 解題教學示意圖|LeetCode 152

Maximum Product Subarray 解題教學:3 種解法完整比較|LeetCode 152 Best Practice

Given an integer array nums, find a subarray that has the largest product, and return the product.

The test cases are generated so that the answer will fit in a 32-bitinteger.

Maximum Product Subarray 解題教學:3 種解法完整比較|LeetCode 152 Best Practice Read More »

Leetcode, 技術文章, , , ,
Search in Rotated Sorted Array 解題教學示意圖|LeetCode 33

Search in Rotated Sorted Array 解題教學:3 種解法完整比較|LeetCode 33 Best Practice

There is an integer array nums sorted in ascending order (with distinct values).

Prior to being passed to your function, nums is possibly rotated at an unknown pivot index k (1 <= k < nums.length) such that the resulting array is [nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]] (0-indexed). For example, [0,1,2,4,5,6,7] might be rotated at pivot index 3 and become [4,5,6,7,0,1,2]. Given the array nums after the possible rotation and an integer target, return the index of target if it is in nums, or -1 if it is not in nums. You must write an algorithm with O(log n) runtime complexity.

Search in Rotated Sorted Array 解題教學:3 種解法完整比較|LeetCode 33 Best Practice Read More »

Leetcode, 技術文章, , , ,
Find Minimum in Rotated Sorted Array 解題教學示意圖|LeetCode 153

Find Minimum in Rotated Sorted Array 解題教學:3 種解法完整比較|LeetCode 153 Best Practice

Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,2,4,5,6,7] might become:

[4,5,6,7,0,1,2] if it was rotated 4 times.
[0,1,2,4,5,6,7] if it was rotated 7 times.

Notice that rotating an array [a[0], a[1], a[2], …, a[n-1]] 1 time results in the array [a[n-1], a[0], a[1], a[2], …, a[n-2]].

Given the sorted rotated array nums of unique elements, return the minimum element of this array.

You must write an algorithm that runs in O(log n) time.

Find Minimum in Rotated Sorted Array 解題教學:3 種解法完整比較|LeetCode 153 Best Practice Read More »

Leetcode, 技術文章, , , ,
Maximum Subarray 解題教學示意圖|LeetCode 53

Maximum Subarray 解題教學:3 種解法完整比較|LeetCode 53 Best Practice

Given an integer array nums, find the subarray with the largest sum, and return its sum.

Maximum Subarray 解題教學:3 種解法完整比較|LeetCode 53 Best Practice Read More »

Leetcode, 技術文章, , , , ,