【【leetcode-Python】-Tree-814. Binary Tree Pruning】教程文章相关的互联网学习教程文章

LeetCode in Python: Remove Duplicates from Sorted Array II【代码】

LeetCode in Python Remove Duplicates from Sorted Array II class Solution:def RemoveDuplicatesFromSortedArrayII(self, nums):if len(nums) <= 1:return len(nums)index = 2for i in range(2, len(nums)):if nums[i] != nums[index -2]:nums[index] = nums[i]index = index + 1print(nums)return indexs = Solution() nums = [1,1,1,3,4,4,5] res = s.RemoveDuplicatesFromSortedArrayII(nums) print(res)

[LeetCode&Python] Problem 485. Max Consecutive Ones【代码】

Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1,1,0,1,1,1] Output: 3 Explanation: The first two digits or the last three digits are consecutive 1s.The maximum number of consecutive 1s is 3. Note:The input array will only contain 0 and 1. The length of input array is a positive integer and will not exceed 10,000 ?class Solution(object):def findMa...

Python Leetcode 验证回文字符串

主要问题是删除字符串中的符号和空格 Python filter() 函数用于过滤序列,过滤掉不符合条件的元素,返回由符合条件元素组成的新列表。py3返回的是迭代器元素语法:filter(function, iterable)iterable--可迭代元素function:str.isdigit--只保留数字 str.isalpha--只保留字母 str.isalnum--保留数字和字母 lambda ch: ch in ‘...’--‘...’中储存你想保存的字符

Python Leetcode 字符串中的第一个唯一字符

利用到了python中字典的collections.Counter()函数 collections中函数Counter的使用和用法:counter工具用于支持便捷和快速地计数,from collections import Counter cnt = Counter() for word in [red, blue, red, green, blue, blue]: cnt[word] += 1print cnt输出为Counter({blue: 3, red: 2, green: 1})快速实现了题目中所要求的只出现一次

【python/leetcode/135/Hard】Candy【代码】【图】

题目 https://leetcode.com/problems/candy/ 基本思路题目要求比其高的邻居要比本身的奖励多,那么最少也要多一个,所有我们可以找到所有的凹点,凹点如上三种情形。 找到所有的凹点后,我们就可以从凹点处开始向左右两个方向依次查找递增序列,其中每个高的都要比相邻的矮的多一个,比如1,2,5,4.我们找到凹点为1 和4,那么从1开始向左没有其他点,我们向右,依次得到2 比1高,2的糖果应该是1的基础上加1,为2, 5比2高,5的糖果是...

【Leetcode_总结】13. 罗马数字转整数 - python【代码】【图】

罗马数字包含以下七种字符: I, V, X, L,C,D 和 M。字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000 例如, 罗马数字 2 写做 II ,即为两个并列的 1。12 写做 XII ,即为 X + II 。 27 写做 XXVII, 即为 XX + V + II 。 通常情况下,罗马数字中小的数字在大的数字的右边。但也存在特例,例如 4 不写做 IIII,而是 IV。数字 1...

[LeetCode&Python] Problem 226. Invert Binary Tree【代码】

Invert a binary tree. Example: Input:4/ 2 7/ \ / 1 3 6 9 Output:4/ 7 2/ \ / 9 6 3 1 Trivia:This problem was inspired by this original tweet by Max Howell: Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so f*** off.?# Definition for a binary tree node. # class TreeNode: # def __init__(self, x): ...

【Leetcode】Python实现无重复字符的最长子串

示例: 给定 “abcabcbb” ,没有重复字符的最长子串是 “abc” ,那么长度就是3。 给定 “bbbbb” ,最长的子串就是 “b” ,长度是1。 给定 “pwwkew” ,最长子串是 “wke” ,长度是3。请注意答案必须是一个子串,”pwke” 是 子序列 而不是子串。class Solution(object):def lengthOfLongestSubstring(self, s):""":type s: str:rtype: int"""# 存储历史循环中最长的子串长度max_len = 0# 判断传入的字符串是否为空if s is No...

