首页
题库
面试
求职
课程
竞赛
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)
分享
纠错
提交结果有问题?
1个回答
30篇题解
添加回答
0
Python
牛客961048420号
#coding:utf-8
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
# 思路就是死循环遍历,直到把0放最后的次数等于里面的0的总数停下,而且每次找到0的时候下标要减1,否则下标继续加1.
# @param nums int整型一维数组
# @return int整型一维数组
#
class Solution:
def moveZeroes(self , nums ):
# write code here
c=0
i=0
while c!=nums.count(0):
if nums[i]==0:
del nums[i]
nums.append(0)
c+=1
i-=1
i+=1
return nums
发表于 2022-04-09 23:05:12
回复(0)
这道题你会答吗?花几分钟告诉大家答案吧!
提交观点
问题信息
双指针
难度:
1条回答
27收藏
1428浏览
热门推荐
通过挑战的用户
查看代码
牛客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]