/ * *

11. Container that holds the most water

The title

Give you n non-negative integers A1, A2… Each number represents a point in the coordinates (I, ai). Draw n vertical lines in the coordinates, and the two endpoints of vertical line I are (I, AI) and (I, 0). Find the two lines that together with the X-axis will hold the most water.

Note: You cannot tilt the container.

Example 1:

,8,6,2,5,4,8,3,7 input: [1] output: 49 explanation: the figure in the vertical line represents the input array,8,6,2,5,4,8,3,7 [1]. In this case, the maximum amount of water the container can hold (shown in blue) is 49. Example 2:

Input: height = [1,1]

Input: height = [4,3,2,1,4] Output: 16

Input: height = [1,2,1

Tip:

n = height.length

2 <= n <= 3 * 104

0 <= height[i] <= 3 * 104

Source: LeetCode link: leetcode-cn.com/problems/co… Copyright belongs to the Collar buckle network. Commercial reprint please contact official authorization, non-commercial reprint please indicate the source.

The test code

Print (maxArea (,8,6,2,5,4,8,3,7 [1]))

notes

So this is a classic two-pointer problem and the idea is you move the two sides to the middle and you move the smaller one and you get the current capacity each time and then you get the maximum capacity

The code address

Github.com/zmfflying/Z… * /

The problem solving code

Import Foundation func maxArea(_ height: [Int]) -> Int {var maxArea: Int = 0 var left: Int = 0 var right: Int = height.count - 1 while left < right { let leftHeight: Int = height[left] let rightHeight: Int = height[right] Int = min(leftHeight, rightHeight) * (right-left) maxArea = Max (maxArea, If leftHeight < rightHeight {left += 1} else {right -= 1}} return maxArea}Copy the code

/ * *

26. Remove duplicates from sorted array

The title

Given a sorted array, you need to remove duplicate elements in place so that each element appears only once, returning the new length of the removed array.

Instead of using extra array space, you must modify the input array in place and do so with O(1) extra space.

Example 1:

Given array nums = [1,1,2],

The function should return a new length of 2, and the first two elements of the original nums array should be changed to 1, 2.

You don’t need to worry about the element after the new length in the array. Example 2:

Given nums = [0,0,1,1,1,2,2,3,3,4],

The function should return a new length of 5, and the first five elements of the original nums array should be changed to 0, 1, 2, 3, 4.

You don’t need to worry about the element after the new length in the array.

Description:

Why is the return value an integer, but the output answer is an array?

Note that the input array is passed “by reference,” which means that modifying the input array in a function is visible to the caller.

You can imagine the internal operation as follows:

// Nums is passed by reference. That is, it does not make any copies of the arguments int len = removeDuplicates(nums);

// Modifying the input array in a function is visible to the caller. // Depending on the length returned by your function, it prints out all elements in the array within that length. for (int i = 0; i < len; i++) { print(nums[i]); }

Source: LeetCode link: leetcode-cn.com/problems/re… Copyright belongs to the Collar buckle network. Commercial reprint please contact official authorization, non-commercial reprint please indicate the source.

The test code

Var nums = [0,0,1,1, 2,2,3,3] print(duplicates (&nums), nums)

notes

So we’re going to use the idea of a fast or slow pointer because they’re saying as long as the previous data is correct, and it’s an ordered array then we can create a fast or slow pointer and the slow pointer points to the 0th element and the fast pointer points to the first element

Then move the slow pointer backward when the fast pointer is not equal to the slow pointer

The code address

Github.com/zmfflying/Z… * /

The problem solving code

import Foundation func removeDuplicates(_ nums: Inout [Int]) -> Int {if nums.count == 0 {return 0} var slow = 0 var fast = 1 while fast < nums.count { // If nums[slow]! = nums[fast] {slow += 1 nums[fast]} return slow + 1}Copy the code

/ * *

283. Move the zero

The title

Given an array nums, write a function to move all zeros to the end of the array while preserving the relative order of the non-zero elements.

Example:

Input: [0,1,0,3,12] output: [1,3,12,0,0] description:

Must operate on the original array, cannot copy additional arrays. Minimize the number of operations.

Source: LeetCode link: leetcode-cn.com/problems/mo… Copyright belongs to the Collar buckle network. Commercial reprint please contact official authorization, non-commercial reprint please indicate the source.

The test code

Var nums = [0,1,0,3,12] print(num)

notes

0 < left must be non-0. Left <= right must be 0

And the way you do that is when you go through the loop, you move left and right when you see something that’s not zero and you switch around and you move to the right when you see something that’s not zero

The code address

Github.com/zmfflying/Z… * /

The problem solving code

import Foundation func moveZeroes(_ nums: Inout [Int]) {if nums.count <= 1 {return} //left <= 0 //left <= right = 0 var left = 0 var right = 0 while Count {//right < nums.count {//right < nums.count {// Right < nums.count {// Right < nums.count} = 0 { if left ! = right { // let temp = nums[left] // nums[left] = nums[right] // nums[right] = temp // left ! Nums [left] = 0 nums[left] = 0 nums[right] = 0 nums[left] = 0 nums[right] = 0 nums[left] = 0 nums[right] = 0 nums[left] = 0 nums[right] = 0 nums[left] = 0 nums[right] = 1}Copy the code