数组
Data Structures & Algorithms · 65 notes
- Next Permutationhistorical
Implement a function that obtains the next permutation. The algorithm must rearrange the given sequence of numbers into the next lexicographically greater permutation.
- Subsetshistorical
Given an integer array nums whose elements are all distinct, return all possible subsets (the power set).
- Unique Pathshistorical
A robot starts at the top-left corner of an m × n grid and can move only down or right. Count the paths to the bottom-right corner.
- Number of Islandshistorical
Given a 2D grid consisting of '1' (land) and '0' (water), calculate the number of islands in the grid.
- Maximal Squarehistorical
In a 2D matrix consisting of '0' and '1', find the largest square containing only '1' and return its area.
- Partition Equal Subset Sumhistorical
Given a non-empty array containing only positive integers, determine whether the array can be partitioned into two subsets whose sums are equal.
- Perfect Squareshistorical
Given a positive integer n, find several perfect-square numbers (such as 1, 4, 9, 16, ...) whose sum equals n. You need to minimize the number of perfect squares used.
- Target Sumhistorical
Assign a plus or minus sign to each number in a nonnegative integer array and count the assignments whose sum equals the target S.
- Combination Sumhistorical
Given an array candidates containing no duplicate elements and a target number target, find all combinations of numbers in candidates whose sum equals target.
- Coin Changehistorical
Given coins of different denominations and a total amount, write a function to compute the minimum number of coins needed to make up that amount. If no combination of coins can make up the amount, return -1.
- Burst Balloonshistorical
Each balloon carries a number, and bursting it earns points based on its neighbors. Choose a bursting order that maximizes the total score.
- House Robberhistorical
You are a professional robber planning to rob houses along a street. Each house contains some cash. The only constraint is that adjacent houses have connected security systems, and the alarm will automatically trigger if two adjacent houses are broken into on the same night.
- Best Time to Buy and Sell Stock with Cooldownhistorical
Given daily stock prices, find the maximum trading profit when selling a stock requires a one-day cooldown before buying again.
- Minimum Path Sumhistorical
Given an m x n grid of non-negative integers, find a path from the top-left corner to the bottom-right corner such that the sum of the numbers along the path is minimized.
- Longest Increasing Subsequencehistorical
Given an integer array nums, find the length of the longest strictly increasing subsequence.
- Jump Gamehistorical
Each element of a nonnegative integer array gives the maximum jump length from that position. Determine whether the last position is reachable.
- Maximum Product Subarrayhistorical
Given an integer array nums, find the contiguous subarray with the largest product (the subarray contains at least one number) and return that product.
- Maximum Subarrayhistorical
Given an integer array nums, find the contiguous subarray with the largest sum (the subarray contains at least one element) and return its sum.
- First Missing Positivehistorical
Given an unsorted integer array nums, find the smallest positive integer that does not appear in the array.
- 3Sumhistorical
Given an array nums containing n integers, determine whether there are three elements a, b, and c such that a + b + c = 0. Find all unique triplets whose sum is 0.
- Find the Duplicate Numberhistorical
Given n + 1 integers whose values range from 1 to n, find the repeated number.
- Two Sumhistorical
Given an integer array, find two numbers in the array whose sum equals the target value. The twoSum function should return the indices of these two numbers (index1, index2), with index1 < index2.
- Find All Numbers Disappeared in an Arrayhistorical
An array of length n contains values from 1 to n, some once and some twice. Find the values in this range that do not appear in the array.
- Subarray Sum Equals Khistorical
Given an integer array and an integer k, find the number of contiguous subarrays whose sum equals k.
- Longest Consecutive Sequencehistorical
Given an unsorted integer array nums, find the length of the longest sequence of consecutive numbers. The elements do not need to be contiguous in the original array.
- Binary Search Upper Boundhistorical
Implement binary search on an ascending array that may contain duplicate values. Return the position of the first value greater than or equal to the target.
- Find First and Last Position of Element in Sorted Arrayhistorical
Given an integer array nums sorted in ascending order and a target value target, find the starting and ending positions of the target value in the array.
- Binary Search Upper Boundhistorical
Implement binary search on an ascending array that may contain duplicate values. Return the position of the first value greater than or equal to the target.
- Find First and Last Position of Element in Sorted Arrayhistorical
Given an integer array nums sorted in ascending order and a target value target, find the starting and ending positions of the target value in the array.
- Search in Rotated Sorted Arrayhistorical
Given a rotated sorted array whose rotation count is unknown in advance (for example, 0 1 2 4 5 6 7 may become 4 5 6 7 0 1 2). Search for the given target value in the array.
- Search in Rotated Sorted Arrayhistorical
Given a rotated sorted array whose rotation count is unknown in advance (for example, 0 1 2 4 5 6 7 may become 4 5 6 7 0 1 2). Search for the given target value in the array.
- Merge Two Sorted Arrayshistorical
Merge two sorted integer arrays A and B into A while preserving order. Assume A has enough space for all elements.
- Find Peak Elementhistorical
A peak element is strictly greater than its neighbors. Find a peak in the given array and return its index.
- Find the K-th Largesthistorical
Given an integer array, use the idea of quicksort to find the K-th largest number in the array.
- Binary Searchhistorical
Given an n-element sorted (ascending) integer array nums and a target value target, write a function to search for target in nums. If the target exists, return its index; otherwise return -1.
- Find Minimum in Rotated Sorted Arrayhistorical
Given a rotated ascending array, find its minimum element.
- Square Roothistorical
Implement the function int sqrt(int x). Compute and return the square root of x, rounded down.
- Spiral Matrixhistorical
Given an m-row, n-column matrix, return all elements of the matrix in clockwise spiral order.
- Find Smallest Letter Greater Than Targethistorical
Given a sorted list of characters letters containing only lowercase English letters, and a target letter target, find the smallest letter in this ordered list that is greater than the target letter.
- Trapping Rain Waterhistorical
Given n non-negative integers representing an elevation map where each bar has width 1, calculate how much rain water can be trapped after raining.
- Container With Most Waterhistorical
Given the heights of vertical lines, choose two lines that form a container with the horizontal axis and maximize its water-holding area.
- Queue Reconstruction by Heighthistorical
Each person is described by their height and the number of people at least as tall ahead of them. Reconstruct the queue from these shuffled descriptions.
- Largest Numberhistorical
Given a group of non-negative integers nums, rearrange the order of each number (each number cannot be split) so that they form the largest integer.
- Top K Frequent Elementshistorical
Given a non-empty integer array, return the k most frequent elements.
- Majority Elementhistorical
Given an array of size n, find its majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times in the array.
- Single Numberhistorical
Given a non-empty integer array, every element appears twice except for one. Find the element that appears only once.
- Move Zeroeshistorical
Given an array nums, write a function to move all 0s to the end of the array while preserving the relative order of the non-zero elements.
- Product of Array Except Selfhistorical
Given an integer array nums of length n, where n > 1, return an output array output where output[i] equals the product of all elements in nums except nums[i].
- Sort Colorshistorical
Given an array with n elements colored red, white, and blue, sort them in place so that elements of the same color are adjacent, in the order red, white, and blue.
- Search a 2D Matrixhistorical
Given a row- and column-sorted matrix and a target value, design an efficient algorithm to determine whether the value is present.
- Maximum Product Subarrayhistorical
Given an integer array nums, find the contiguous subarray with the largest product (the subarray contains at least one number) and return that product.
- Subarray Sum Equals Khistorical
Given an integer array and an integer k, find the number of contiguous subarrays whose sum equals k.
- Maximal Squarehistorical
In a 2D matrix consisting of '0' and '1', find the largest square containing only '1' and return its area.
- Daily Temperatureshistorical
Given a list of daily temperatures, generate a new list where each position contains the minimum number of days you must wait to observe a warmer temperature.
- Daily Temperatureshistorical
Given a list of daily temperatures, generate a new list where each position contains the minimum number of days you must wait to observe a warmer temperature.
- Sliding Window Maximumhistorical
Slide a window of length k across an array from left to right and return the maximum value in each window.
- Merge Intervalshistorical
Given a collection of intervals, merge all overlapping intervals and return the resulting coverage.
- Min Stackhistorical
Design a stack that supports push, pop, and top, and can retrieve the minimum element in constant time.
- Median of Two Sorted Arrayshistorical
Given two sorted arrays of lengths m and n, find the median of their combined values.
- Jump Gamehistorical
Each element of a nonnegative integer array gives the maximum jump length from that position. Determine whether the last position is reachable.
- Shortest Unsorted Continuous Subarrayhistorical
Given an integer array nums, find a continuous subarray such that sorting only this subarray in ascending order makes the whole array sorted in ascending order.
- Best Time to Buy and Sell Stockhistorical
Given daily stock prices, find the maximum profit from a single purchase followed by a single sale.
- Rotate Imagehistorical
Given an n x n two-dimensional matrix matrix representing an image, rotate the image 90 degrees clockwise.