1、删除元素
#坑:1、由于要求在原来数组上删除所以是不能创建一个新的列表记录不等于目标的数值。
# 2、是不能正向删除的,使用反向删除
def removeElement(A, elem):
# write your code here
j = len(A) - 1
while j >= 0:
if A[j] == elem:
A.pop(j)
j -= 1
return len(A)
2.子数组之和
#思路:最容易想到的是,遍历所有子串返回和为0的。这种解法复杂度为O(n2)不理想
#进阶解法:用一个字典记录,到当前位置前面所有子串的和,当出现两个和一样,或者有某个为0的时候,就可以知道之间就存在和为0的子串。
def subarraySum(self, nums):
# write your code here
if nums == [0]:return [0,0] #坑!
d = {nums[0]: 0}
pre_sum = nums[0]
for i in range(1, len(nums)):
now_sum = pre_sum + nums[i]
pre_sum = now_sum
if now_sum == 0:
return [0, i]
elif now_sum in d:
return [d[now_sum]+1, i]
else:
d[now_sum] = i
3.删除排序数组中的重复数字
思路:倒着删除,当后一项和前一项相等时,删除后一项即可。
class Solution:
"""
@param: nums: An ineger array
@return: An integer
"""
def removeDuplicates(self, nums):
# write your code here
if len(nums) == 1:return len(nums)
for i in range(len(nums) - 1, -1, -1):
if nums[i] == nums[i - 1]:
nums.pop(i)
return len(nums)
其实在做这道题的时候,刚开始眼瞎了,没看到有序!然后就当乱序的来做,下面是乱序的解法(上面算是这里的一种特殊情况)
def fun(nums):
from collections import Counter
c = Counter(nums)
for i in range(len(nums) - 1, -1, -1):
if c[nums[i]] > 1:
c[nums[i]] -= 1
nums.pop(i)
return len(nums)
4、合并排序数组
def mergeSortedArray(self, A, m, B, n):
# write your code here
A[m:m+n] = B[:n]
A[:m+n] = sorted(A[:m+n])
5、两数之和
思路:建立一个字典,用于存储已经遍历过的元素的值和索引,当目标值与当前值的差已经遍历过,即返回。
class Solution:
"""
@param numbers: An array of Integer
@param target: target = numbers[index1] + numbers[index2]
@return: [index1, index2] (index1 < index2)
"""
def twoSum(self, numbers, target):
# write your code here
dic={}
for i in range(len(numbers)):
if(target-numbers[i] in dic):
return [dic[target-numbers[i]],i]
dic[numbers[i]]=i
6、数组剔除元素后的乘积
思路:使用两个数组,分别保存当前元素前面左边和右边所有元素的成绩
class Solution:
"""
@param: nums: Given an integers array A
@return: A long long array B and B[i]= A[0] * ... * A[i-1] * A[i+1] * ... * A[n-1]
"""
def productExcludeItself(self, nums):
# write your code here
pre_list = [1]
last_list = [1]
ret = []
for i in range(1, len(nums)):
pre_list.append(pre_list[-1] * nums[i - 1])
for i in range(len(nums) - 2, -1, -1):
last_list.append(last_list[-1] * nums[i + 1])
for i in range(len(nums)):
ret.append(pre_list[i] * last_list[-(i + 1)])
return ret
7.丢失的第一个正整数
class Solution:
"""
@param A: An array of integers
@return: An integer
"""
def firstMissingPositive(self, A):
# write your code here
d = {i: None for i in A}
ret = 1
while 1:
if ret not in d:
return ret
ret += 1
8、最接近的三数和
思路:排序后,利用双指针思想
class Solution:
"""
@param numbers: Give an array numbers of n integer
@param target: An integer
@return: return the sum of the three integers, the sum closest target.
"""
def threeSumClosest(self, numbers, target):
# write your code here
l = len(numbers)
if l == 3: return sum(numbers)
new_list = sorted(numbers)
ret = float('Inf')
for i in range(l - 2):
j = i + 1
k = l - 1
while j < k:
new_sum = new_list[i] + new_list[j] + new_list[k]
if new_sum == target:
return new_sum
elif new_sum < target:
j += 1
else:
k -= 1
if abs(ret - target) > abs(new_sum - target):
ret = new_sum
return ret
9、数组划分
思路:快排思想
class Solution:
"""
@param nums: The integer array you should partition
@param k: An integer
@return: The index after partition
"""
def partitionArray(self, nums, k):
# write your code here
i = 0
j = len(nums) - 1
while i <= j:
if nums[i] >= k and nums[j] < k:
nums[i], nums[j] = nums[j], nums[i]
i += 1
j -= 1
elif nums[i] < k:
i += 1
else:
j -= 1
return i