首页
题库
面试
求职
课程
竞赛
More+
所有博客
搜索面经/职位/试题/公司
搜索
我要招人
去企业版
登录 / 注册
首页
>
试题广场
>
移动 0
[编程题]移动 0
热度指数:3883
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 256M,其他语言512M
算法知识视频讲解
给定一个数组,请你实现将所有 0 移动到数组末尾并且不改变其他数字的相对顺序。
数据范围:数组长度满足
,数组中的元素满足
示例1
输入
[1,2,0,3]
输出
[1,2,3,0]
示例2
输入
[1,2,3]
输出
[1,2,3]
示例3
输入
[0,0]
输出
[0,0]
马上挑战
算法知识视频讲解
提交运行
算法知识视频讲解
添加笔记
求解答(0)
邀请回答
收藏(27)
分享
纠错
提交结果有问题?
33个回答
30篇题解
开通博客
lee≤e
发表于 2022-04-18 14:50:30
#啊这啊这,直接遍历,遇到0 remove删除,后在列表尾append class Solution: def moveZeroes(self , nums: List[int]) -> List[int]: # write code here for
展开全文
小白牛牛01
发表于 2022-06-26 23:06:58
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 *
展开全文
机灵的干饭人在思考
发表于 2023-06-11 17:39:22
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型vector * @return int整型vector
展开全文
牛客82035003号
发表于 2022-04-09 20:56:03
一个工作指针负责遍历,一个数组指针负责赋值。 一边遍历一边把不是0的值复制保留, 遍历完后非零值已按原序保存, 就只需把剩下的空全部赋值为0即可。 int* moveZeroes(int* nums, int nums
展开全文
万千少男的梦
发表于 2024-05-23 17:18:12
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型vector * @return int整型vector
展开全文
爱唱歌的潜伏者在发呆
发表于 2024-02-26 12:10:16
class Solution { public: //双指针划分为三个区间 //[0,dest]非0元素,[dest+1,cur-1]0元素,[cur,n-1]待处理元素 vector<int> moveZeroes(vector<int>&
展开全文
xsd123
发表于 2022-03-22 13:39:02
在原数组上修改 两个指针 first_0 , last_0 分别指向第一个0 和最后一个0 (连续区间) 非0值前移 :nums[first_0++] = nums[last_0++] 直到nums[last_0] !=0 给此时的 [first_0, last_0) 区间内赋0 ,重复上述操
展开全文
苦行潜修者
发表于 2024-03-29 23:58:35
using System; using System.Collections.Generic; class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param num
展开全文
君无颜
发表于 2022-02-03 22:16:22
方法一: 遇到一个0,就删掉,并在列表末尾追加一个0 python实现 class Solution: def moveZeroes(self , nums: List[int]) -> List[int]: # write code here i=0
展开全文
倔强小头
发表于 2021-11-26 19:49:34
import java.util.*; public class Solution { public static int[] moveZeroes (int[] nums) { List list=new ArrayList<>(); for (int i = 0; i < nu
展开全文
问题信息
双指针
难度:
33条回答
27收藏
1425浏览
热门推荐
通过挑战的用户
查看代码
牛客50552...
2022-09-15 15:41:28
Doorn
2022-09-14 21:55:41
牛客81129...
2022-09-14 18:42:54
牛客27437...
2022-09-14 16:50:22
Asphyxiay
2022-09-14 01:57:59
相关试题
和为S的两个数字
数组
数学
双指针
评论
(1508)
来自
“一战通offer”互联...
最小面积子矩阵
动态规划
双指针
前缀和
评论
(44)
神奇的数字
排序
双指针
评论
(46)
在IP地址方案中,159.226....
网络基础
评论
(1)
使用全局置换算法,程序不可控制自身...
操作系统
评论
(1)
扫描二维码,关注牛客网
意见反馈
下载牛客APP,随时随地刷题
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @return int整型一维数组 */ public int[] moveZeroes (int[] nums) { // write code here } }
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型vector * @return int整型vector */ vector
moveZeroes(vector
& nums) { // write code here } };
#coding:utf-8 # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param nums int整型一维数组 # @return int整型一维数组 # class Solution: def moveZeroes(self , nums ): # write code here
using System; using System.Collections.Generic; class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @return int整型一维数组 */ public List
moveZeroes (List
nums) { // write code here } }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @return int整型一维数组 */ function moveZeroes( nums ) { // write code here } module.exports = { moveZeroes : moveZeroes };
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param nums int整型一维数组 # @return int整型一维数组 # class Solution: def moveZeroes(self , nums: List[int]) -> List[int]: # write code here
package main import "fmt" /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @return int整型一维数组 */ func moveZeroes( nums []int ) []int { // write code here }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @param numsLen int nums数组长度 * @return int整型一维数组 * @return int* returnSize 返回数组行数 */ int* moveZeroes(int* nums, int numsLen, int* returnSize ) { // write code here }
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param nums int整型一维数组 # @return int整型一维数组 # class Solution def moveZeroes(nums) # write code here end end
object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @return int整型一维数组 */ def moveZeroes(nums: Array[Int]): Array[Int] = { // write code here } }
object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @return int整型一维数组 */ fun moveZeroes(nums: IntArray): IntArray { // write code here } }
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @return int整型一维数组 */ public int[] moveZeroes (int[] nums) { // write code here } }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @return int整型一维数组 */ export function moveZeroes(nums: number[]): number[] { // write code here }
public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @return int整型一维数组 */ func moveZeroes ( _ nums: [Int]) -> [Int] { // write code here } }
struct Solution{ } impl Solution { fn new() -> Self { Solution{} } /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @return int整型一维数组 */ pub fn moveZeroes(&self, nums: Vec
) -> Vec
{ // write code here } }
[1,2,0,3]
[1,2,3,0]
[1,2,3]
[1,2,3]
[0,0]
[0,0]