LeetCode- array of topics

27. Remove Element Removes the specified element

https://leetcode.com/problems/remove-element/

Title: nums given array values ​​and val, to remove all instances of that value and returns the new length. Do not allocate additional space for another array, you must (1) place extra memory to modify the input array by using O to achieve this. You can change the order of the elements, in addition to the new length, leaving what is not important.

Ideas: the need for a variable count, then traverse the original array, and if the current value is different from a given value, we leave the current value of the count variable position covering, and the count variable is incremented.

 

41. First Missing Positive lost smallest positive integer

https://leetcode.com/problems/first-missing-positive/

Title: integer array to a given unsorted, find lost smallest positive integer.

Ideas: The first idea is that all the elements into the HashSet, from the beginning to find the missing smallest positive integer. The second idea is

 

48. Rotate Image rotate image

http://oj.leetcode.com/problems/rotate-image/

Title: n × n to represent a two-dimensional matrix image. The image is rotated 90 degrees (clockwise).

Ideas: a method, a group of four atoms to rotate. First, determine the new position after the first rotational position, the new position is referred to the original position after the rotation number of the third number, the third position after the rotation number of the original number of the fourth, while the fourth after the number of rotations of the initial position of the first number, the order number of every four rotation cycles. The second method is first inverted from the diagonal axis, the x-axis center line and then turned upside down to get the results.

 

Spiral 54. Spiral Matrix Matrix

https://leetcode.com/problems/spiral-matrix/

To a matrix (m row, n-column) given the m × n elements, spiral sequence returns all elements of the matrix.

Matrix Input two-dimensional array of m rows and n columns, row and last referred to index and rowStart rowEnd, end to end and the index column is colStart colEnd, four steps to traverse the matrix:

1. From right to colEnd colStart traversal, after traversing rowStart self-energizing;

2. rowEnd to traverse downwardly from rowStart, after traversing colEnd decremented;

3. If the condition rowStart <= rowEnd traverse to the left, after traversing rowEnd decremented;

4. If the condition colStart <= colEnd the upward traverse, after traversing colStart self-energizing;

5. If the condition rowBegin <= rowEnd && colBegin <= colEnd above process is repeated.

 

Rotation matrix 59. Spiral Matrix II II

https://leetcode.com/problems/spiral-matrix-ii/

Title: Given a positive integer n, generated from a 1 to a n- 2 matrix elements of the square spiral filled sequentially.

Ideas: rotation matrix and 54. Similarly, a process corresponding to inverse operation.

 

88. Merge Sorted Array mixed ordered array

https://leetcode.com/problems/merge-sorted-array/

Title: Given two integers ordered arrays nms 1 and nms 2, the nms 1 nums 2 and combined into an ordered array.

Idea: As the two arrays are ordered, all as long as you can compare the size of the order. The title says the nums1 array has enough space, that we do not resize arrays, gave us m and n, then know the size of the array after mixing, so we started one by one from the end of the array nums1 and nums2 comparing, after the end of the array of the larger number, in order from the forward mixing was added. Need three variables i, j, k, respectively, points to the end nums1, nums2, and mixing of the array. For while loop, if i and j are greater than 0, if the look nums1 [i]> nums2 [j], to be described first nums1 [i] array at the end of mixing was added, and after addition of k 1 i to be decremented; Conversely put nums2 [j] added to the mixture at the end of the array, after addition of j and k must be 1 decremented. After the end of the cycle, it is possible also i or j is greater than or equal to 0, if j is greater than 0, then we also need to continue the cycle, will continue to copy into the digital nums2 in nums1. If i is greater than or equal to 0, then no tube, since the mixing of the array itself in nums1.

 

Guess you like

Origin www.cnblogs.com/nomad1c/p/11355220.html