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

leetcode 922. Sort Array By Parity II(python)【代码】

描述 Given an array A of non-negative integers, half of the integers in A are odd, and half of the integers are even. Sort the array so that whenever A[i] is odd, i is odd; and whenever A[i] is even, i is even. You may return any answer array that satisfies this condition. Example 1: Input: [4,2,5,7] Output: [4,5,2,7] Explanation: [4,7,2,5], [2,5,4,7], [2,7,4,5] would also have been accepted.Note:...

leetcode 832. Flipping an Image(python)【代码】

描述 Given a binary matrix A, we want to flip the image horizontally, then invert it, and return the resulting image. To flip an image horizontally means that each row of the image is reversed. For example, flipping [1, 1, 0] horizontally results in [0, 1, 1]. To invert an image means that each 0 is replaced by 1, and each 1 is replaced by 0. For example, inverting [0, 1, 1] results in [1, 0, 0]. ...

leetcode 647. Palindromic Substrings(python)【代码】

描述 Given a string, your task is to count how many palindromic substrings in this string. The substrings with different start indexes or end indexes are counted as different substrings even they consist of same characters. Example 1: Input: "abc" Output: 3 Explanation: Three palindromic strings: "a", "b", "c".Example 2: Input: "aaa" Output: 6 Explanation: Six palindromic strings: "a", "a", "a", "...

leetcode 647. Palindromic Substrings(python)【代码】

描述 Given a string, your task is to count how many palindromic substrings in this string. The substrings with different start indexes or end indexes are counted as different substrings even they consist of same characters. Example 1: Input: "abc" Output: 3 Explanation: Three palindromic strings: "a", "b", "c".Example 2: Input: "aaa" Output: 6 Explanation: Six palindromic strings: "a", "a", "a", "...

leetcode简单题(python)【代码】

简单题-python 1.两数之和给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。你可以假设每种输入只会对应一个答案。但是,数组中同一个元素不能使用两遍。示例: 给定 nums = [2, 7, 11, 15], target = 9因为 nums[0] + nums[1] = 2 + 7 = 9 所以返回 [0, 1]# 1. def twoSum(self, nums, target):for i in range(len(nums)):for j in range(i + 1, len(nums)):if nu...

Leetcode题库:动态规划在猜石子游戏中的应用(Python语言)【代码】【图】

动态规划 关于什么是动态规划,这里不再赘述,网上有大把教程和概念,阅读本文前提是你对动态规划已经有了初步的认识。 概括来说,动态规划有四个步骤: 定义状态。寻找状态转移方程状态压缩(如果可行的话)得出结果 LeetCode上的猜石子游戏 LeetCode有很多猜石子的游戏,大多是角色都能发挥最优的水平,属于博弈的范畴,下面就介绍三道可以用动态规划来解决的猜石子题目,难度也有容易用困难。 1、LeetCode486:预测赢家 题目描述...

LeetCode 290[Python]. 单词规律 给定一种规律 pattern 和一个字符串 str ,判断 str 是否遵循相同的规律。【代码】

LeetCode 290. 单词规律 给定一种规律 pattern 和一个字符串 str ,判断 str 是否遵循相同的规律。 这里的 遵循 指完全匹配,例如, pattern 里的每个字母和字符串 str 中的每个非空单词之间存在着双向连接(映射)的对应规律。 示例1: 输入: pattern = “abba”, str = “dog cat cat dog” 输出: true Code def wordPattern(self, pattern: str, s: str) -> bool:dic1={}#字典1dic2={}#字典2words=s.split()#string->charif len(pa...

Leetcode 1396. Design Underground System (python)【代码】【图】

题目错误解法 先分享一种错误解法。下面的这种错误解法错在很多个点上。首先,他每个操作不是O(1)的,时间复杂度就错了。第二,如果某个id先进出某两个站,然后再进了某个站,这个时候计算他的travel time是错的,因为按照下面这种解法会是出站时间小于进站时间,也就是这种解法不满足同一个id多次进出的情况。第三,如果某个idA进B出,然后C进D出,这个时候按下面的解法,在query(A,D)这个pair的时候就会被错误的包括进去,所以st...

