LeetCode之Move Zeroes
1、题目
Given an array nums
, write a function to move all 0
's to the end of it while maintaining the relative order of the non-zero elements.
For example, given nums = [0, 1, 0, 3, 12]
, after calling your function, nums
should be [1, 3, 12, 0, 0]
.
Note:
- You must do this in-place without making a copy of the array.
- Minimize the total number of operations.
2、代码实现
public class Solution {public void moveZeroes(int[] nums) { if (nums == null || nums.length == 0)return;int length = nums.length;int allZero = 0;for (int i = 0; i < length; ++i) {if (nums[i] == 0)allZero++;}int count = 0;for (int i = 0; i < length; ++i) {if (nums[i] != 0) {nums[count++] = nums[i];if (count == length - allZero) break;}}for (int i = count; i < length; ++i)nums[count++] = 0;}
}
下面的代码是我通过思路,通过遍历所有的数字,想遇到一个0,然后数据左移,如果数据不是0,不动, 特么发现还是蛮复杂的,而且只测试了部分可以,但是不能过AC,比如
public static void moveZeroes1(int[] nums) {if (nums == null || nums.length == 0)return;int length = nums.length;int count = 0;for (int i = 0; i < length - 1; ++i) {if (nums[i] == 0) {for (int j = i ; j <= length - i; j++) {if (j + 1 < length) {nums[j] = nums[j + 1];}}nums[length - 1] = 0;}}}
3、思考和总结
思路一、
思路二、
LeetCode之Move Zeroes相关推荐
- LeetCode 283. Move Zeroes
题目: Given an array nums, write a function to move all 0's to the end of it while maintaining the rel ...
- [东哥的leetcode刷题日记] leetcode 283 : Move Zeroes
leetcode 283 : Move Zeroes 题目链接: https://leetcode-cn.com/problems/move-zeroes/ 难度: 简单 归类 : 数组操作 题目: ...
- [LeetCode] Move Zeroes - 整数数组处理问题
目录: 1.Move Zeroes - 数组0移到末尾 [顺序交换] 2. 一.Move Zeroes 题目概述: Given an arraynums, write a function to m ...
- Leetcode PHP题解--D68 283. Move Zeroes
2019独角兽企业重金招聘Python工程师标准>>> D68 283. Move Zeroes 题目链接 283. Move Zeroes 题目分析 给定一个整数数组,将值为0的元 ...
- LeetCode之283. Move Zeroes
---------------------------------------------------------------------- 解法一:空间换时间 我使用的办法也是类似于"扫描 ...
- C#LeetCode刷题之#283-移动零(Move Zeroes)
问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3907 访问. 给定一个数组 nums,编写一个函数将所有 0 移 ...
- 【leetcode❤python】Move Zeroes
#-*- coding: UTF-8 -*- #filter()函数可以对序列做过滤处理,就是说可以使用一个自定的函数过滤一个序列, #把序列的每一项传到自定义的过滤函数里处理,并返回结果做过滤.最终 ...
- LeetCode 1516. Move Sub-Tree of N-Ary Tree(DFS)
文章目录 1. 题目 2. 解题 1. 题目 Given the root of an N-ary tree of unique values, and two nodes of the tree p ...
- [LeetCode] Factorial Trailing Zeroes
Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...
最新文章
- 数据库、记录、字段、文档
- css中哪些属性与创建多列相关,css3中的新增属性有哪些
- [问题解决]基于注解配置dubbo遇到ConnectionLoss for /dubbo/xxx问题解决
- leetcode256. 粉刷房子
- 浅谈javascript数值类型转换
- 网页跳转-重定向-102.课时102.【Django视图高级】重定向详解(Av61533158,P102)
- Windows 下 Composer 与 Laravel 4 的安装
- 滴滴是如何从零构建集中式实时计算平台的?| 技术头条
- php 科学计数 位数,PHP采用超长位数字运算防止数字以科学计数法显示的实例
- php装箱,PHP实现的装箱算法示例
- 黑龙江测绘局事业单位2009拟聘人员公示
- xp如何删除计算机管理员用户账户,“WINXP系统除Administrator以外只有一个管理员账户时,此帐户默认无法删除,如何删除”的解决方案...
- 网页内嵌的视频添加 vtt 外挂字幕
- 繁荣国家数学教育,坚持“知识共享”许可原则
- 从零开始学数据分析之——《笨办法学Python》(习题0-10)
- Python wheel文件
- 150分钟学会R语言与R常见问题解答
- python 调用matlab的.m文件
- 如何updateR的version
- 台北房价窥北京未来:调控阻止不了高房价
热门文章
- 通过Dapr实现一个简单的基于.net的微服务电商系统(二)——通讯框架讲解
- ASP.NET Core集成Nacos配置中心之适配多格式配置
- .NET Core 使用 Consul 服务注册发现
- C#刷剑指Offer | 从上到下打印二叉树
- WeihanLi.Npoi 1.10.0 更新日志
- EntityFramework Core 3.x添加查询提示(NOLOCK)
- 【视频教程】使用 ASP.NET Core 3.x 构建 RESTful Web API 已完结
- 向net core 3.0进击——April.WebApi从2.2爬到3.0
- Ocelot(五)- 流量限制、服务质量
- .NET开发人员如何开始使用ML.NET