Data structures and algorithms-arrays

Source: Internet
Author: User

Question 1: How to use recursion to implement an array summation

Method 1:

Task Type 2: How to print a two-dimensional array with a for loop

Method 1:array the row and column numbers in a two-dimensional array are [I/maxy],[i%maxy] respectively

Task 3: Using recursive and non-recursive methods to find two points

Question 4: How to find the number of occurrences of a given number in a sorted array

Method 1: Two-point search, find the left and right boundary, respectively, the difference between the boundary

Method 2: Sequential lookup, statistical counting

Task Type 5: How to calculate the intersection of two ordered integer arrays

Method 1: Using the two-way merge to traverse two arrays, respectively, I, j from the beginning to traverse two arrays, if arr[i]<arr[j],i++; if arr[i]>arr[j],j++; otherwise will arr[i] deposited into the intersection, i++,j++;

Method 2: Traverse two arrays sequentially, store the array elements in a hash table, and count the array elements. If 2, the intersection element for both

Method 3: Iterate through any of the two arrays, place the traversed elements into the hash table, and then iterate over the other array, querying the established hash table, and if present, the intersection element

Task 6: How to find the most repeated number in an array

Method 1: With space-time, you can define an array of int Count[max], and initialize its array elements to 0, then iterate over the array, and for the count[a[i]]++ operation, find the maximum number in count.

Method 2: Use the Map mapping table by introducing the map table, where the first is the keyword, the second is the number of occurrences of the keyword, and then the number of times is determined.

Question 7: How to find the number of occurrences in an array that exceeds half in the time complexity of O (n)

Method 1:hash, first creates a hash_map, where key is an array of element values, and the number of times that value appears. Iterate through the array and use Hash_map to count the occurrences of each number.

Method 2:partition method, because the most frequently occurring elements will also be the median, so every time you use Patition to find a position of a number, if the number is just the median, it is more than half of the number.

Method 3: Each time you take out two different numbers, the number of repetitions in the remaining numbers is definitely more than the other numbers, reducing the scale. If you delete two different numbers at a time, the number of the highest frequency in the remaining number is the same as 50%.

Task 8: How to find the unique repeating element in the array (array a[n],1 to N-1 number in a[n], one of which repeats once)

Method 1: Because the topic requires each element to access only once, without secondary storage space, you can start from the principle, using mathematical summation, because only one number repeats once, and the number is continuous, according to the summation principle, the sum of all the items of the array, and then subtract 1 to N-1, that is, the number of repetitions.

Method 2: Use an XOR method, the same number is 0, the number is not the same as 1.

Method 3: Bitmap method, first apply a string of length N-1 and all ' 0 ', then iterate through the array a[n], take the value of each array element A[i], the corresponding position in the corresponding string is set to 1, if already 1, then the number is the number of repetitions.

Task 9: If you determine whether the values in an array are contiguous

An integer sequence in which the element value may be any number in the 0~65535, the same element will not recur , and 0 is an exception that can recur. If the difference between the maximum and minimum values is greater than 0, then it is discontinuous. (If the elements in the array are duplicates, you need to order the duplicates and intervals)

Question 10: How to find an odd number of elements in an array

Method 1: Using an XOR method, all elements will be different or the last remaining will be an odd number of elements

Extension: An array of n elements, the number of n-2 appear even this, two numbers appear odd number of times (the two numbers are not equal), how to use O (1) space complexity, find out the two numbers?

After all the numbers are different or later, the result is equal to two numbers a and B xor. X=a^b, find the lowest bit in X is 1, where A and B are sure that there is a number of the bit is 1, all of the bits in the array are 1 of the number of the XOR, all the bits of 0 are XOR. will be able to get these two numbers separately.

Task 11: How to find the number of symbol conditions in a series

Method 1: Brute force method, to find all possible pairs of and, to see if and whether the number

