problem 16,17,18,19,20
16. 3Sum Closest
Medium
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:
1 | |
对于这种问题,要减少复杂度,排序是必须的
重要的是如何利用排序之后的结果
一个简单的思路是暴力破解,这样的话排序就是没有必要的了,复杂度为$O(n^3)$
1 | |
17. Letter Combinations of a Phone Number
Medium
Given a string containing digits from 2-9 inclusive, return all possible letter combinations that the number could represent.
A mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters.
Example:
1 | |
1 | |
18. 4Sum
Medium
Given an array nums of n integers and an integer target, are there elements a, b, c, and d in numssuch that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.
Note:
The solution set must not contain duplicate quadruplets.
Example:
1 | |
字典查询,排序加速
1 | |
19. Remove Nth Node From End of List
Medium
Given a linked list, remove the n-th node from the end of list and return its head.
Example:
1 | |
Note:
Given n will always be valid.
Follow up:
Could you do this in one pass?
快慢指针
1 | |
20. Valid Parentheses
Easy
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid.
An input string is valid if:
- Open brackets must be closed by the same type of brackets.
- Open brackets must be closed in the correct order.
Note that an empty string is also considered valid.
Example 1:
1 | |
Example 2:
1 | |
Example 3:
1 | |
Example 4:
1 | |
Example 5:
1 | |
堆栈的运用
1 | |