数组
Data Structures & Algorithms · 16 notes
- Search in a 2D Arrayhistorical
Each row and column of a two-dimensional array is sorted in ascending order. Determine whether a given integer appears in the array.
- Two Numbers with Sum Shistorical
Given an array sorted in increasing order and a number S, find two numbers in the array whose sum is exactly S. If there are multiple pairs whose sum equals S, output the pair with the smallest product.
- Continuous Positive Sequences with Sum Shistorical
Given a positive integer S, find all sequences of at least two consecutive positive integers whose sum is S.
- Last Remaining Number in a Circlehistorical
Arrange n numbers in a circle and repeatedly remove every m-th number. Find the last remaining number.
- Arrange an Array into the Smallest Numberhistorical
Given an array of positive integers, concatenate all numbers to form one number and print the smallest possible result. For example, for {3,32,321}, the smallest number that can be formed is 321323.
- Count Occurrences in a Sorted Arrayhistorical
Count how many times a number appears in a sorted array.
- Number Appearing More Than Half the Timehistorical
An array contains a number that appears more than half the length of the array. Find this number. For example, in the length-9 array {1,2,3,2,2,2,5,4,2}, the number 2 appears five times, which is more than half the array length, so output 2.
- Numbers Appearing Only Oncehistorical
In an integer array, every number appears twice except for two numbers. Write a program to find the two numbers that appear only once.
- Inverse Pairs in an Arrayhistorical
An inversion occurs when an earlier array element is greater than a later one. Count all inversions in the given array.
- Duplicate Number in an Arrayhistorical
An array of length n contains values from 0 to n - 1, with some values repeated. Find a duplicate value.
- Minimum Number in a Rotated Arrayhistorical
Moving some elements from the beginning of an array to the end is called a rotation of the array. Given a rotation of a non-decreasingly sorted array, output the minimum element of the rotated array.
- Smallest K Numbershistorical
Given n integers, find the smallest K numbers. For example, among 4,5,1,6,2,7,3,8, the smallest four numbers are 1,2,3,4.
- Robot Movement Rangehistorical
There is a grid with m rows and n columns. A robot starts at coordinate (0,0) and can move one cell at a time left, right, up, or down, but it cannot enter a cell when the sum of the digits of its row and column coordinates is greater than k.
- Maximum in Sliding Windowshistorical
Given an array and a sliding-window size, find the maximum value in every sliding window. For example, for the array {2,3,4,2,6,2,5,1} with window size 3, there are six windows and their maximum values are {4,4,6,6,6,5}.
- Reorder Array with Odd Numbers Before Even Numbershistorical
Given an integer array, implement a function that reorders its numbers so that all odd numbers are in the first half and all even numbers are in the second half, while preserving the relative order among odd numbers and among even numbers.
- Maximum Sum of a Contiguous Subarrayhistorical
Given an integer array that may contain negative values, find the maximum sum of a contiguous subarray.