上一页 1 ··· 4 5 6 7 8 9 10 11 12 ··· 16 下一页
摘要: class ListNode: def __init__(self, x): self.val = x self.next = None# 快慢指针的做法,定义两个指针,一个走一步,一个走两步# 如果他们不相遇的话,那就是没有环,如果在某一时刻相遇,# 就说明是有环的。class Solution: 阅读全文
posted @ 2020-07-20 22:04 月为暮 阅读(520) 评论(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 月为暮 阅读(445) 评论(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 月为暮 阅读(419) 评论(0) 推荐(0) 编辑
摘要: # 导入requests 和 BeautifulSoupimport requestsfrom bs4 import BeautifulSoupdef download_page(url): # 定义头部,用来骗过浏览器 headers ={'User-Agent': 'Mozilla/5.0 (W 阅读全文
posted @ 2020-07-17 22:00 月为暮 阅读(497) 评论(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 月为暮 阅读(466) 评论(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 月为暮 阅读(534) 评论(0) 推荐(0) 编辑
摘要: from typing import Listclass Solution: # 第一种是我想的办法 def singleNumber(self, nums: List[int]) -> int: # 首先进行排序 nums.sort() # 然后判断重复的数字,数组中的数字必定为奇数个, # 如果 阅读全文
posted @ 2020-07-15 20:47 月为暮 阅读(330) 评论(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 月为暮 阅读(191) 评论(0) 推荐(0) 编辑
摘要: from typing import List# 用动态规划的写法来写题。# 每一天都有五种情况发生,#1,今天买入,2,今天卖出,3今天是冷冻期,4,今天不买入也不卖出(没有持有股票)5,今天不买入也不卖出(持有股票)class Solution: def maxProfit(self, pric 阅读全文
posted @ 2020-07-14 19:22 月为暮 阅读(259) 评论(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 月为暮 阅读(275) 评论(0) 推荐(0) 编辑
上一页 1 ··· 4 5 6 7 8 9 10 11 12 ··· 16 下一页