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:
Given nums = [2, 7, 11, 15], target = 9, Because nums[0] + nums[1] = 2 + 7 = 9, return [0, 1].
题目地址:https://leetcode.com/problems/two-sum/description/
题意:给定一个整形数组和一个目标,返回数组中相加等于目标的两个数的下标
思路:创建一个hash表,枚举每一个数n,把数值作为key,下标作为values,遍历时找target-n
python
class Solution(object): def twoSum(self, nums, target): """ :type nums: List[int] :type target: int :rtype: List[int] """ d ={} for i, n in enumerate(nums): m = target - n if m in d: return[d[m],i] else: d[n] = 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.
Example:
Given 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.
题目地址:https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/
题意:给定一个已经排序好的数组,用O(1)的空间在原数组去掉重复的数,返回新的长度
思路:利用双指针,一个指向当前不重复的长度len,一个指向遍历看不重复的写到len
python
class Solution(object): def removeDuplicates(self, nums): """ :type nums: List[int] :rtype: int """ if nums == []: return 0 index = 0 for i in range(1,len(nums)): if nums[index] != nums[i]: index += 1 nums[index] = nums[i] return index+1
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:
Given nums = [3,2,2,3], val = 3, Your function should return length = 2, with the first two elements of nums being 2.
题目地址:https://leetcode.com/problems/remove-element/description/
题意:给定一个数组和目标,在原位将数组中的目标值删除,返回新的长度
思路:双指针
python
class Solution(object): def removeElement(self, nums, val): """ :type nums: List[int] :type val: int :rtype: int """ j = 0 for i in range(len(nums)): if nums[i] != val: nums[j] = nums[i] j += 1 return j
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:
Input: [1,3,5,6], 5 Output: 2
Example 2:
Input: [1,3,5,6], 2 Output: 1
Example 3:
Input: [1,3,5,6], 7 Output: 4
Example 1:
Input: [1,3,5,6], 0 Output: 0
题目地址:https://leetcode.com/problems/search-insert-position/description/
题意:给定一个数组和target,如果在数组里找到target,返回target的下标,如果找不到返回插入的位置
思路:二分法
python
class Solution(object): def searchInsert(self, nums, target): """ :type nums: List[int] :type target: int :rtype: int """ start, end = 0, len(nums) while start < end: mid = (start+end) / 2 if nums[mid] < target: start = mid+1 else: end = mid return start
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
.
题目地址:https://leetcode.com/problems/maximum-subarray/description/
题意:找出连续子数组的最大和
思路:动态规划问题
python
class Solution(object): def maxSubArray(self, nums): """ :type nums: List[int] :rtype: int """ ans, sum = nums[0], 0 for x in nums: sum += x if sum > ans: ans = sum if sum < 0: sum = 0 return ans
作者:huangqiancun
出处:http://www.cnblogs.com/huangqiancun/
本博客若无特殊说明则由作者原创发布,欢迎转载,但请注明出处 :)