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

【LeetCode】14.最长公共前缀(python版)【代码】【图】

题目描述: 编写一个函数来查找字符串数组中的最长公共前缀。 如果不存在公共前缀,返回空字符串 “”。 示例 1: 输入: ["flower","flow","flight"] 输出: "fl"示例 2: 输入: ["dog","racecar","car"] 输出: "" 解释: 输入不存在公共前缀。说明: 所有输入只包含小写字母 a-z 。思路: 本题虽然只是“容易”等级的题目,但是可以有至少五种解法,而且这是一个在搜索领域中很常见的问题,所以还是值得一看。暴力解法: 先找到两个字符...

LeetCode 1. 两数之和(python3)实现【代码】

给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标。 你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中同样的元素。 示例: 给定 nums = [2, 7, 11, 15], target = 9 因为 nums[0] + nums[1] = 2 + 7 = 9所以返回 [0, 1] 来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/two-sum# 最简单的优化解法 # 时间复杂度为O(n) class Solu...

leetcode 69. Sqrt(x) 二分法 python3【代码】

一.问题描述 Implement int sqrt(int x). Compute and return the square root of x, where x is guaranteed to be a non-negative integer. Since the return type is an integer, the decimal digits are truncated and only the integer part of the result is returned. Example 1:Input: 4 Output: 2Example 2:Input: 8 Output: 2 Explanation: The square root of 8 is 2.82842..., and since the decimal par...

leetcode No.429 N叉树的层序遍历 (python3实现)【代码】【图】

来源 https://leetcode-cn.com/problems/n-ary-tree-level-order-traversal/ 题目描述 给定一个 N 叉树,返回其节点值的层序遍历。 (即从左到右,逐层遍历)。 例如,给定一个 3叉树 : 返回其层序遍历: [ [1], [3,2,4], [5,6]] 说明: 树的深度不会超过 1000。 树的节点总数不会超过 5000。 代码实现 """ # Definition for a Node. class Node:def __init__(self, val=None, children=None):self.val = valself.children = childr...

leetcode No.2 两数相加 (python3实现)【代码】

来源 https://leetcode-cn.com/problems/add-two-numbers/ 题目描述 给出两个 非空 的链表用来表示两个非负的整数。其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字。 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和。 您可以假设除了数字 0 之外,这两个数都不会以 0 开头。 示例: 输入:(2 -> 4 -> 3) + (5 -> 6 -> 4) 输出:7 -> 0 -> 8 原因:342 + 465 = 807 代码...

【LeetCode】528. 按权重随机选择 解题报告 (python)

原题地址:https://leetcode-cn.com/problems/random-pick-with-weight/submissions/ 题目描述: 给定一个正整数数组 w ,其中 w[i] 代表位置 i 的权重,请写一个函数 pickIndex ,它可以随机地获取位置 i,选取位置 i 的概率与 w[i] 成正比。 说明: 1 <= w.length <= 10000 1 <= w[i] <= 10^5 pickIndex 将被调用不超过 10000 次 示例1: 输入: ["Solution","pickIndex"] [[[1]],[]] 输出: [null,0] 示例2: 输入: ["Solution","p...

LeetCode--169--多数元素(python)【代码】【图】

给定一个大小为 n 的数组,找到其中的多数元素。多数元素是指在数组中出现次数大于 ? n/2 ? 的元素。 你可以假设数组是非空的,并且给定的数组总是存在多数元素。 废话不多说 直接分治 1 class Solution:2 def majorityElement(self, nums: List[int]) -> int:3 def help(low,high):4 if low == high:5 return nums[low]6 mid = (high-low)//2 + low7 left = ...

Leetcode【111】Find N Unique Integers Sum up to Zero(Python版)

