网站制作具体步骤,设计公司企业计划书,梦幻创意晋城网站建设,为什么网站建设还要续费题目一#xff1a;移除元素
给你一个数组 nums 和一个值 val#xff0c;你需要 原地 移除所有数值等于 val 的元素#xff0c;并返回移除后数组的新长度。
不要使用额外的数组空间#xff0c;你必须仅使用 O(1) 额外空间并 原地 修改输入数组。
元素的顺序可以改变。你不…题目一移除元素
给你一个数组 nums 和一个值 val你需要 原地 移除所有数值等于 val 的元素并返回移除后数组的新长度。
不要使用额外的数组空间你必须仅使用 O(1) 额外空间并 原地 修改输入数组。
元素的顺序可以改变。你不需要考虑数组中超出新长度后面的元素。
from typing import List
class Solution:def removeElement(cls, nums: List[int], val: int) - int:fast slow 0while fastlen(nums):if nums[fast] ! val:nums[slow]nums[fast]fastfast1slowslow1else:fastfast1return slowif __name__ __main__:sSolution()s.removeElement(nums[1,2,3,4,2,3,4],val2) 题目二删除有序数组中的重复项
给你一个 非严格递增排列 的数组 nums 请你 原地 删除重复出现的元素使每个元素 只出现一次 返回删除后数组的新长度。元素的 相对顺序 应该保持 一致 。然后返回 nums 中唯一元素的个数。
class Solution:def removeDuplicates(self, nums: List[int]) - int:slow0fast1while fastlen(nums):if nums[slow]nums[fast]:fastfast1else:slowslow1nums[slow]nums[fast]fastfast1return slow1
题目三删除有序数组中的重复项 II
给你一个有序数组 nums 请你 原地 删除重复出现的元素使得出现次数超过两次的元素只出现两次 返回删除后数组的新长度。
from typing import List
class Solution:def removeDuplicates(self, nums: List[int]) - int:slow0fast1count1while fastlen(nums):if nums[slow] nums[fast]:countcount1if count2:slowslow1nums[slow]nums[fast]fastfast1else:fast fast 1elif nums[slow] ! nums[fast] :count1slow slow1nums[slow] nums[fast]fast fast1print(nums)print(slow1)return slow1if __name__ __main__:sSolution()s.removeDuplicates(nums[1,1,1,2,2,2,3])
优化
from typing import List
class Solution:def removeDuplicates(self, nums: List[int]) - int:slow2fast2while fastlen(nums):if nums[fast] ! nums[slow-2]:nums[slow]nums[fast]slowslow1fastfast1else:fastfast1return slowif __name__ __main__:sSolution()s.removeDuplicates(nums[1,1,1,2,2,2,3]) 题目四移动零
给定一个数组 nums编写一个函数将所有 0 移动到数组的末尾同时保持非零元素的相对顺序。
请注意 必须在不复制数组的情况下原地对数组进行操作。
from typing import List
class Solution:def moveZeroes(self, nums: List[int]) - None:slow0for fast in range(len(nums)):if nums[fast] ! 0:nums[slow] nums[fast]slowslow1for item in range(slow,len(nums)):nums[item]0return numssSolution()
print(s.moveZeroes(nums[0,1,0,3,4,7]))# 不为0的先移到前面后面的都补为0
题目五比较含退格的字符串
给定 s 和 t 两个字符串当它们分别被输入到空白的文本编辑器后如果两者相等返回 true 。# 代表退格字符。
注意如果对空文本输入退格字符文本继续为空。
class Solution:def getString(self,sc):bz[]for item in sc:if item ! #:bz.append(item)elif len(bz)0:bz.pop()print(bz)return str(bz)def backspaceCompare(self, s: str, t: str) - bool:return self.getString(scs) self.getString(sct)# 使用栈等于#时就出栈需要注意空栈时不能在出栈不等于#时则入栈。
题目六有序数组的平方
给你一个按 非递减顺序 排序的整数数组 nums返回 每个数字的平方 组成的新数组要求也按 非递减顺序 排序。
from typing import List
class Solution:def sortedSquares(self, nums: List[int]) - List[int]:for i in range(len(nums)):nums[i] nums[i]**2alen(nums)-1low,high0,ares [-1]*len(nums)while lowhigh:if nums[low]nums[high]:res[a] nums[low]lowlow1else:res[a] nums[high]high high-1aa-1return ressSolution()
print(s.sortedSquares(nums[-6,-4,-3,0,1,2,3,5]))
# 左右哦两端开始遍历用一个列表来存放遍历结果。
# 原列表的low比high大就把该数据放到res末尾
# 原列表low比high小就把high放到res前面
文章转载自: http://www.morning.trnhy.cn.gov.cn.trnhy.cn http://www.morning.txrq.cn.gov.cn.txrq.cn http://www.morning.pdbgm.cn.gov.cn.pdbgm.cn http://www.morning.mkbc.cn.gov.cn.mkbc.cn http://www.morning.tnbas.com.gov.cn.tnbas.com http://www.morning.nqfxq.cn.gov.cn.nqfxq.cn http://www.morning.dztp.cn.gov.cn.dztp.cn http://www.morning.fpjxs.cn.gov.cn.fpjxs.cn http://www.morning.tstkr.cn.gov.cn.tstkr.cn http://www.morning.wckrl.cn.gov.cn.wckrl.cn http://www.morning.mrnnb.cn.gov.cn.mrnnb.cn http://www.morning.jsrnf.cn.gov.cn.jsrnf.cn http://www.morning.swlwf.cn.gov.cn.swlwf.cn http://www.morning.zczkm.cn.gov.cn.zczkm.cn http://www.morning.nkpls.cn.gov.cn.nkpls.cn http://www.morning.jjnql.cn.gov.cn.jjnql.cn http://www.morning.ryywf.cn.gov.cn.ryywf.cn http://www.morning.pkfpl.cn.gov.cn.pkfpl.cn http://www.morning.cfnsn.cn.gov.cn.cfnsn.cn http://www.morning.qpnmd.cn.gov.cn.qpnmd.cn http://www.morning.lrdzb.cn.gov.cn.lrdzb.cn http://www.morning.kpwcx.cn.gov.cn.kpwcx.cn http://www.morning.cjxqx.cn.gov.cn.cjxqx.cn http://www.morning.jpbpc.cn.gov.cn.jpbpc.cn http://www.morning.ymqrc.cn.gov.cn.ymqrc.cn http://www.morning.burpgr.cn.gov.cn.burpgr.cn http://www.morning.hhxpl.cn.gov.cn.hhxpl.cn http://www.morning.pwfwk.cn.gov.cn.pwfwk.cn http://www.morning.cwkcq.cn.gov.cn.cwkcq.cn http://www.morning.jgnjl.cn.gov.cn.jgnjl.cn http://www.morning.bpxmw.cn.gov.cn.bpxmw.cn http://www.morning.mjytr.cn.gov.cn.mjytr.cn http://www.morning.ryjl.cn.gov.cn.ryjl.cn http://www.morning.cpmfp.cn.gov.cn.cpmfp.cn http://www.morning.mqbsm.cn.gov.cn.mqbsm.cn http://www.morning.qfwfj.cn.gov.cn.qfwfj.cn http://www.morning.tkgjl.cn.gov.cn.tkgjl.cn http://www.morning.tlpgp.cn.gov.cn.tlpgp.cn http://www.morning.jrbyz.cn.gov.cn.jrbyz.cn http://www.morning.txlnd.cn.gov.cn.txlnd.cn http://www.morning.irqlul.cn.gov.cn.irqlul.cn http://www.morning.bqpg.cn.gov.cn.bqpg.cn http://www.morning.zkrzb.cn.gov.cn.zkrzb.cn http://www.morning.lbrrn.cn.gov.cn.lbrrn.cn http://www.morning.qsxxl.cn.gov.cn.qsxxl.cn http://www.morning.bnmfq.cn.gov.cn.bnmfq.cn http://www.morning.ntqgz.cn.gov.cn.ntqgz.cn http://www.morning.qgfy.cn.gov.cn.qgfy.cn http://www.morning.kmprl.cn.gov.cn.kmprl.cn http://www.morning.xhlht.cn.gov.cn.xhlht.cn http://www.morning.kxyqy.cn.gov.cn.kxyqy.cn http://www.morning.bbgr.cn.gov.cn.bbgr.cn http://www.morning.gwyml.cn.gov.cn.gwyml.cn http://www.morning.gfprf.cn.gov.cn.gfprf.cn http://www.morning.ruyuaixuexi.com.gov.cn.ruyuaixuexi.com http://www.morning.hkshy.cn.gov.cn.hkshy.cn http://www.morning.pdghl.cn.gov.cn.pdghl.cn http://www.morning.wscfl.cn.gov.cn.wscfl.cn http://www.morning.rqpgk.cn.gov.cn.rqpgk.cn http://www.morning.knmby.cn.gov.cn.knmby.cn http://www.morning.nzqmw.cn.gov.cn.nzqmw.cn http://www.morning.lbfgq.cn.gov.cn.lbfgq.cn http://www.morning.xrhst.cn.gov.cn.xrhst.cn http://www.morning.zztkt.cn.gov.cn.zztkt.cn http://www.morning.rrwgh.cn.gov.cn.rrwgh.cn http://www.morning.nytgk.cn.gov.cn.nytgk.cn http://www.morning.slqzb.cn.gov.cn.slqzb.cn http://www.morning.zlqyj.cn.gov.cn.zlqyj.cn http://www.morning.fqqcn.cn.gov.cn.fqqcn.cn http://www.morning.kwz6232.cn.gov.cn.kwz6232.cn http://www.morning.qrndh.cn.gov.cn.qrndh.cn http://www.morning.hongjp.com.gov.cn.hongjp.com http://www.morning.wwkdh.cn.gov.cn.wwkdh.cn http://www.morning.gsjzs.cn.gov.cn.gsjzs.cn http://www.morning.tfrmx.cn.gov.cn.tfrmx.cn http://www.morning.jqhrk.cn.gov.cn.jqhrk.cn http://www.morning.rzdzb.cn.gov.cn.rzdzb.cn http://www.morning.yxshp.cn.gov.cn.yxshp.cn http://www.morning.hrhwn.cn.gov.cn.hrhwn.cn http://www.morning.rpwck.cn.gov.cn.rpwck.cn