【LeetCode | 0417. Pacific Atlantic Water Flow太平洋大西洋水流问题【Python】】教程文章相关的互联网学习教程文章

LeetCode | 0122. Best Time to Buy and Sell Stock II买卖股票的最佳时机 II【Python】【代码】

LeetCode 0122. Best Time to Buy and Sell Stock II买卖股票的最佳时机 II【Easy】【Python】【贪心】Problem LeetCode Say you have an array for which the ith element is the price of a given stock on day i. Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times). Note: You may not engage in multiple ...

LeetCode | 0121. Best Time to Buy and Sell Stock买卖股票的最佳时机【Python】【代码】

LeetCode 0121. Best Time to Buy and Sell Stock买卖股票的最佳时机【Easy】【Python】【贪心】Problem LeetCode Say you have an array for which the ith element is the price of a given stock on day i. If you were only permitted to complete at most one transaction (i.e., buy one and sell one share of the stock), design an algorithm to find the maximum profit. Note that you cannot sell a stock before you ...

【python】一道LeetCode搞懂递归算法!#131分割回文串 #以及刷LeetCode的一点点小心得 [数据结构与算法基础]【代码】

题目:给定一个字符串 s,将 s 分割成一些子串,使每个子串都是回文串。返回 s 所有可能的分割方案。# 示例 输入: "aab" 输出: [["aa","b"],["a","a","b"] ]https://leetcode-cn.com/problems/palindrome-partitioning/视频<iframe allowfullscreen="true" data-mediaembed="bilibili" frameborder="0" id="C4HvQLxS-1580957903424" src="https://player.bilibili.com/player.html?aid=86926331"></iframe> 【python】一道LeetCode...

LeetCode 42 用Python3来接雨水【代码】【图】

