07 2020 档案

摘要:class ListNode: def __init__(self, x): self.val = x self.next = Nonea = ListNode(1)b = ListNode(3)c = ListNode(2)d = ListNode(1)e = ListNode(5)a.next 阅读全文
posted @ 2020-07-31 12:49 月为暮 阅读(285) 评论(0) 推荐(0)
摘要:# Definition for a binary tree node.class TreeNode: def __init__(self, x): self.val = x self.left = None self.right = None# 后序遍历,先遍历左子树,在遍历右子树,在遍历根节点。 阅读全文
posted @ 2020-07-30 19:07 月为暮 阅读(221) 评论(0) 推荐(0)
摘要:# 这道题我是用动态规划的方法来做的。# 遍历从1 ~ n 里边所有的数差分后的最大乘积,# 然后从中找出两个数相加等于n,判断其中的最大乘积是多少。# 注意,这里还有一种可能,就是这个数的最大乘积,没有这个数本身大。# 所以判断的时候用max(index2,dp[index2])class Sol 阅读全文
posted @ 2020-07-30 13:31 月为暮 阅读(222) 评论(0) 推荐(0)
摘要:from typing import List# 这道题看了大佬写的代码,经过自己的理解写出来了。# 从最外围的四周找有没有为O的,如果有的话就进入深搜函数,然后深搜遍历# 判断上下左右的位置是否为Oclass Solution: def solve(self, board: List[List[s 阅读全文
posted @ 2020-07-29 19:49 月为暮 阅读(275) 评论(0) 推荐(0)
摘要:class TreeNode: def __init__(self, x): self.val = x self.left = None self.right = Nonea = TreeNode(1)b = TreeNode(2)c = TreeNode(3)a.left = ba.right = 阅读全文
posted @ 2020-07-29 18:44 月为暮 阅读(229) 评论(0) 推荐(0)
摘要:# 导入需要的包import requestsimport time,randomfrom openpyxl import Workbookimport pymysql.cursors#@ 连接数据库;# 这个是我本地上边运行的程序,用来获取代理服务器。def get_proxy(): try: P 阅读全文
posted @ 2020-07-25 15:05 月为暮 阅读(332) 评论(0) 推荐(0)
摘要:class ListNode: def __init__(self, x): self.val = x self.next = Noneclass Solution: def swapPairs(self, head: ListNode) -> ListNode: # 定义一个节点,并将它指向头结点 阅读全文
posted @ 2020-07-20 22:07 月为暮 阅读(554) 评论(0) 推荐(0)
摘要:class ListNode: def __init__(self, x): self.val = x self.next = None# 快慢指针的做法,定义两个指针,一个走一步,一个走两步# 如果他们不相遇的话,那就是没有环,如果在某一时刻相遇,# 就说明是有环的。class Solution: 阅读全文
posted @ 2020-07-20 22:04 月为暮 阅读(546) 评论(0) 推荐(0)
摘要:# Definition for singly-linked list.class ListNode: def __init__(self, x): self.val = x self.next = Nonea = ListNode(1)b = ListNode(2)a.next = b# 有关链表 阅读全文
posted @ 2020-07-20 21:30 月为暮 阅读(470) 评论(0) 推荐(0)
摘要:from typing import List# 这道题很容易能够想到,只需要遍历两边列表就可以了# 两层循环class Solution: def twoSum(self, numbers: List[int], target: int) -> List[int]: # 第一次遍历列表 for i 阅读全文
posted @ 2020-07-20 21:10 月为暮 阅读(454) 评论(0) 推荐(0)
摘要:# 导入requests 和 BeautifulSoupimport requestsfrom bs4 import BeautifulSoupdef download_page(url): # 定义头部,用来骗过浏览器 headers ={'User-Agent': 'Mozilla/5.0 (W 阅读全文
posted @ 2020-07-17 22:00 月为暮 阅读(539) 评论(0) 推荐(0)
摘要:from typing import List# 这道题是比较容易的,只需要遍历一遍就好了class Solution: def searchInsert(self, nums: List[int], target: int) -> int: # 这里定义一个变量用于接收索引 num_index = 阅读全文
posted @ 2020-07-17 16:47 月为暮 阅读(493) 评论(0) 推荐(0)
摘要:# Definition for a binary tree node.# 前序遍历的意思是先遍历根节点,然后遍历左子树,最后是右子树# 因此这道题可以用递归的方法直接解出来。class TreeNode: def __init__(self, x): self.val = x self.left 阅读全文
posted @ 2020-07-16 22:12 月为暮 阅读(571) 评论(0) 推荐(0)
摘要:from typing import Listclass Solution: # 第一种是我想的办法 def singleNumber(self, nums: List[int]) -> int: # 首先进行排序 nums.sort() # 然后判断重复的数字,数组中的数字必定为奇数个, # 如果 阅读全文
posted @ 2020-07-15 20:47 月为暮 阅读(358) 评论(1) 推荐(0)
摘要:# 二叉搜索树的特点是左子树小于根节点,右子树大于根节点。# 因此当根节点为i的时候,左子树的值为1:i-1,右子树为i+1:n# 当节点为n的时候所有的能够组成的树为左子树个数乘以右子树个数。class Solution: def numTrees(self, n: int) -> int: dp 阅读全文
posted @ 2020-07-15 19:54 月为暮 阅读(216) 评论(0) 推荐(0)
摘要:from typing import List# 用动态规划的写法来写题。# 每一天都有五种情况发生,#1,今天买入,2,今天卖出,3今天是冷冻期,4,今天不买入也不卖出(没有持有股票)5,今天不买入也不卖出(持有股票)class Solution: def maxProfit(self, pric 阅读全文
posted @ 2020-07-14 19:22 月为暮 阅读(288) 评论(0) 推荐(0)
摘要:# 动态规划的解法,参考别人的。from typing import Listclass Solution: def respace(self, dictionary: List[str], sentence: str) -> int: d = {}.fromkeys(dictionary,"heh 阅读全文
posted @ 2020-07-14 16:15 月为暮 阅读(295) 评论(0) 推荐(0)
摘要:from typing import List# 这道题比较容易,遍历一遍k + 1就好了,# 可以算出所有的结果,但是会有重复值,因此需要将重复值排除# 同时避免超市class Solution: def divingBoard(self, shorter: int, longer: int, k 阅读全文
posted @ 2020-07-14 15:42 月为暮 阅读(209) 评论(0) 推荐(0)
摘要:from typing import List# 八皇后问题,用递归的方法来写。class Solution: def solveNQueens(self, n: int) -> List[List[str]]: # 如果n < 1直接返回空列表 if n < 1:return [] # 定义变量用 阅读全文
posted @ 2020-07-13 19:51 月为暮 阅读(311) 评论(0) 推荐(0)
摘要:from typing import Listclass Solution: def findSubstring(self, s: str, words: List[str]) -> List[int]: # 导入计数类 from collections import Counter # 如果s和w 阅读全文
posted @ 2020-07-13 19:15 月为暮 阅读(183) 评论(0) 推荐(0)
摘要:from typing import Listclass Solution: def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float: # 这样写可以不用判断两个数组和为奇数和偶数的情况 index1 阅读全文
posted @ 2020-07-06 10:34 月为暮 阅读(254) 评论(0) 推荐(0)
摘要:class Solution: def strStr(self, haystack: str, needle: str) -> int: # 判断needle是否为NOne或者为空字符串 if not needle or len(needle) == 0: return 0 # 定义两个变量,用来接 阅读全文
posted @ 2020-07-06 09:55 月为暮 阅读(232) 评论(0) 推荐(0)
摘要:# 利用双指针,一次遍历,求出结果class Solution: def isPalindrome(self, s: str) -> bool: # 定义变量,接收字符串的长度 length = len(s) # 长度小于等于1直接返回真 if length <= 1:return True # 定 阅读全文
posted @ 2020-07-04 21:02 月为暮 阅读(213) 评论(0) 推荐(0)
摘要:# Definition for a binary tree node.class TreeNode: def __init__(self, x): self.val = x self.left = None self.right = Nonea = TreeNode(1)b = TreeNode( 阅读全文
posted @ 2020-07-03 20:24 月为暮 阅读(285) 评论(0) 推荐(0)
摘要:from typing import Listclass Solution: def maxProfit(self, prices: List[int]) -> int: length = len(prices) if length <= 1:return 0 dp=[ [[0,0,0],[0,0, 阅读全文
posted @ 2020-07-02 16:29 月为暮 阅读(311) 评论(0) 推荐(0)
摘要:# 爬取豆瓣最受欢迎的250部电影,并写入Excel表格中import requests,xlwtfrom bs4 import BeautifulSoup# 请求豆瓣网站,获取网页源码def request_douban(url): try : # 请求url headers = {"User-A 阅读全文
posted @ 2020-07-02 13:40 月为暮 阅读(331) 评论(0) 推荐(0)
摘要:# import requests,re,json# # 定义一个函数用来请求当当网的网页信息# def request_dangdang(url):# try:# # 使用get请求# response = requests.get(url)# # 判断返回的状态码是否为200# if respo 阅读全文
posted @ 2020-07-01 21:00 月为暮 阅读(465) 评论(0) 推荐(1)
摘要:from typing import List# 这道题和上一题很类似,但有一些不同,每天都可以买入,或者卖出# 那么利润最大化,就是i + 1 天的价格比i天的价格高的时候买入# 然后在i + 1天卖出class Solution: def maxProfit(self, prices: List 阅读全文
posted @ 2020-07-01 16:40 月为暮 阅读(268) 评论(0) 推荐(0)