Basic features of arrays

Arrays are the simplest data structures.

Arrays are used to store a series of data of the same type. Data is stored consecutively and memory is allocated at one time.

To insert and delete in the middle of an array, all subsequent data must be moved each time to maintain continuity, time complexity O(N).

Array index

Arrays support fast access to data through indexes, an array of length N with subscripts starting at 0 and ending at n-1.

Array elements can be accessed by the array name indexed. The index of the element is placed in square brackets, following the array name.

Array capacity

The array memory space is allocated at a time. During capacity expansion, apply for a larger memory space and copy data to the new memory space. The time complexity is O(N).

Array traversal

for (int i = 0; i < arr.length; I ++) {// use arr[I] to manipulate array elements}Copy the code

Force button 414. The third largest number

Given a non-empty array, return the third largest number in the array. If not, return the largest number in the array.

A detailed description

Their thinking

Define three temporary variables to hold the maximum value, the second largest value, and the third largest value respectively.

First, second, third;

Iterate through the array, loop through the array, update in sequence, each update order is third, second, first;

If a[I] > first, then first=a[I]; If a[I] > second, then second=a[I]; If a[I] > third=a[I]Copy the code

The third largest number is returned, or the largest number is returned.

Complexity analysis

Time complexity: O(n) where n is the length of the array. You need to go through the array once. Space complexity: O(1)Copy the code

Animation simulation

The sample

Public int thirdMax(int[] nums) {if (nums.length == 1) return nums[0]; if (nums.length == 2) return Math.max(nums[0], nums[1]); Int first = nums[0]; int first = nums[0]; long second = Long.MIN_VALUE; long third = Long.MIN_VALUE; Third, second, first for (int n: nums) {if (n > first) {third = second; second = first; first = n; } else if (n > second && n ! First) {// The value is greater than the second value, but not equal to the maximum value, equal to the maximum need not update; second = n; } else if (n > third && n ! = first && n ! = second) {// The value is greater than the third value, but not equal to the first, second value; } } return third ! = Long.MIN_VALUE ? (int) third : first; }Copy the code

Short words long said

Learning algorithms, don’t know where to start? What do you learn first? At what stage should I brush what questions?

Structurally sort the questions by category, from lower to higher, graphical algorithm (update…) , interested in children’s shoes, welcome to start from xiaobai zero base brush force buckle, common progress!

Interesting search: Jiongmefeishi (or search: Jiongmefeishi)

Public number reply: 678, access to the classified part of the brush question order, the subsequent content will continue to update, interested partners free to take!

In addition, about classification, ask friends do not ask me the name of the last category, strange technology and craftiness is a commendatory term, meaning refers to novel skills and works.

Force buckle training system topic, topic classification and recommended brush topic sequence and solution

Currently, it is tentatively divided into four stages:

Algorithm low entry - martial forging body algorithm intermediate advanced - Wu Huang lian heart algorithm high order strengthening - Wudi essence of the soul algorithm strange technology pornography - combat lexus above classification forgive me have a dream...Copy the code

Missing content, trying to sort out…