记录大二小白的LeetCode(python)刷题之旅【图】

LeetCode第一题之TwoSum 记录一下大二小白的LeetCode刷题之旅 难度:简单 题目描述:给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。 你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。 示例: 给定 nums = [2, 7, 11, 15], target = 9 因为 nums[0] + nums[1] = 2 + 7 = 9 所以返回 [0, 1] 自己的憨憨解法: 思路:先通过for循环...

【Leetcode】155: 最小栈(Python)【代码】【图】

题目描述: 设计一个支持 push ,pop ,top 操作,并能在常数时间内检索到最小元素的栈。 push(x) —— 将元素 x 推入栈中。pop() —— 删除栈顶的元素。top() —— 获取栈顶元素。getMin() —— 检索栈中的最小元素。 示例: 输入:["MinStack","push","push","push","getMin","pop","top","getMin"][[],[-2],[0],[-3],[],[],[],[]] 输出:[null,null,null,null,-3,null,0,-2] 解释:MinStack minStack = new MinStack();minStack...

LeetCode每日一题(Python实现)649.Dota2参议院【代码】

题目 Dota2 的世界里有两个阵营:Radiant(天辉)和 Dire(夜魇)Dota2 参议院由来自两派的参议员组成。现在参议院希望对一个 Dota2 游戏里的改变作出决定。他们以一个基于轮为过程的投票进行。在每一轮中,每一位参议员都可以行使两项权利中的一项:禁止一名参议员的权利: 参议员可以让另一位参议员在这一轮和随后的几轮中丧失所有的权利。 宣布胜利: 如果参议员发现有权利投票的参议员都是同一个阵营的,他可以宣布胜利并决定在游戏...

leetcode--python--605【代码】

605. 种花问题 假设你有一个很长的花坛,一部分地块种植了花,另一部分却没有。可是,花卉不能种植在相邻的地块上,它们会争夺水源,两者都会死去。 给定一个花坛(表示为一个数组包含0和1,其中0表示没种植花,1表示种植了花),和一个数 n 。能否在不打破种植规则的情况下种入 n 朵花?能则返回True,不能则返回False。 class Solution:def canPlaceFlowers(self, flowerbed: List[int], n: int) -> bool:tem_list = [0] + flowe...

LeetCode 283. 移动零[Python] 给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。【代码】

LeetCode 283. 移动零 给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。 示例: 输入: [0,1,0,3,12] 输出: [1,3,12,0,0] Code import queue#库 class Solution:def moveZeroes(self, nums: List[int]) -> None:n=len(nums)m=0q=queue.Queue(n)for i in range(0,n,1):if nums[i] != 0:q.put(nums[i])m=m+1#计数for i in range(m,n,1):q.put(0)for i in range(0,n,1):#输出nums[i]=q.get()想...

LeetCode每日一题(Python实现):118.杨辉三角【代码】

题目 给定一个非负整数 numRows,生成杨辉三角的前 numRows 行。 在杨辉三角中,每个数是它左上方和右上方的数的和。示例:输入: 5 输出: [[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1] ]来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/pascals-triangle 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。代码 from typing import * class Solution:def generate(self,numRows: int)-> List[Lis...

leetcode刷题记录1481-1490 python版【代码】

前言 继续leetcode刷题生涯 这里记录的都是笔者觉得有点意思的做法 参考了好几位大佬的题解,感谢各位大佬 1481. 不同整数的最少数目 class Solution:def findLeastNumOfUniqueInts(self, arr: List[int], k: int) -> int:import collectionsl = collections.Counter(arr)sort_val = sorted(l.values())for index, val in enumerate(sort_val):if k >= val:k = k - valelse:return len(sort_val) - indexreturn 01482. 制作 m 束花...