python刷leetcode【代码】

第一题:给定一个整数数组和一个目标值,找出数组中和为目标值的两个数。 示例: 给定 nums = [2, 7, 11, 15], target = 9因为 nums[0] + nums[1] = 2 + 7 = 9 所以返回 [0, 1]class Solution(object):def twoSum(self, nums, target):""":type nums: List[int]:type target: int:rtype: List[int]""" #result = []for i in range(len(nums)):for j in range(i+1,len(nums)):sum = nums[i] + nums[j]if sum == target:#result....

【leetcode/python/138/M】Copy List with Random Pointer【代码】

题目A linked list is given such that each node contains an additional random pointer which could point to any node in the list or null. Return a deep copy of the list.基本思路 链表的拷贝其实可以看做两个步骤,一个是节点数据的拷贝,另一个是节点关系的拷贝。我们也可以先把所有的节点进行拷贝,并存入字典中。然后遍历链表并拷贝两个指针。因为任意指针可能指向空指针,所以在字典中添加一个空指针项。 实现代码 # ...

[LeetCode&Python] Problem 104. Maximum Depth of Binary Tree【代码】

Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. Note: A leaf is a node with no children. Example: Given binary tree [3,9,20,null,null,15,7],3/ 9 20/ 15 7 return its depth = 3.?# Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val ...

LeetCode 628. 三个数的最大乘积(C、C++、python)【代码】

给定一个整型数组,在数组中找出由三个数组成的最大乘积,并输出这个乘积。 示例 1:输入: [1,2,3] 输出: 6示例 2:输入: [1,2,3,4] 输出: 24注意: 给定的整型数组长度范围是[3,104],数组中所有的元素范围是[-1000, 1000]。 输入的数组中任意三个数的乘积不会超出32位有符号整数的范围。 Cint maximumProduct(int* nums, int numsSize) {if(numsSize<3){return 0;}sort(nums,0,numsSize-1);int res1=nums[numsSize-1]*nums[numsSiz...

Leetcode 345. 反转字符串中的元音字母 By Python【代码】

编写一个函数,以字符串作为输入,反转该字符串中的元音字母。 示例 1: 输入: "hello" 输出: "holle" 示例 2: 输入: "leetcode" 输出: "leotcede" 说明: 元音字母不包含字母"y"。思路 设立2个指针,一个从索引0开始向右,一个从末尾向前,根据条件进行处理即可 代码 class Solution:def reverseVowels(self, s):""":type s: str:rtype: str"""yuan = ['a','e','i','o','u','A','E','I','O','U']s = list(s)l = 0r = len(s)-1while ...

Leetcode 349. 两个数组的交集 By Python【代码】

给定两个数组,编写一个函数来计算它们的交集。 示例 1: 输入: nums1 = [1,2,2,1], nums2 = [2,2] 输出: [2] 示例 2: 输入: nums1 = [4,9,5], nums2 = [9,4,9,8,4] 输出: [9,4] 思路 没什么好说的,用set,再用交运算就好了 代码 class Solution:def intersection(self, nums1, nums2):""":type nums1: List[int]:type nums2: List[int]:rtype: List[int]"""return list(set(nums1) & set(nums2))

leetcode第17题,电话号码的字母组合,python实现【代码】【图】

题目 给定一个仅包含数字 2-9 的字符串,返回所有它能表示的字母组合。 给出数字到字母的映射如下(与电话按键相同)。注意 1 不对应任何字母。示例: 输入:“23” 输出:[“ad”, “ae”, “af”, “bd”, “be”, “bf”, “cd”, “ce”, “cf”]. 说明: 尽管上面的答案是按字典序排列的,但是你可以任意选择答案输出的顺序。 思路 本题的核心在于如何创建映射关系,当然可以写多个loop的嵌套,但是那样总感觉不够优雅。 下列代...