Pythonic class Solution:def sumZero(self, n: int) -> List[int]:return range(1-n, n, 2) 菜鸟版 class Solution(object):def sumZero(self, n):""":type n: int:rtype: List[int]"""ans = []if n % 2 == 0:tmp_1 = [i for i in range(1,n/2+1)]tmp_2 = [-i for i in range(1,n/2+1)]ans = tmp_1 + tmp_2else:tmp_1 = [i for i in range(1,(n-1)/2+1)]tmp_2 = [-i for i in range(1,(n-1)/2+1)]ans = tmp_1 + tmp_2 + ["0"]retu...

leetcode算法题121-123 --python版本【代码】

给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。 实例输入: [0,1,0,3,12] 输出: [1,3,12,0,0]说明:必须在原数组上操作,不能拷贝额外的数组。 尽量减少操作次数。 思路:从左到右遍历数组存在数字把是0的逐一的替换,左右更替,最后在遍历剩余的直接填写0就可以class Solution: def moveZeroes(self, nums): if len(nums)<0: return pos = 0 for i i...

LeetCode-239-剑指offer-滑动窗口的最大值-队列与栈-python【代码】

给定一个数组和滑动窗口的大小,找出所有滑动窗口里数值的最大值。例如,如果输入数组{2,3,4,2,6,2,5,1}及滑动窗口的大小3,那么一共存在6个滑动窗口,他们的最大值分别为{4,4,6,6,6,5}; 针对数组{2,3,4,2,6,2,5,1}的滑动窗口有以下6个: {[2,3,4],2,6,2,5,1}, {2,[3,4,2],6,2,5,1}, {2,3,[4,2,6],2,5,1}, {2,3,4,[2,6,2],5,1}, {2,3,4,2,[6,2,5],1}, {2,3,4,2,6,[2,5,1]}。 思路: class Solution:def maxInWindows(self, ...

LeetCode--006--Z 字形变换(python)【代码】【图】

将一个给定字符串根据给定的行数,以从上往下、从左到右进行 Z 字形排列。 比如输入字符串为 "LEETCODEISHIRING" 行数为 3 时,排列如下: 之后,你的输出需要从左往右逐行读取,产生出一个新的字符串,比如:"LCIRETOESIIGEDHN"。 请你实现这个将字符串进行指定行数变换的函数: string convert(string s, int numRows);示例 1: 输入: s = "LEETCODEISHIRING", numRows = 3输出: "LCIRETOESIIGEDHN"示例 2: 输入: s = "LEETC...

LeetCode做题记录(Python版)

1. 两数之和点击查看折叠代码块 class Solution:def twoSum(self, nums: List[int], target: int) -> List[int]:dic = {}for i, x in enumerate(nums):dic[x] = ifor i, x in enumerate(nums):if target - x in dic and dic[target - x] != i:return [i, dic[target - x]]

LeetCode--114--二叉树展开为链表(python)【代码】

给定一个二叉树,原地将它展开为链表。 例如,给定二叉树1   / \   2 5   / \ \ 3 4 6将其展开为:1      \      2      \      3       \      4        \        5         \         6 将root的右子树放到root的左子树的最右边作为右孩子 将root的左孩子变为自己的右孩子 (r...

LeetCode.27 移除元素(python解法)【代码】

目录题目solution_1 题目 给定一个数组 nums 和一个值 val,你需要原地移除所有数值等于 val 的元素,返回移除后数组的新长度。 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。 元素的顺序可以改变。你不需要考虑数组中超出新长度后面的元素。 示例 1: 给定 nums = [3,2,2,3], val = 3,函数应该返回新的长度 2, 并且 nums 中的前两个元素均为 2。你不需要考虑数组中超出新长度后面的元素。...

Python3解leetcode Reach a Number【代码】

问题描述: You are standing at position 0 on an infinite number line. There is a goal at position target. On each move, you can either go left or right. During the n-th move (starting from 1), you take n steps. Return the minimum number of steps required to reach the destination. Example 1: Input: target = 3 Output: 2 Explanation: On the first move we step from 0 to 1. On the second step we step f...