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:

  1. You must do this in-place without making a copy of the array.
  2. Minimize the total number of operations.

2、代码实现

这个代码可以AC
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,比如

{0, 0 ,1},像日了狗一样
   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、思考和总结

思路一、

通过遍历所有的数字,想遇到一个0,然后数据左移,如果数据不是0,不动, 特么发现还是蛮复杂的,而且只测试了部分可以,但是不能过AC,比如
{0, 0 ,1},像日了狗一样,而且搞了很久,没搞出来,心都快奔溃了,

思路二、

如果一个题目搞了很久没搞出来,那么我们应该换思路思考这个问题,就像做项目产品一样,如果感觉越做越复杂,越做越不清楚,估计架构就有问题,果断放弃,因该是觉得越做越简单,很明显呀,一位数组,我们大不了从来组装数组,把不等于0的数据依依放在从下表为0的开始的位置,然后进行index++,然后算出0的个数,得到不为0的个数,当所有我们填充的数据慢慢增加到不为0的个数的时候,我们break,然后再把后面所有的数据赋值为0就可以了,以后要形成条件反射,看到一位数组需要改变顺序的,我们第一个想到的应该是,从下标0开始赋值,然下标慢慢变大,再赋我们需要的数据,而不是傻不拉几的去移动数组,而且有时候还不需要移动数组,增加题目的复杂度。

LeetCode之Move Zeroes相关推荐

  1. 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 ...

  2. [东哥的leetcode刷题日记] leetcode 283 : Move Zeroes

    leetcode 283 : Move Zeroes 题目链接: https://leetcode-cn.com/problems/move-zeroes/ 难度: 简单 归类 : 数组操作 题目: ...

  3. [LeetCode] Move Zeroes - 整数数组处理问题

    目录: 1.Move Zeroes  - 数组0移到末尾 [顺序交换] 2. 一.Move Zeroes 题目概述: Given an arraynums, write a function to m ...

  4. Leetcode PHP题解--D68 283. Move Zeroes

    2019独角兽企业重金招聘Python工程师标准>>> D68 283. Move Zeroes 题目链接 283. Move Zeroes 题目分析 给定一个整数数组,将值为0的元 ...

  5. LeetCode之283. Move Zeroes

    ---------------------------------------------------------------------- 解法一:空间换时间 我使用的办法也是类似于"扫描 ...

  6. C#LeetCode刷题之#283-移动零(Move Zeroes)

    问题 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/3907 访问. 给定一个数组 nums,编写一个函数将所有 0 移 ...

  7. 【leetcode❤python】Move Zeroes

    #-*- coding: UTF-8 -*- #filter()函数可以对序列做过滤处理,就是说可以使用一个自定的函数过滤一个序列, #把序列的每一项传到自定义的过滤函数里处理,并返回结果做过滤.最终 ...

  8. 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 ...

  9. [LeetCode] Factorial Trailing Zeroes

    Given an integer n, return the number of trailing zeroes in n!. Note: Your solution should be in log ...

最新文章

  1. 数据库、记录、字段、文档
  2. css中哪些属性与创建多列相关,css3中的新增属性有哪些
  3. [问题解决]基于注解配置dubbo遇到ConnectionLoss for /dubbo/xxx问题解决
  4. leetcode256. 粉刷房子
  5. 浅谈javascript数值类型转换
  6. 网页跳转-重定向-102.课时102.【Django视图高级】重定向详解(Av61533158,P102)
  7. Windows 下 Composer 与 Laravel 4 的安装
  8. 滴滴是如何从零构建集中式实时计算平台的?| 技术头条
  9. php 科学计数 位数,PHP采用超长位数字运算防止数字以科学计数法显示的实例
  10. php装箱,PHP实现的装箱算法示例
  11. 黑龙江测绘局事业单位2009拟聘人员公示
  12. xp如何删除计算机管理员用户账户,“WINXP系统除Administrator以外只有一个管理员账户时,此帐户默认无法删除,如何删除”的解决方案...
  13. 网页内嵌的视频添加 vtt 外挂字幕
  14. 繁荣国家数学教育,坚持“知识共享”许可原则
  15. 从零开始学数据分析之——《笨办法学Python》(习题0-10)
  16. Python wheel文件
  17. 150分钟学会R语言与R常见问题解答
  18. python 调用matlab的.m文件
  19. 如何updateR的version
  20. 台北房价窥北京未来:调控阻止不了高房价

热门文章

  1. 通过Dapr实现一个简单的基于.net的微服务电商系统(二)——通讯框架讲解
  2. ASP.NET Core集成Nacos配置中心之适配多格式配置
  3. .NET Core 使用 Consul 服务注册发现
  4. C#刷剑指Offer | 从上到下打印二叉树
  5. WeihanLi.Npoi 1.10.0 更新日志
  6. EntityFramework Core 3.x添加查询提示(NOLOCK)
  7. 【视频教程】使用 ASP.NET Core 3.x 构建 RESTful Web API 已完结
  8. 向net core 3.0进击——April.WebApi从2.2爬到3.0
  9. Ocelot(五)- 流量限制、服务质量
  10. .NET开发人员如何开始使用ML.NET