【leetcode刷题记第27题解法(python解析)(移除元素)】教程文章相关的互联网学习教程文章

leetcode—python 50天刷题 第18题 最大子序和【代码】

题目描述 给定一个整数数组 nums ,找到一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。 示例: 输入: [-2,1,-3,4,-1,2,1,-5,4], 输出: 6 解释: 连续子数组 [4,-1,2,1] 的和最大,为 6。 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/maximum-subarray 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 解答过程 class Solution(object):def maxSubArray(self, nu...

原 python写算法题:leetcode: 115. Distinct Subsequences

class Solution(object):def numDistinct(self, s, t):""":type s: str:type t: str:rtype: int"""cnt=[0]*len(t)for si,sc in enumerate(s):lastc=""rep=0for ti in xrange(len(t)-1,-1,-1):tc=t[ti]if tc==sc:if ti==0:cnt[ti]+=1else:cnt[ti]+=cnt[ti-1]return cnt[-1]

LeetCode--059--螺旋矩阵 II(python)【代码】

效率超级低,但是能过。。。。 1 class Solution:2 def generateMatrix(self, n):3 tR = tC = 04 dR = n-15 dC = n-16 x = [[0 for i in range(n)] for j in range(n)]7 nowNum=18 while(tR <= dR and tC <=dC):9 nowNum = self.draw(tR,tC,dR,dC,x,nowNum) 10 tR+=1 11 tC+=1 12 dR-=1 13 dC-=1 14 re...

python写算法题:leetcode: 100. Same Tree

class Solution(object):def isSameTree(self, p, q):if (p==None and q==None): return Trueif (p==None or q==None): return Falseif p.val!=q.val: return False""":type p: TreeNode:type q: TreeNode:rtype: bool"""return self.isSameTree(p.left, q.left) and self.isSameTree(p.right, q.right);

leetcode 167. 两数之和 II - 输入有序数组(python)

167. 两数之和 II - 输入有序数组【简单】 给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数。 函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2。 说明: 返回的下标值(index1 和 index2)不是从零开始的。 你可以假设每个输入只对应唯一的答案,而且你不可以重复使用相同的元素。 示例: 输入: numbers = [2, 7, 11, 15], target = 9 输出: [1,2] 解释: 2 与 7 之和等于目标数 ...

leetcode 27. 移除元素(python)【代码】

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

leetcode 数组 (python)【代码】

1.题目描述 给定一个范围在 1 ≤ a[i] ≤ n ( n = 数组大小 ) 的 整型数组,数组中的元素一些出现了两次,另一些只出现一次。 找到所有在 [1, n] 范围之间没有出现在数组中的数字。 您能在不使用额外空间且时间复杂度为O(n)的情况下完成这个任务吗? 你可以假定返回的数组不算在额外空间内。 示例: 输入:  [4,3,2,7,8,2,3,1] 输出:  [5,6] 2. 思路题目中给定n个元素的数组其元素范围在1~n之间,可以考虑数组元素值和其下...

LeetCode 腾讯50题Python实现之《子集》【代码】

题目 给定一组不含重复元素的整数数组 nums,返回该数组所有可能的子集(幂集)。 说明:解集不能包含重复的子集。 示例: 输入: nums = [1,2,3] 输出: [ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], [] ] 思路 回溯法 代码 def subsets(self, nums: List[int]) -> List[List[int]]:length = len(nums)res = []def show(i, tmp):if i > length:returnif tmp not in res:res.append(tmp)for j in range(i, length):show(j+1, tmp ...

python写算法题:leetcode: 97. Interleaving String

class Solution(object):def isInterleave(self, s1, s2, s3):""":type s1: str:type s2: str:type s3: str:rtype: bool"""if len(s1)+len(s2)!=len(s3):return Falsepath=[]p0=-1p1=-1i=0checked=set()while i<len(s3):matched=0if (p0,p1,i) not in checked:if p0+1<len(s1) and s1[p0+1]==s3[i]:checked.add((p0,p1,i))p0+=1matched=1if p1+1<len(s2) and s2[p1+1]==s3[i]:if matched==1:path.append((i+1,p0,p1+1))else:matche...

leetcode 40. 组合总和 II (python)【代码】

给定一个数组 candidates 和一个目标数 target ,找出 candidates 中所有可以使数字和为 target 的组合。 candidates 中的每个数字在每个组合中只能使用一次。 说明: 所有数字(包括目标数)都是正整数。解集不能包含重复的组合。 示例 1: 输入: candidates = [10,1,2,7,6,1,5], target = 8,所求解集为:[ [1, 7], [1, 2, 5], [2, 6], [1, 1, 6]]示例 2: 输入: candidates = [2,5,2,1,2], target = 5,所求解集为:[ [1,2,2], ...

[LeetCode] 224. Basic Calculator @ python【代码】

一.题目: 实现一个基本的计算器,输入为包含"(",")","+","-"," "的字符串,输出运算结果. 二.解题思路: 这种题肯定是用栈实现,一般建立两个栈(数字栈和符号栈),有以下几种情况: (1)对于数字,注意连续几位都是数字; (2)对于符号,注意符号的优先级 代码如下: class Solution(object):def calculate(self, s):""":type s: str:rtype: int"""num_stack = []sign_stack = []for i in range(len(s)):if s[i].isdigit():if n...

leetcode 215. 数组中的第K个最大元素(python)【代码】

在未排序的数组中找到第 k 个最大的元素。请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素。 示例 1: 输入: [3,2,1,5,6,4] 和 k = 2输出: 5示例 2: 输入: [3,2,3,1,2,4,5,5,6] 和 k = 4输出: 4说明: 你可以假设 k 总是有效的,且 1 ≤ k ≤ 数组的长度。class Solution:def findKthLargest(self, nums: List[int], k: int) -> int:nums = sorted(nums)N = len(nums)return nums[N-k]

元素 - 相关标签