Leetcode One Question of the Day 2020.10.29 Question 15: Sum of Three Numbers

15. Sansanowa

Title description

Insert picture description here

Example

Insert picture description here

Ideas and algorithms

The easiest way to think of is to use a triple loop to traverse the entire array, but this method is too cumbersome. Because the sum of the three numbers is equal to a certain number, when one of the numbers (we set it as the first number here) is determined, the other two numbers are also relatively determined. The larger the second number b, The third number c is required to be smaller, so that it can be implemented by using double pointers in an array arranged in ascending order. Because the triples found in the title cannot be repeated, we can sort the array from small to large first to ensure that the triples satisfy the following relationship: the first number a of the previous triple a <the next triple The first number a', the second and third numbers b, c and so on.

Code

Insert picture description here

Guess you like

Origin blog.csdn.net/m0_51210480/article/details/109393133