Leetcode
1. Two Sum
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
Example:
|
|
思路
题意是让你从给定的数组中找到两个元素的和为指定值的两个索引,利用HashMap作为存储,键为目标值减去当前元素值,索引为值,比如i = 0时,此时首先要判断nums[0] = 2是否在map中,如果不存在,那么插入键值对key = 9 - 2 = 7, value = 0,之后当i = 1时,此时判断nums[1] = 7已存在于map中,那么取出该value = 0作为第一个返回值,当前i作为第二个返回值。
|
|
26. Remove Duplicates from Sorted Array
Given a sorted array, 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.
For example,
Given input array nums = [1,1,2],
Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. It doesn’t matter what you leave beyond the new length.
思路
题意是让你从一个有序的数组中移除重复的元素,并返回之后数组的长度。我的思路是:首先判断如果数组长度等于0的话直接返回原长度即可,否则的话遍历一遍数组,用一个index变量指向尾部,如果后面的元素和前面的元素不同,就让index变量加1,最后返回index。
|
|
27. Remove Element
Given an array and a value, 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.
Example:
|
|
思路
题意是移除数组中值等于val的元素,并返回之后数组的长度,并且题目中指定空间复杂度为O(1),我的思路是用index标记尾部,遍历该数组时当索引元素不等于val时,index加一,尾部指向当前元素,最后返回index即可。
|
|
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.
思路
题意是让你从一个旋转过后的递增序列中寻找给定值,找到返回索引,找不到返回-1,我们在下面这组数据中寻找规律。
|
|
由于是旋转一次,所以肯定有一半及以上的序列仍然是具有递增性质的,我们观察到如果中间的数比左面的数大的话,那么左半部分序列是递增的,否则右半部分就是递增的,那么我们就可以确定给定值是否在递增序列中,从而决定取舍哪半边。
|
|
15. 3Sum
Given an array S of n integers, are there elements a, b, c in S 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.
|
|
思路
题意是让你从数组中找出所有三个和为0的元素构成的非重复序列,这样的话我们可以把数组先做下排序,然后遍历这个排序数组,用两个指针分别指向当前元素的下一个和数组尾部,判断三者和与0的大小来移动两个指针,其中细节操作就是注意去重。
|
|
16. 3Sum Closest
Given an array S of n integers, find three integers in S such that the sum is closest to a given number, target. Return the sum of the three integers. You may assume that each input would have exactly one solution.
|
|
思路
题意是让我们求最接近给定值的三数之和,是在之前那道3Sum三数之和的基础上又增加了些许难度,那么这道题让我们返回这个最接近于给定值的值,即我们要保证当前三数和跟给定值之间的差的绝对值最小,所以我们需要定义一个变量A用来记录差的绝对值,然后我们还是要先将数组排个序,然后开始遍历数组,思路跟那道三数之和很相似,都是先确定一个数,然后用两个指针left和right来滑动寻找另外两个数,每确定两个数,我们求出此三数之和,然后算和给定值的差的绝对值存在newA中,然后和A比较并更新A和结果closest即可。
|
|
53. Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest sum.
For example, given the array [-2,1,-3,4,-1,2,1,-5,4],
the contiguous subarray [4,-1,2,1] has the largest sum = 6.
思路
题意是求数组中子数组的最大和,利用动态规划的方法去求解。当部分序列和大于0的话就一直加下一个元素,并和当前最大值进行比较,如果出现部分序列小于0,那就需要从当前元素算起。
|
|
38. Count and Say
The count-and-say sequence is the sequence of integers with the first five terms as following:
|
|
1 is read off as "one 1" or 11.11 is read off as "two 1s" or 21.21 is read off as "one 2, then one 1" or 1211.
Given an integer n, generate the nth term of the count-and-say sequence.
Note: Each term of the sequence of integers will be represented as a string.
Example 1:
|
|
Example 2:
|
|
21. Merge Two Sorted Lists
Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists.
思想
题意是用一个新链表来合并两个已排序的链表,那我们只需要从头开始比较已排序的两个链表,新链表指针每次指向值小的节点,依次比较下去,最后,当其中一个链表到达了末尾,我们只需要把新链表指针指向另一个没有到末尾的链表此时的指针即可。
|
|
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.
思路
木桶的容积取决于短板的高度。首先计算最左边(left)和最右边(right)组成的木桶的面积,并把它设置为maxArea。在left<right的情况下,首先判断height[left]和height[right]值的大小,若height[left]<height[right],则height取值向右边移动,同时计算每移动一步后新的Area的面积并与maxArea做比较,得出新的最大面积;反之,则height取值向左边移动,重复上一步操作。最终得到最大的区域积。
|
|
14. Longest Common Prefix
Write a function to find the longest common prefix string amongst an array of strings.
思路
题意是编写一个函数来查找字符串数组中最长的公共前缀字符串。利用纵向扫描,对每一个位置比较所以字符串,直到遇到一个不匹配。
|
|
9. Palindrome Number
Determine whether an integer is a palindrome. Do this without extra space.
Some hints:
Could negative integers be palindromes? (ie, -1)
If you are thinking of converting the integer to string, note the restriction of using extra space.
You could also try reversing an integer. However, if you have solved the problem “Reverse Integer”, you know that the reversed integer might overflow. How would you handle such case?
There is a more generic way of solving this problem.
思路
题意是判断一个有符号整型数是否是回文,也就是逆序过来的整数和原整数相同,首先负数肯定不是,接下来我们直接算出他的回文数,然后和给定值比较即可。
|
|
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.
Example 1:
|
|
Example 2:
|
|
Example 3:
|
|
Example 1:
|
|
思路
题意是给定一个有序数组和一个目标值,如果找到目标,则返回索引。如果没有,则返回按顺序插入的索引。解题思路:首先遍历数组,若当前数字大于或等于目标值,则返回当前坐标,如果遍历结束了,说明目标值比数组中任何一个数都要大,则返回数组长度n即可。
|
|
48. Rotate Image
You are given an n x n 2D matrix representing an image.
Rotate the image by 90 degrees (clockwise).
Note:
You have to rotate the image in-place, which means you have to modify the input 2D matrix directly. DO NOT allocate another 2D matrix and do the rotation.
Example 1:
|
|
Example 2:
|
|
思路
题意是给定一个n*n个2维矩阵来表示一个图。在原矩阵上旋转图形90°。主要思路是,首先沿着矩阵副对角线翻转一次,然后沿着水平线翻转一次。
|
|
34. Search for a Range
Given an array of integers 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].
For example,
Given [5, 7, 7, 8, 8, 10] and target value 8,
return [3, 4].
思路
题意是给定按升序排序的整数数组,找到给定目标值的开始和结束位置。其中算法的运行时复杂度必须按照O(log n)的顺序。如果在数组中找不到目标,则返回[-1,-1]。首先,建立二元结果数组A,左边left,右边right。二分法求左边界:当中点小于target,left移向中点,否则right移向中点;先判断左边,再判断右边是否等于target,如果是,赋值给A[0]。二分法求右边界:当中点大于target,right移向中点,否则left移向中点;先判断右边,再判断左边是否等于target,如果是,赋值给A[1]。返回A。
|
|
66. Plus One
Given a non-negative integer represented as a non-empty array of digits, plus one to the integer.
You may assume the integer do not contain any leading zero, except the number 0 itself.
The digits are stored such that the most significant digit is at the head of the list.
思路
题意是给定一个数组表示非负整数,其高位在数组的前面,对这个整数加1。使用大数加法,遍历数组的每一位,同时处理进位,如果最后还有进位,则在数组最前面插入1即可。
|
|
121. Best Time to Buy and Sell Stock
Say you have an array for which the $i^{th}$ element is the price of a given stock on day i.
If you were only permitted to complete at most one transaction (ie, buy one and sell one share of the stock), design an algorithm to find the maximum profit.
Example 1:
|
|
Example 2:
|
|
思路
题意是假设有一个数组,数组中的元素是第i天给定股票的价格,如果最多只能完成最多一笔交易(买入或者卖出一股股票),设计一个算法来找到最大的利润值。首先初始化利润值为0,假定当前的最小值cur_min为数组中的第一个数值,遍历当前数组,取初始化利润值与prices[i]-cur_min中较大值作为新的profit,取cur_min和prices[i]中较小的值作为新的cur_min,迭代最终得到最大的profit。
|
|
122. Best Time to Buy and Sell Stock II
Say you have an array for which the $i^{th}$ element is the price of a given stock on day i.
Design an algorithm to find the maximum profit. You may complete as many transactions as you like (ie, buy one and sell one share of the stock multiple times). However, you may not engage in multiple transactions at the same time (ie, you must sell the stock before you buy again).
思路
题意是设计一个算法来找到最大的利润,可以根据需要完成尽可能多的交易(即多次买入和卖出一次股票)。但是不可以同时进行多笔交易(即您必须在再次购买之前出售股票)。因为买一次接着卖一次,所以只要计算买卖一次的利润值,对利润值大于0的进行求和。
|
|
118. Pascal’s Triangle
Given numRows, generate the first numRows of Pascal’s triangle.
For example, given numRows = 5,
Return
|
|
思路
题意是生成帕斯卡三角形,我们可以发现,从第三行开始,该行中的值等于上一行的左上角和右上角之和,利用这个规律进行求解。
|
|
119. Pascal’s Triangle II
Given an index k, return the $k^th$ row of the Pascal’s triangle.
For example, given k = 3, Return [1,3,3,1].
思路
题意是生成帕斯卡三角形中的某一列。
|
|
39. Combination Sum
Given a set of candidate numbers (C) (without duplicates) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.
The same repeated number may be chosen from C unlimited number of times.
Note:
- All numbers (including target) will be positive integers.
- The solution set must not contain duplicate combinations.
For example, given candidate set [2, 3, 6, 7] and target 7,
A solution set is:
|
|
思路
题意:给定一个正数集合和一个目标数,求出所有的子集合,使子集的和为target,要求子集合递增排列,且不包含重复的子集合,子集合中的数字可以重复。我们需要写一个递归函数,在这个函数中需要添加三个变量,result记录最终的结果,path记录中间的一个解,start记录当前递归到的下标。调用递归函数时,target要减去当前数组的数,为了减小计算量,进行相应的剪枝处理,即大于target,就减去。数组中的那个值。
|
|
40. Combination Sum II
Given a collection of candidate numbers (C) and a target number (T), find all unique combinations in C where the candidate numbers sums to T.
Each number in C 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.
For example, given candidate set [10, 1, 2, 7, 6, 1, 5] and target 8,
A solution set is:
|
|
思路
题意:给定一个正数集合和一个目标数,求出所有的子集合,使子集的和为target,要求子集合递增排列,且不包含重复的子集合,子集合中的数字也不可以重复。与上一个题目差不多,需要加一个判断,如果上一轮循环已经使用candidates[i],本次循环就不能再选candidates[i],确保candidates[i]最多循环一次。
|
|