API
python:
列表:
-
数组长度 len(array)
-
添加元素
- 在末尾插入一个元素 list.append(e)
- 将列表元素(或任何可迭代的元素)添加到当前列表的末尾 list.extend(anotherlist)
- 在指定位置添加元素 list.insert(position,element)
-
删除元素
- 按索引:pop(n)
- 按值:remove(value) 仅删除首次出现的指定值。
- 删除列表中的所有元素 list.clear()
-
返回列表副本 list.copy()
-
返回具有指定值的元素数量 list.count(value)
-
颠倒列表顺序 list.reverse()
-
返回具有指定值的第一个元素的索引 list.index(value)
-
对列表进行排序 list.sort(reverse=True|False, key=myFunc) reverse=True是对列表进行降序排列,默认是False;key是指定排序标准的函数
C++:
STL中的向量和表:
(对所有STL容器通用)
- 返回元素个数 size()
- 删除所有元素 clear()
对Vector和list都适用
- 容器尾部加入一个元素 push_back()
- 移除容器最后一个元素 pop_back()
- 返回表末尾对象 back()
- 返回表前端对象 front()
仅对list有效
- push_front(value) 在前端添加
- pop_front() 删除list前端对象
仅对vector有效
-
operator[](idx) 返回vector中idx索引位置的对象
-
at() 返回vector中idx索引位置的对象
-
capcity() 返回容器内部容量
-
容器是否为空 empty()
-
交换值 swap()
-
erase_if()/remove()/remove_if()
迭代器
获得迭代器:
- iterator begin()返回指向容器的第一项的一个适当的迭代器
- iterator end()返回指向容器的终止标志
迭代器操作:
- iter++;++iter
- iter1==iter2;iter1!=iter2
- *iter
需要迭代器的容器操作:
- iterator insert(iratetor pos,const Object &x) 添加x到表中迭代器pos所指向的位置之前的位置。
- iterator erase(iterator pos) 删除迭代器所给出位置的对象
- iterator erase(iratetor start,iterator end)删除从start开始,到end结束的所有元素
Leetcode 数组题
直接用hash表把每个值作为key,把他们的索引存起来,从头开始,将访问过的值存进hash表,并检查hash表中有没有target-nums[i],有就直接返回索引了。
参考代码:
hashset={}
for i in range(len(nums)):
if hashset.get(target-nums[i]) is not None :
return [hashset.get(target-nums[i]),i]
hashset[nums[i]]=i
双指针!
先把数组排序。用for循环遍历first,先固定first,再用两个指针,一个从first+1的位置,一个从数组的末尾,将两个元素的和和target比较。固定first+1的位置,如果两个元素的和大于target,将最右边的指针往左移,如此循环。
官方题解:
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
n = len(nums)
nums.sort()
ans = list()
# 枚举 a
for first in range(n):
# 需要和上一次枚举的数不相同
if first > 0 and nums[first] == nums[first - 1]:
continue
# c 对应的指针初始指向数组的最右端
third = n - 1
target = -nums[first]
# 枚举 b
for second in range(first + 1, n):
# 需要和上一次枚举的数不相同
if second > first + 1 and nums[second] == nums[second - 1]:
continue
# 需要保证 b 的指针在 c 的指针的左侧
while second < third and nums[second] + nums[third] > target:
third -= 1
# 如果指针重合,随着 b 后续的增加
# 就不会有满足 a+b+c=0 并且 b<c 的 c 了,可以退出循环
if second == third:
break
if nums[second] + nums[third] == target:
ans.append([nums[first],nums[second],nums[third]])
return ans
作者:LeetCode-Solution
链接:https://leetcode-cn.com/problems/3sum/solution/san-shu-zhi-he-by-leetcode-solution/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
仍然是双指针!
还是先对数组排序。先for循环遍历整个数组,每次循环中,指定second指针指向first+1的位置,third指针指向数组最后一个元素,和target比较,如果大于target就右边的往左移,如果小于target就左边往右移。将三个位置的元素之和和target的差与之前得到的最好的和和target的差比较,更新一下最好的结果。
优化:在比较前还可计算出当固定first时可以得到的三数之和的最大值与最小值,如果最大值小于target,可以跳过这一循环,如果最小值大于target,那么这个就是最终答案了,直接结束循环。
官方题解:
class Solution:
def threeSumClosest(self, nums: List[int], target: int) -> int:
n=len(nums)
nums.sort()
ans=float('inf')
for first in range(n-2):
if first>0 and nums[first]==nums[first-1]:
continue
second,third=first+1,n-1
max_sum=nums[first]+nums[-2]+nums[-1]
min_sum=nums[first]+nums[first+1]+nums[first+2]
if max_sum<=target:
if abs(max_sum-target)<abs(ans-target):
ans=max_sum
continue
elif min_sum>=target:
if abs(min_sum-target)<abs(ans-target):
ans=min_sum
break
while second<third:
tow_sum_target=target-nums[first]
s=nums[second]+nums[third]
if abs(s+nums[first]-target)<abs(ans-target):
ans=s+nums[first]
if s<tow_sum_target:
second+=1
while second<third and nums[second]==nums[second-1]:
second+=1
elif s>tow_sum_target:
third-=1
while second<third and nums[third]==nums[third+1]:
third-=1
else:
return target
return ans
还是按照三数之和的思路,用两个for循环,用first和second遍历,每个循环中,固定first和second,看情况移动third和fourth。
优化:在first那一层的循环中先计算出最大值和最小值,如果最大值小于target,可以跳过这一循环,如果最小值大于target,就结束整个循环。在second那一层也做这样的优化。
class Solution:
def fourSum(self, nums: List[int], target: int) -> List[List[int]]:
n=len(nums)
nums.sort()
ans=[]
first=0
while first<n-3:
if nums[first]+3*nums[first+1]>target:
break
if nums[first]+3*nums[-1]<target:
while first<n-4 and nums[first]==nums[first+1]:first+=1
first+=1
continue
second=first+1
while second<n-2:
if nums[first]+nums[second]+2*nums[second+1]>target:
break
if nums[first]+nums[second]+2*nums[-1]<target:
while second<n-3 and nums[second]==nums[second+1]:second+=1
second+=1
continue
third,fourth=second+1,n-1
two_sum_target=target-nums[first]-nums[second]
while third<fourth:
if nums[third]+nums[fourth]<two_sum_target:
third+=1
elif nums[third]+nums[fourth]>two_sum_target:
fourth-=1
else:
ans.append([nums[first],nums[second],nums[t],nums[fourth]])
third+=1
fourth-=1
while third<fourth and nums[third]==nums[third-1]:
third+=1
while third<fourth and nums[fourth]==nums[fourth+1]:
fourth-=1
while second<n-3 and nums[second]==nums[second+1]:
second+=1
second+=1
while first<n-4 and nums[first]==nums[first+1]:
first+=1
first+=1
return ans