【Leetcode 345. 反转字符串中的元音字母 By Python】教程文章相关的互联网学习教程文章

【LeetCode】66. 加一 python实现【图】

点赞 收藏分享文章举报飞飞晗发布了203 篇原创文章 · 获赞 6 · 访问量 1万+私信 关注

【python-leetcode637-树的宽度遍历】二叉树的层平均值【代码】

给定一个非空二叉树, 返回一个由每层节点平均值组成的数组. 示例 1: 输入: 3 / \ 9 20 / \ 15 7输出: [3, 14.5, 11]解释:第0层的平均值是 3, 第1层是 14.5, 第2层是 11. 因此返回 [3, 14.5, 11].注意: 节点值的范围在32位有符号整数范围内。 # Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = No...

LeetCode | 0417. Pacific Atlantic Water Flow太平洋大西洋水流问题【Python】【代码】

LeetCode 0417. Pacific Atlantic Water Flow太平洋大西洋水流问题【Medium】【Python】【DFS】Problem LeetCode Given an m x n matrix of non-negative integers representing the height of each unit cell in a continent, the "Pacific ocean" touches the left and top edges of the matrix and the "Atlantic ocean" touches the right and bottom edges. Water can only flow in four directions (up, down, left, or righ...

python【力扣LeetCode算法题库】257- 二叉树的所有路径【代码】【图】

给定一个二叉树,返回所有从根节点到叶子节点的路径。 说明: 叶子节点是指没有子节点的节点。 示例: 输入: 1 / 2 35 输出: [“1->2->5”, “1->3”] 解释: 所有根节点到叶子节点的路径为: 1->2->5, 1->3 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/binary-tree-paths class Solution:def binaryTreePaths(self, root):""":type root: TreeNode:rtype: List[str]"""if not root:return []res, stack = ...

python刷LeetCode:21. 合并两个有序链表【代码】

难度等级:简单题目描述: 将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 示例: 输入:1->2->4, 1->3->4输出:1->1->2->3->4->4 来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/merge-two-sorted-lists著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 解题思路: 1、此题主要考察对链表的操作 2、解法有多种,此处采用递归 3、不断比较...

【python-leetcode442-循环排序】数组中重复的数据【代码】【图】

问题描述: 给定一个整数数组 a,其中1 ≤ a[i] ≤ n (n为数组长度), 其中有些元素出现两次而其他元素出现一次。 找到所有出现两次的元素。 你可以不用到任何额外空间并在O(n)时间复杂度内解决这个问题吗? 示例: 输入:[4,3,2,7,8,2,3,1] 输出:[2,3] 按照循环排序思想:class Solution:def findDuplicates(self, nums: List[int]) -> List[int]:l,r=0,len(nums)-1while l<=r:if nums[l] != nums[nums[l]-1]:nums[nums[l]-1],num...

python刷LeetCode:9. 回文数【代码】

难度等级:简单题目描述: 判断一个整数是否是回文数。回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数。 示例 1: 输入: 121输出: true示例 2: 输入: -121输出: false解释: 从左向右读, 为 -121 。 从右向左读, 为 121- 。因此它不是一个回文数。示例 3: 输入: 10输出: false解释: 从右向左读, 为 01 。因此它不是一个回文数。进阶: 你能不将整数转为字符串来解决这个问题吗? 来源:力扣(LeetCode)链接:https:/...

leetcode刷题笔记(python3)--199. Binary Tree Right Side View【代码】

Binary Tree Right Side View Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.Example: Input: [1,2,3,null,5,null,4] Output: [1, 3, 4] Explanation: 1 <— / 2 3 <— \ 5 4 <— 解题思路,栈从左到右存储每一层的node,然后弹出top node func rightSideView(root *TreeNode) []in...

leetcode刷题笔记(python3)--168. Excel Sheet Column Title【代码】

168. Excel Sheet Column Title Given a positive integer, return its corresponding column title as appear in an Excel sheet. For example: 1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA 28 -> AB ...Example 1: Input: 1 Output: “A” Example 2: Input: 28 Output: “AB” Example 3: Input: 701 Output: “ZY” func convertToTitle(n int) string {res := ""for n > 0 {n--res = string(n%26+'A') + resn = n / 26}retur...

leetcode刷题笔记(python3)--160. Intersection of Two Linked Lists【代码】【图】

160. Intersection of Two Linked Lists Write a program to find the node at which the intersection of two singly linked lists begins. For example, the following two linked lists:begin to intersect at node c1. Example 1:Input: intersectVal = 8, listA = [4,1,8,4,5], listB = [5,0,1,8,4,5], skipA = 2, skipB = 3 Output: Reference of the node with value = 8 Input Explanation: The intersected node’s value...

【python-leetcode876-快慢指针】链表的中间节点【代码】【图】

问题描述: 给定一个带有头结点 head 的非空单链表,返回链表的中间结点。 如果有两个中间结点,则返回第二个中间结点。 示例 1: 输入:[1,2,3,4,5]输出:此列表中的结点 3 (序列化形式:[3,4,5])返回的结点值为 3 。 (测评系统对该结点序列化表述是 [3,4,5])。注意,我们返回了一个 ListNode 类型的对象 ans,这样:ans.val = 3, ans.next.val = 4, ans.next.next.val = 5, 以及 ans.next.next.next = NULL.示例 2: 输入:[1,2,...

leetcode刷题笔记(python3)--151. Reverse Words in a String【代码】

151. Reverse Words in a String Given an input string, reverse the string word by word. Example 1: Input: “the sky is blue” Output: “blue is sky the” Example 2: Input: " hello world! " Output: “world! hello” Explanation: Your reversed string should not contain leading or trailing spaces. Example 3: Input: “a good example” Output: “example good a” Explanation: You need to reduce multip...

【python-leetcode141-快慢指针】环形链表【代码】【图】

问题描述: 给定一个链表,判断链表中是否有环。 为了表示给定链表中的环,我们使用整数 pos 来表示链表尾连接到链表中的位置(索引从 0 开始)。 如果 pos 是 -1,则在该链表中没有环。 示例 1: 输入:head = [3,2,0,-4], pos = 1 输出:true 解释:链表中有一个环,其尾部连接到第二个节点。 核心思想:两个指针,一个走得慢,一个走得快,如果是有环的话,这两个指针迟早会相遇,否则快指针走到头了,则没有环。 这里有两种写法...

LeetCode | 0392. Is Subsequence判断子序列【Python】

LeetCode 0392. Is Subsequence判断子序列【Easy】【Python】【双指针】Problem LeetCode Given a string s and a string t, check if s is subsequence of t. You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) string, and s is a short string (<=100). A subsequence of a string is a new string which is formed from the original string ...

LeetCode | 0605. Can Place Flowers种花问题【Python】【代码】

LeetCode 0605. Can Place Flowers种花问题【Easy】【Python】【贪心】Problem LeetCode Suppose you have a long flowerbed in which some of the plots are planted and some are not. However, flowers cannot be planted in adjacent plots - they would compete for water and both would die. Given a flowerbed (represented as an array containing 0 and 1, where 0 means empty and 1 means not empty), and a number n,...