LeetCode 16. 3Sum Closest

Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution.

Example:

思路:和15题差不多,先排序,然后定好三个数中一个数,再搜索剩下两个数。

 

LeetCode 15. 3Sum

Given an array nums of n integers, are there elements abc in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.

Note:

The solution set must not contain duplicate triplets.

Example:

思路:目标是搜索一个三元组,先把所有的数排序,然后定好一个数,再搜索剩下的两个数。
两个指针分别从左右开始搜索:
1、若三个数之和为0,则搜索到,同时向中间移动;
2、若三个数之和小于0,则把左边的指针向右移动,使得和增加;
3、若三个数之和大于0,则把右边的指针向左移动,使得和减小;

 

LeetCode 14. Longest Common Prefix

Write a function to find the longest common prefix string amongst an array of strings.

If there is no common prefix, return an empty string "".

Example 1:

Example 2:

Note:

All given inputs are in lowercase letters a-z.