https://leetcode-cn.com/problems/4sum/
给定一个包含 n 个整数的数组 nums 和一个目标值 target,判断 nums 中是否存在四个元素 a,b,c 和 d ,使得 a + b + c + d 的值与 target 相等?找出所有满足条件且不重复的四元组。
注意:
答案中不可以包含重复的四元组。
示例:
给定数组 nums = [1, 0, -1, 0, -2, 2],和 target = 0。
满足要求的四元组集合为:
[
[-1, 0, 0, 1],
[-2, -1, 1, 2],
[-2, 0, 0, 2]
]
这题和求 3 数之和的方法一样,只是多加一重循环。时间复杂度为 O(n^3)。
class Solution:
def fourSum(self, nums: List[int], target: int) -> List[List[int]]:
if len(nums) < 4:
return []
nums.sort()
res = set()
for i, iv in enumerate(nums[:-3]):
if i >= 1 and iv == nums[i - 1]:
continue
j = i + 1
while j < len(nums) - 2:
jv = nums[j]
if j > i + 1 and jv == nums[j - 1]:
j += 1
continue
start = j + 1
end = len(nums) - 1
while start < end:
if iv + jv + nums[start] + nums[end] == target:
res.add((iv, jv, nums[start], nums[end]))
start += 1
end -= 1
elif iv + jv + nums[start] + nums[end] > target:
end -= 1
else:
start += 1
j += 1
return list(map(list, res))
https://leetcode-cn.com/problems/3sum/
给定一个包含 n 个整数的数组 nums,判断 nums 中是否存在三个元素 a,b,c ,使得 a + b + c = 0 ?找出所有满足条件且不重复的三元组。
注意:答案中不可以包含重复的三元组。
例如, 给定数组 nums = [-1, 0, 1, 2, -1, -4],
满足要求的三元组集合为:
[
[-1, 0, 1],
[-1, -1, 2]
]
暴力求解,3 重循环,时间复杂度 O(n^3)
把 3 重循环中的一重变成查找哈希表,时间复杂度为 O(n^2)。
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
if len(nums) < 3:
return []
nums.sort()
res = set()
for i, v in enumerate(nums[:-2]):
if i >= 1 and v == nums[i - 1]: # 这个是为了去掉重复的结果
continue
d = {}
for x in nums[i + 1:]:
if x not in d:
d[-v - x] = 1
else:
res.add((v, -v - x, x))
return list(map(list, res))
先排序,然后先遍历第一重循环,剩下的两重循环可以用求两数之和的两头逼近法。
时间复杂度为 O(n^2)。
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
if len(nums) < 3:
return []
nums.sort()
res = set()
for i, v in enumerate(nums[:-2]):
if i >= 1 and v == nums[i - 1]:
continue
start = i + 1
end = len(nums) - 1
while start < end:
if v + nums[start] + nums[end] == 0:
res.add((v, nums[start], nums[end]))
start += 1
end -= 1
elif v + nums[start] + nums[end] > 0:
end -= 1
else:
start += 1
return list(map(list, res))
3 Longest Substring without Repeating
19 Remove Nth Node From End of List
104 Maximum Depth of Binary Tree
116 Populating Next Right Pointers in Each Node
237 Delete Node in a Linked List
303 Range Sum Query - Immutable
345 Reverse Vowels of a String
349 Intersection of Two Arrays
350 Intersection of Two Arrays II
387 First Unique Character in a String
405 Convert a Number to Hexadecimal
720 Longest Word in Dictionary
just type “how to use google”
just type “how to __” in google
you will get you want.
就是加双引号
What it does: searches for an exact phrase
What to type: "one small step for man"
What you'll get: results that include the exact phrase "one small step for man"
在单词前面加上一个小横杆 -,搜索的时候就会排除这个单词
What it does: excludes search results with a particular word or phrase
What to type: bass -fishing
What you'll get: results about bass that are not related to fishing
在单词前面加上波浪号 ~。
What it does: searches for a word and all its synonyms
What to type: ~mobile phone
What you'll get: results with the word "phone" as well as "cell" "cellular" "wireless" etc.
or 关键字
What it does: searches for webpages that include either word
What to type: vacation London OR Paris
What you'll get: results with the word "vacation" and either "London" or "Paris"
查询数字范围
What it does: searches for a range of numbers.
What to type: Willie Mays 1950...1960
What you'll get: results about Willie Mays during this time period
define: 关键字
What it does: defines a word or phrase
What to type: define:plethora
What you'll get: links to definitions of the word "plethora"
我觉得这个最有用了。
使用 site:
What it does: searches only particular websites.
What to type:global warming site:edu
What you'll get: references to global warming found on .edu websites.
输入: golang site:mdgsf.github.io
在我的博客里面查找和 golang 相关的东西。
What it does: searches for webpages that link to a particular website
What to type: link:www.umich.edu
What you'll get: websites that link to the University of Michigan website
What it does: basic calculator functions
What to type: 4 + 7, 30% of 55, 20^2, sqrt(4), etc.
What you'll get: the answer
类型转换:厘米转英寸,温度摄氏度转华氏度,美元转英镑
What it does: converts units of measure
What to type: cm in foot, 28C in F, $ in pound, days in fortnight, miles in league, mph in speed of light, etc.
What you'll get: the converted answer.
菜单栏 ---> Code ---> Reformat Code
快捷键: Ctrl + Alt + L
把鼠标光标移动到要查看的函数上
右键 ---> Go To ---> Declaration
快捷键: Ctrl + B
File —> Settings —> Editor —> File and Code Templates
然后在右边的选项卡中选择 Files
然后选择下面的 Python Script
然后把下面代码复制到右边的编辑框中
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
def main():
pass
if __name__ == "__main__":
main()
最后确定即可。
如何使用呢?
右键 —> New —> Python File
然后生成的 python 文件,就会用上面的那个模板填充。
安装过程也超级简单,一直下一步就好了。
pycharm community
vscode
vim