Method 2: Sort the array first, then use the binary lookup method, point to the first and last element with two indicators, and then traverse the middle at the same time from both ends until the two pointers cross.

Method 3: Sort by count, put the number of 1~n in a large piece of space, such as 1 placed in the 1th digits, n in the n number of bits, O (n) time complexity, and then the value.

Extension: How to solve an array pair if it is an arbitrary array instead of a regular array? That is, given an array of arbitrary integers array[n], look for pairs that are in the array and whose values are sum.

Method 1: Brute Force method

Method 2: Same method as above 2

Method 3: Hash the array into the hash table and find the sum-m in the hash table

Extension: The known size of M, N of two unordered arrays A, B and a number constant C, to meet a[i]+b[j]=c all A[i] and b[j]

Method 1: Enumeration method

Method 2: Sorting + Two-point lookup method

Method 3: Sort + Linear Scan method

Method 4:map, put an array into the map, traverse another array, and for a few m, find the sum-m in the map

Question 12: How to find missing numbers in a sequence

An unordered sequence consisting of n-1 integers whose elements are different integers in 1~n. How to find missing integers in a sequence

You can add sums by accumulating them. The n-1 integer is first explained, the sum is obtained, and then the sum is subtracted by the (1+n) N/2, and the resulting difference is the missing integer.

Task Type 13: How to determine if the array has duplicate elements (assuming that the array a has n elements, the value range of the element is 1~n, how to determine whether the array has duplicate elements)

Method 1: Sort the array, and then compare whether adjacent elements are the same. Time complexity O (NLOGN)

Method 2: Use the Bitmap method to define a char array of length N/8, each bit representing whether the number has occurred. Iterate through the array and use bitmap to count whether the numbers appear. Time complexity O (n), spatial complexity O (n)

Method 3: Iterate through the array, assuming that the number of the first position is a[i], and I!=a[i], then a[i] is swapped to the position labeled A[i], and if the position of the subscript is a[i]], then duplicate appears.

Question 14: How to reorder the array so that the left of the array is odd and the right is even.

Method 1: Use a similar method for quick sorting, using two pointers to point to the head and tail, and swap them if they are not pointing to the corresponding number

Task Type 15: How to remove a repeating number from an array of integers.

Method 1: It iterates through each element of the array, compares the elements to the previous traversed elements, removes them if they are found to be duplicates of the previous array, and, if there is no repetition, continues to traverse the next element. Because each element is compared to all previous elements, the time complexity is O (n^2)

Method 2: Sort the elements, then iterate through each element, and count the number of elements that do not repeat, if the current element is different from its previous element, execute a[count]=a[i],count++;

Question 16: How to find the second largest number in an array.

Method 1: Sort all the elements and the second largest number in the second position

Method 2: Use two variables to record the first and second largest number, for the maximum initial value of the primary element, the second largest number is initialized to the smallest negative numbers. Each time the current element is compared with the maximum value, if it is greater than the maximum value, it is assigned to the maximum value, and the original maximum value is assigned to the second large value, otherwise it is compared with the second largest value if it is less than the maximum value.

Question 17: How to move the number of M after the array to the previous m number

Method 1:

Reverses the order of the first m elements;

Reverses the order of the N-M elements in the back

Reverses the order of n elements

Task 18: How to calculate the first n data of a sequence

Each element in a positive integer sequence Q can at least be divisible by a positive integer A and B, and now given A and B, how do you calculate the first few items in Q? For example, when a=3,b=5,n=6, the sequence 3,5,6,9,10,12. can be associated with the merge, given two arrays A, B, array A is stored: 3*1,3*2,3*3 ... array b is stored 5*1,5*2,5*3 ... there are two pointers I, J respectively Point A, The first element of B, take min (A[i],b[j]) and move the pointer of the smaller value, and then continue the comparison.

Extension: The number of ugly, can only contain the number of factors 2 3 5.

Data structures and algorithms-arrays

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.