该笔记只为个人所写算法,不一定是最优解法,仅供参考
[1] Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
难度:Easy (42.36%)
考点:哈希表
思路:用一遍循环 一边向哈希表中存值,一边比较判断
1 | var twoSum = function(nums, target) { |
[11] Container With Most Water
Given n non-negative integers a1, a2, …, an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.
Note: You may not slant the container and n is at least 2.
难度:Medium (42.93%)
考点:动态规划
思路:
- 设定 i,j 分别指向数组的头和尾
- 比较 i,j 所对应的位置的值,值较小的那一个移动(i++或 j–)
1 | var maxArea = function(height) { |
[15] 3Sum
Given an array nums of n integers, are there elements a, b, c 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.
难度:Medium (23.55%)
思路:
- 数组排序(升序)
- 设定三个指针,最外层循环从 0 开始,到数组的尾结束(i=0)
- 第二层循环,一个指向上一个指针的下一个元素(j=i+1),另一个指向数组的尾部(k=nums.length-1)
- 如果三个元素之和等于 0,则 push 进要返回的数组中;如果三个元素之和大于 0,说明第三个指针指向的元素过大,则第三个指针向前移(k–);如果三个元素之和小于 0,说明第二个指针指向的元素过小,则第二个指针向后移(j++);
1 | var threeSum = function(nums) { |
[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.
难度:Medium (41.40%)
思路:与上题类似
1 | var threeSumClosest = function(nums, target) { |
[18] 4Sum
Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums such 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.
难度:Medium (29.83%)
思路:思路同 3Sum,多一层循环。注意跳过相同的数(最外两层的循环变量)
1 | var fourSum = function(nums, target) { |
[26] Remove Duplicates from Sorted Array
Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length.
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
难度:Easy (39.80%)
思路:两个指针,一个指针负责寻找和后一个不相等的数,另一个指针负责一步步向后移去重。
1 | var removeDuplicates = function(nums) { |
[27] Remove Element
Given an array nums and a value val, remove all instances of that value in-place and return the new length.
Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.
The order of elements can be changed. It doesn’t matter what you leave beyond the new length.
难度:Easy (43.73%)
思路:找到和 val 值相等的位置,将数组最后一个元素赋值过来(去掉这个 val,数组长度减一)
1 | var removeElement = function(nums, val) { |
[31] Next Permutation
Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers.
If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order).
The replacement must be in-place and use only constant extra memory.
Here are some examples. Inputs are in the left-hand column and its corresponding outputs are in the right-hand column.
1,2,3 → 1,3,2
3,2,1 → 1,2,3
1,1,5 → 1,5,1
难度:Medium (30.09%)
思路:
- 从后向前比较相邻的两个元素,直到前一个元素小于后一个元素,停止(i)。
- 若已经没有了前一个元素(i=0),则该序列为递减序列,没有 Next Permutation。按照题目要求,直接反转序列。
- 前一个元素(j=i-1)小于后一个元素(i),找到前一个元素(j)要交换的元素,从 i 的后一个元素开始往后查找,找到最后一个比“前一个元素(j)”大的元素(k),也就是再往后的元素,就比元素 j 小了。交换 j 和 k 元素。
- 从 i 开始,包括 i 到序列的尾部,反转。
则得出的即是 Next Permutation
1 | var nextPermutation = function(nums) { |
[33] Search in Rotated Sorted Array
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand.(i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]).
You are given a target value to search. If found in the array return its index, otherwise return -1.
You may assume no duplicate exists in the array.
Your algorithm’s runtime complexity must be in the order of O(log n).
难度:Medium (32.68%)
考点:二分法
注意:判断和循环的边界条件
1 | var search = function(nums, target) { |
[34] Find First and Last Position of Element in Sorted Array
Given an array of integers nums sorted in ascending order, find the starting and ending position of a given target value.
Your algorithm’s runtime complexity must be in the order of O(log n).
If the target is not found in the array, return [-1, -1].
难度:Medium (33.06%)
考点:二分法
注意:判断和循环的边界条件
1 | var searchRange = function(nums, target) { |
[35] Search Insert Position
Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.
You may assume no duplicates in the array.
难度:Easy (40.50%)
1 | var searchInsert = function(nums, target) { |
[39] Combination Sum
Given a set of candidate numbers (candidates) (without duplicates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target.
The same repeated number may be chosen from candidates unlimited number of times.
Note:All numbers (including target) will be positive integers.The solution set must not contain duplicate combinations.
难度:Medium (46.97%)
考点:递归
1 | var combinationSum = function(candidates, target) { |
[40] Combination Sum II
Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sums to target.
Each number in candidates may only be used once in the combination.
Note:All numbers (including target) will be positive integers.The solution set must not contain duplicate combinations.
难度:Medium (40.37%)
考点:递归
注意:限制边界条件,过滤重复的结果
1 | var combinationSum2 = function(candidates, target) { |