力扣(leetcode)第283题移动零(Python)

283.移动零

题目链接:283.移动零

给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。

请注意 ,必须在不复制数组的情况下原地对数组进行操作。

示例 1:

输入: nums = 0,1,0,3,12

输出: 1,3,12,0,0
示例 2:

输入: nums = 0

输出: 0

提示:

1 <= nums.length <= 104

-231 <= numsi <= 231 - 1

解答

python 复制代码
class Solution:
    def moveZeroes(self, nums: List[int]) -> None:
        """
        Do not return anything, modify nums in-place instead.
        """
        if not nums:
            return 0
        j = 0
        for i in range(len(nums)):
            if nums[i]:
                nums[i],nums[j]=nums[j],nums[i]
                j+=1

最后,我写了一篇MySQL教程,里面详细的介绍了MySQL的基本概念以及操作指令等内容,欢迎阅读!
MySQL数据库万字保姆级教程

相关推荐
65岁退休Coder1 小时前
LangGraph v1.2.9 节点容错策略 & 流式输出 & 持久化记忆管理
后端·python·langchain
杉氧2 小时前
状态管理变迁史:为什么我们放弃了 Redux 选择 Zustand?
android·前端·react native
ikun_文4 小时前
Django框架路由Router的使用
python·pycharm·django
IvanCodes4 小时前
Python 基础语法(二):字符串与常用操作
python
昭昭日月明4 小时前
LangChain 生态:从链到代理,开发者需要掌握的三大核心
python·langchain·agent
Csvn4 小时前
🐍 Day 8:面向对象编程
后端·python
程序员天天困4 小时前
向量检索不准怎么办:混合检索与 Rerank 重排序召回优化实战
后端·python·ai编程
alphaTao6 小时前
LeetCode 每日一题 2026/8/24-2026/8/30
python·算法·leetcode
苏灿烤鱼6 小时前
当 AI Agent 遇见真实科学环境:深度拆解 Scientific Agent Skills,把"聊天机器人"变成"AI 科学家"
python·开源·agent
张文君6 小时前
ubuntu26.04坏道坏块分区隔离急速版260831-V0.12
linux·python