【LeetCode | 0392. Is Subsequence判断子序列【Python】】教程文章相关的互联网学习教程文章

python7.4.2二叉树路径问题及LeetCode题目解析

下面题目中的路径,定义有所延伸,在解法思路及时间空间复杂度上有所挑战。  437. Path Sum III  You are given a binary tree in which each node contains an integer value.  Find the number of paths that sum to a given value.  The path does not need to start or end at the root or a leaf, but it must go downwards (traveling only from parent nodes to child nodes).  The tree has no more than 1,00...

【LeetCode】43.字符串相乘--Python【代码】【图】

题目描述 ??给定以字符串形式比表示的非负整数num1和num2,返回num1和num2的乘积的字符串形式。 ??例如num1=“123”,num2=“456” ??输出:“56088” 分析 ??这类涉及乘法的问题要考虑数字界限,但对于Python来说,大数完全没问题。可以直接return str(int(num1)*int(num2)),但是对于我们这些有追求的人 老实人,还是要考虑一下界限问题。思路是这样的,我们需要考虑平时手算乘法的过程,例如: 1?2?3 ??6 --------------------- ...

LeetCode *231.Power of Two (Python Solution)【代码】

题目描述 Given an integer, write a function to determine if it is a power of two. 给定一个整数,写一个函数来确定它是否是2的幂。 Example 1:Input: 1 Output: true Explanation: 20 = 1Example 2:Input: 16 Output: true Explanation: 24 = 16Example 3:Input: 218 Output: falsePython Solution class Solution:def isPowerOfTwo(self, n: int) -> bool:return n > 0 and n & (n - 1) == 0分析: 整数包含正数和负数,但负...

LeetCode-Python-220. 存在重复元素 III

给定一个整数数组,判断数组中是否有两个不同的索引 i 和 j,使得 nums [i] 和 nums [j] 的差的绝对值最大为 t,并且 i 和 j 之间的差的绝对值最大为 ?。 示例 1: 输入: nums = [1,2,3,1], k = 3, t = 0 输出: true 示例 2: 输入: nums = [1,0,1,1], k = 1, t = 2 输出: true 示例 3: 输入: nums = [1,5,9,1,5,9], k = 2, t = 3 输出: false 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/contains-duplicate-ii...

LeetCode 74 搜索二维矩阵 Python

编写一个高效的算法来判断 m x n 矩阵中,是否存在一个目标值。该矩阵具有如下特性: 每行中的整数从左到右按升序排列。 每行的第一个整数大于前一行的最后一个整数。 示例 1: 输入: matrix = [ [1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 50] ] target = 3 输出: true之前做的二分查找的题都是一维的,但这道题是一个二维的矩阵,但是它的上一行的最后一个元素的值小于下一行的第一个元素的值,那么可以将下标看成一...

LeetCode-Python-206. 反转链表

反转一个单链表。 示例: 输入: 1->2->3->4->5->NULL 输出: 5->4->3->2->1->NULL 进阶: 你可以迭代或递归地反转链表。你能否用两种方法解决这道题? 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/reverse-linked-list 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 第一种思路: 递归,假设我们已经得到了把head.next翻转完成的结果,p指向这个结果的头, head此时指向这个结果的最后...

LeetCode 26. 删除排序数组中的重复项(python)【代码】【图】

题目链接 题目描述: 给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度。 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。 示例 1: 给定数组 nums = [1,1,2], 函数应该返回新的长度 2, 并且原数组 nums 的前两个元素被修改为 1, 2。 你不需要考虑数组中超出新长度后面的元素。 示例 2: 给定 nums = [0,0,1,1,1,2,2,3,3,4], 函数应该返回...

LeetCode的某些easy难度算法题的Python解法

因为一些公司的校招提前批开始了,所以翻了翻LeetCode做下准备工作。首先看了easy的题目(循序渐进),发现一些题目使用Python解题非常奇妙,所以写一个帖子记录一下。 题目一:To Lower Case。将输入的字符串全部改为小写,并以字符串输出。 这题一般的思路就是遍历字符串,根据ASCII码表来转换大小写。如果用Python可以使用str.lower(),然后就秒答了。代码如下:class Solution(object):def toLowerCase(self, str):""":type st...

LeetCode 7. 整数反转(python)【代码】【图】

题目链接 题目描述: 给出一个 32 位的有符号整数,你需要将这个整数中每位上的数字进行反转。 示例 1: 输入: 123 输出: 321 示例 2: 输入: -123 输出: -321 示例 3: 输入: 120 输出: 21 注意: 假设我们的环境只能存储得下 32 位的有符号整数,则其数值范围为 [?231, 231 ? 1]。请根据这个假设,如果反转后整数溢出那么就返回 0。 解题思路: 如果原数字末尾有0,再反转后是开头是不会有0的,所以首先将原数字末尾的0去掉(这一步...

leetcode刷题121 买卖股票的最佳时机 Best Time to Buy and Sell Stock(简单) Python Java

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

LeetCode in Python 547. Friend Circles【代码】

There are N students in a class. Some of them are friends, while some are not. Their friendship is transitive in nature. For example, if A is a direct friend of B, and B is a direct friend of C, then A is an indirect friend of C. And we defined a friend circle is a group of students who are direct or indirect friends. Given a N*N matrix M representing the friend relationship between students in th...

LeetCode in Python 1020. Number of Enclaves【代码】

Given a 2D array A, each cell is 0 (representing sea) or 1 (representing land) A move consists of walking from one land square 4-directionally to another land square, or off the boundary of the grid. Return the number of land squares in the grid for which we cannot walk off the boundary of the grid in any number of moves. Example 1: Input: [[0,0,0,0],[1,0,1,0],[0,1,1,0],[0,0,0,0]] Output: 3 Expl...

59-合并两个排序链表-LeetCode21(python)【代码】

题目描述 将两个有序链表合并为一个新的有序链表并返回。新链表是通过拼接给定的两个链表的所有节点组成的。 示例输入:1->2->4, 1->3->4 输出:1->1->2->3->4->4 思路 官方题解之迭代:1.使用哨兵节点prehead,易于之后返回合并后的链表;2.两个链表都是有序的,维护一个pre指针,调整它的next指针;从l1和l2的当前节点中选择较小的一个接在pre指针的后面,同时将其指针向后移。3.在循环终止时,l1和l2中最多有一个非空,且该链表中...

[LeetCode]Coin Change@Python【代码】

Coin Change You are given coins of different denominations and a total amount of money amount. Write a function to compute the fewest number of coins that you need to make up that amount. If that amount of money cannot be made up by any combination of the coins, return -1. Example Input: coins = [1, 2, 5], amount = 11 Output: 3 Explanation: 11 = 5 + 5 + 1 Solution class Solution:def coinChange(sel...

Python3解leetcode Lowest Common Ancestor of a Binary Search TreeBinary Tree Paths【代码】

问题描述: Given a binary tree, return all root-to-leaf paths. Note: A leaf is a node with no children. Example: Input:1/ 2 35Output: ["1->2->5", "1->3"]Explanation: All root-to-leaf paths are: 1->2->5, 1->3 思路: 二叉树的问题,首先考虑递归算法,用深度优先搜索。 为了体现模块化思想,一般讲DFS算法单独写成一个方法 代码: 1 # Definition for a binary tree node.2 # class TreeNode:3 # ...