【LeetCode 26. 删除排序数组中的重复项(python)】教程文章相关的互联网学习教程文章

Python3解leetcode Same Tree【代码】

问题描述: Given two binary trees, write a function to check if they are the same or not. Two binary trees are considered the same if they are structurally identical and the nodes have the same value. Example 1: Input: 1 1/ \ / 2 3 2 3[1,2,3], [1,2,3]Output: true Example 2: Input: 1 1/ 2 2[1,2], [1,null,...

Leetcode加一 (java、python3)【代码】【图】

加一 给定一个由整数组成的非空数组所表示的非负整数,在该数的基础上加一。 最高位数字存放在数组的首位, 数组中每个元素只存储一个数字。 你可以假设除了整数 0 之外,这个整数不会以零开头。 Given a non-empty array of digits representing a non-negative integer, plus one to the integer. The digits are stored such that the most significant digit is at the head of the list, and each element in the array conta...

leetcode 46 Permutations Python 实现(回溯算法)【代码】

Given a collection of distinct integers, return all possible permutations. Example: Input: [1,2,3]Output:[ [1,2,3], [1,3,2], [2,1,3], [2,3,1], [3,1,2], [3,2,1]]回溯算法自己的一个思路如下:需要保留数字使用情况,my_nums增加和删除元素等操作 1 class Solution:2 def permute(self, nums: List[int]) -> List[List[int]]:3 results = []4 use_dict = dict.fromkeys(nums, False) #初始化一...

相同的树 ---- LeetCode(Python3实现)【代码】

一、题目 题目链接:https://leetcode-cn.com/problems/same-tree/ 给定两个二叉树,编写一个函数来检验它们是否相同。 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。 示例 1:输入: 1 1/ \ / 2 3 2 3[1,2,3], [1,2,3]输出: true 示例 2:输入: 1 1/ 2 2[1,2], [1,null,2]输出: false示例 3:输入: 1 ...

leetcode441. 排列硬币(python)【代码】【图】

题目链接 题目描述: 你总共有 n 枚硬币,你需要将它们摆成一个阶梯形状,第 k 行就必须正好有 k 枚硬币。 给定一个数字 n,找出可形成完整阶梯行的总行数。 n 是一个非负整数,并且在32位有符号整型的范围内。 示例 1: n = 5 硬币可排列成以下几行: 因为第三行不完整,所以返回2. 示例 2: n = 8 硬币可排列成以下几行: 因为第四行不完整,所以返回3. 解题思路: 方法1: 时间超长,算出第k行需要的硬币直到所需硬币大...

LeetCode [链表]19.Remove Nth Node From End of List (C++和Python实现)

19.Remove Nth Node From End of List [难度:中等] 【题目】 Given a linked list, remove the n-th node from the end of list and return its head. Example:Given linked list: 1->2->3->4->5, and n = 2.After removing the second node from the end, the linked list becomes 1->2->3->5.Note: Given n will always be valid. Follow up: Could you do this in one pass? 【解题C++】 (题外话:刚还想说用不惯LeetCode,感...

LeetCode刷题笔记342:4的幂(Python实现)【代码】

给定一个整数 (32 位有符号整数),请编写一个函数来判断它是否是 4 的幂次方。 示例 1:输入: 16 输出: true示例 2:输入: 5 输出: false 进阶: 你能不使用循环或者递归来完成本题吗? Solution1:使用递归 这道题和263丑数有相似的地方,递归isPowerOfFour这个方法,直到将num用4除到商为1,则为4的幂,否则不是 代码:class Solution:def isPowerOfFour(self,num):''':param num:int :return: bool'''if num == 0:return Falseif ...

LeetCode-Python-37. 解数独【图】

编写一个程序,通过已填充的空格来解决数独问题。 一个数独的解法需遵循如下规则: 数字 1-9 在每一行只能出现一次。 数字 1-9 在每一列只能出现一次。 数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。 空白格用 '.' 表示。一个数独。答案被标成红色。 Note: 给定的数独序列只包含数字 1-9 和字符 '.' 。 你可以假设给定的数独只有唯一解。 给定数独永远是 9x9 形式的。 思路: 回溯,一个数一个数地试…… 不知道为什么b...

LeetCode-Python-1038. 从二叉搜索树到更大和树【代码】【图】

给出二叉搜索树的根节点,该二叉树的节点值各不相同,修改二叉树,使每个节点 node 的新值等于原树中大于或等于 node.val 的值之和。 提醒一下,二叉搜索树满足下列约束条件: 节点的左子树仅包含键小于节点键的节点。 节点的右子树仅包含键大于节点键的节点。 左右子树也必须是二叉搜索树。 示例:输入:[4,1,6,0,2,5,7,null,null,null,3,null,null,null,8] 输出:[30,36,21,36,35,26,15,null,null,null,33,null,null,null,8] ...

LeetCode 236 二叉树的最近公共祖先 Lowest Common Ancestor of a Binary Tree Python【代码】

有关二叉树的做题笔记,Python实现 二叉树的定义 # Definition for a binary tree node. class TreeNode:def __init__(self, x):self.val = xself.left = Noneself.right = None236. 二叉树的最近公共祖先 Lowest Common Ancestor of a Binary Tree LeetCodeCN 第236题链接 首先如果root为空,返回root,然后如果root就是p或者q,那root就是最近公共祖先。然后分别对左子树和右子树做递归并保存结果,如果两边都能找到,证明本节点...

LeetCode 有关二叉树的做题笔记 Python实现【代码】

有关二叉树的做题笔记,Python实现 二叉树的定义 # Definition for a binary tree node. class TreeNode:def __init__(self, x):self.val = xself.left = Noneself.right = None98. 验证二叉搜索树 Validate Binary Search Tree LeetCodeCN 第98题链接 第一种方法:中序遍历二叉树存入数组,与直接升序排序去重后的原二叉树对比class Solution:def isValidBST(self, root: TreeNode) -> bool:inorder = self.inorder(root)return i...

leetcode 73 矩阵置零 Python【代码】

矩阵置零 ??给定一个 m x n 的矩阵,如果一个元素为 0,则将其所在行和列的所有元素都设为 0。请使用原地算法。 示例 1: 输入: [ [1,1,1], [1,0,1], [1,1,1] ] 输出: [ [1,0,1], [0,0,0], [1,0,1] ]示例 2: 输入: [ [0,1,2,0], [3,4,5,2], [1,3,1,5] ] 输出: [ [0,0,0,0], [0,4,5,0], [0,3,1,0] ] 进阶:一个直接的解决方案是使用 O(mn) 的额外空间,但这并不是一个好的解决方案。 一个简单的改进方案...

LeetCode算法题:买卖股票的最佳时机---python【代码】【图】

给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。 如果你最多只允许完成一笔交易(即买入和卖出一支股票),设计一个算法来计算你所能获取的最大利润。 注意你不能在买入股票前卖出股票。 示例 1: 输入: [7,1,5,3,6,4] 输出: 5 解释: 在第 2 天(股票价格 = 1)的时候买入,在第 5 天(股票价格 = 6)的时候卖出,最大利润 = 6-1 = 5 。 注意利润不能是 7-1 = 6, 因为卖出价格需要大于买入价格。 示例 2: 输入: [7,6,4...

LeetCode题解(python)-50. Pow(x, n)【代码】

LeetCode题解(python) 50. Pow(x, n) 题目描述 实现 pow(x, n) ,即计算 x 的 n 次幂函数。 示例 1: 输入: 2.00000, 10 输出: 1024.00000示例 2: 输入: 2.10000, 3 输出: 9.26100示例 3: 输入: 2.00000, -2 输出: 0.25000 解释: 2-2 = 1/22 = 1/4 = 0.25说明:-100.0 < x < 100.0 n 是 32 位有符号整数,其数值范围是 [?231, 231 ? 1] 。解题心得本题看起来很简单嘛,for循环,一直乘,正数直接结果,负数指数绝对值,结果取倒数。...

leetcode67 二进制求和 python

二进制求和(简单)(leetcode67) 给定两个二进制字符串,返回他们的和(用二进制表示)输入为非空字符串且只包含数字0,1。 示例 1:输入: a = "11", b = "1"输出: "100"示例 2:输入: a = "1010", b = "1011"输出: "10101" def addBinary(self, a: str, b: str): return bin(int(a,2)+int(b,2))[2:]