leetcode 42 (https://leetcode-cn.com/problems/trapping-rain-water/) Given n non-negative integers representing an elevation map where the width of each bar is 1, compute how much water it is able to trap after raining.Example: Input: [0,1,0,2,1,0,1,3,2,1,2,1] Output: 6 解法一 先说说自己超时的算法思路,也顺便把一种暴力解法展示出来,然后介绍别人家的高级算法,也算是抛砖引玉。 很明显,刚看到这题,我就...

leetcode字符串(python)

3. 无重复字符的最长子串 https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/solution/hua-dong-chuang-kou-by-powcai/ 这道题主要用到思路是:滑动窗口 什么是滑动窗口? 其实就是一个队列,比如例题中的 abcabcbb,进入这个队列(窗口)为 abc 满足题目要求,当再进入 a,队列变成了 abca,这时候不满足要求。所以,我们要移动这个队列! 如何移动?我们只要把队列的左边的元素移出就行了,直到满...

Leetcode 112. Path Sum 二叉树中和为某一值的路径 Python3

方法 :递归 最直接的方法就是利用递归,遍历整棵树:如果当前节点不是叶子,对它的所有孩子节点,递归调用 hasPathSum 函数,其中 sum 值减去当前节点的权值;如果当前节点是叶子,检查 sum 值是否为 0,也就是是否找到了给定的目标和。 # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = Noneclass Solution:de...

LeetCode-Python-1342. 数组大小减半(贪心 + 堆)

给你一个整数数组 arr。你可以从中选出一个整数集合,并删除这些整数在数组中的每次出现。 返回 至少 能删除数组中的一半整数的整数集合的最小大小。 示例 1: 输入:arr = [3,3,3,3,5,5,5,2,2,7] 输出:2 解释:选择 {3,7} 使得结果数组为 [5,5,5,2,2]、长度为 5(原数组长度的一半)。 大小为 2 的可行集合有 {3,5},{3,2},{5,2}。 选择 {2,7} 是不可行的,它的结果数组为 [3,3,3,3,5,5,5],新数组长度大于原数组的二分之一。 示...

leetcode刷题记第27题解法(python解析)(移除元素)【代码】

leetcode刷题记--> 27题解法(python解析)题目定义1. 双指针2. 双指针 —— 当要删除的元素很少时实现 题目定义 给定一个数组 nums 和一个值 val,你需要原地移除所有数值等于 val 的元素,返回移除后数组的新长度。 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。 元素的顺序可以改变。你不需要考虑数组中超出新长度后面的元素。 示例 1: 给定 nums = [3,2,2,3], val = 3,函数应该返回新...

(python刷题)leetcode top100第一题:两数之和【代码】【图】

题目在leetcode上的链接为: https://leetcode-cn.com/problems/two-sum/ 题目描述解题思路 使用dict来做,时间复杂度为o(n),空间复杂度为o(n) 1.将列表nums中的元素值作为key,元素对应下标作为value,建立dict 2.遍历列表nums元素a,判断b = target-a是否在dict中,如果b在dict中且a,b的下标不同,输出a,b下标 在建立dict时可能会出现nums中的元素相同而键值覆盖的情况,但是由于第二次遍历列表nums时,nums中的元素和以前一致,...

LeetCode474 - Ones and Zeros - Medium (Python)【代码】

In the computer world, use restricted resource you have to generate maximum benefit is what we always want to pursue. For now, suppose you are a dominator of m 0s and n 1s respectively. On the other hand, there is an array with strings consisting of only 0s and 1s. Now your task is to find the maximum number of strings that you can form with given m 0s and n 1s. Each 0 and 1 can be used at most on...

LeetCode469 - Convex Polygon - Medium (Python)【代码】

Given a list of points that form a polygon when joined sequentially, find if this polygon is convex (Convex polygon definition). [[0,0],[0,1],[1,1],[1,0]]Answer: True [[0,0],[0,10],[10,10],[10,0],[5,5]]Answer: False思路:这题问的是给一系列polygon的点,判断其是否是convex的。一个比较好的判断方法是看所有点是否是按照同一个顺序排列的,比如所有点都是依次顺时针,逆时针或者是同一条线排列。这里判断方向的方...

LeetCode 4. Median of Two Sorted Arrays Python3【代码】【图】

LeetCode 4. Median of Two Sorted Arrays Python3 Description 点击查看题目 There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the two sorted arrays. The overall run time complexity should be O(log (m+n)). You may assume nums1 and nums2 cannot be both empty. Example 1:nums1 = [1, 3] nums2 = [2] The median is 2.0 Example 2:nums1 = [1, 2] nums2 = [3, 4] The med...

LeetCode 92反转链表II Python3【代码】【图】

原题:链接 反转从位置 m 到 n 的链表。请使用一趟扫描完成反转。 说明: 1 ≤ m ≤ n ≤ 链表长度。 示例:解法一 其实刚开始看这道题也没啥特别的想法,直接迭代方法开干就行了。但是有很多细节需要处理,导致代码修改了很久才通过。这里简单地捋一下吧。设第m个节点为start节点,在后续节点的翻转操作之前,我们需要保存start节点的前一个节点front,因此为了使得程序便于处理一些边界条件,加入一个虚拟头结点vHead来作为front的...

Leetcode刷题之——两数之和||输入有序数组(python解决)【图】

题目描述:解决方案: 将有序数组中的元素使用字典的形式进行表示,对于target减去数组中的元素值是否在数组的其他位置出现过进行以一比对,代码如下:class Solution(object):def twoSum(self, numbers, target):""":type numbers: List[int]:type target: int """dic = {}for index, value in enumerate(numbers):dic[value] = indexfor index, value in enumerate(numbers):if dic.get(target - value) is not None:return [ind...

LeetCode-Python-1325. 删除给定值的叶子节点(DFS + 拓扑排序)

给你一棵以 root 为根的二叉树和一个整数 target ,请你删除所有值为 target 的 叶子节点 。 注意,一旦删除值为 target 的叶子节点,它的父节点就可能变成叶子节点;如果新叶子节点的值恰好也是 target ,那么这个节点也应该被删除。 也就是说,你需要重复此过程直到不能继续删除。 示例 1: 输入:root = [1,2,3,2,null,2,4], target = 2 输出:[1,null,3,null,4] 解释: 上面左边的图中,绿色节点为叶子节点,且它们的值与 ta...