描述

给定一个数组nums,写一个函数,将数组中所有的0挪到数组的末尾,维持其他所有非0元素的相对位置。 举例: nums = [0, 1, 0, 3, 12],

函数运行后结果为[1, 3, 12, 0, 0]

解析

快慢指针,慢指针指向第一个0,快指针指向第一个非0.

代码

  1. public static void main(String[] args) {
  2. int[] n = {4,2,4,0,0,3,0,5,1,0};
  3. moveZero(n);
  4. System.out.println(JSON.toJSONString(n));
  5. }
  6.  
  7. public static void moveZero(int[] n) {
  8. if (n == null || n.length < 2) {
  9. return;
  10. }
  11. int slow = 0;
  12. int fast = 1;
  13. while (fast < n.length && slow < n.length) {
  14. if (n[slow] == 0 && n[fast] != 0) {
  15. swap(n, slow++, fast++);
  16. } else if (n[slow] == 0 && n[fast] == 0) {
  17. fast++;
  18. } else {
  19. slow++;
  20. fast++;
  21. }
  22. }
  23. }
  24.  
  25. public static void swap(int[] n, int a, int b) {
  26. if (a == b) {
  27. return;
  28. }
  29. n[a] = n[a] ^ n[b];
  30. n[b] = n[a] ^ n[b];
  31. n[a] = n[a] ^ n[b];
  32. }

[LeetCode] 283. Move Zeroes ☆(移动0到最后)的更多相关文章

  1. LN : leetcode 283 Move Zeroes

    lc 283 Move Zeroes 283 Move Zeroes Given an array nums, write a function to move all 0's to the end ...

  2. LeetCode 283. Move Zeroes (移动零)

    Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...

  3. leetcode 283. Move Zeroes -easy

    题目链接:https://leetcode.com/problems/move-zeroes/ 题目内容: Given an array nums, write a function to move ...

  4. [LeetCode] 283. Move Zeroes 移动零

    Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...

  5. Java [Leetcode 283]Move Zeroes

    题目描述: Given an array nums, write a function to move all 0's to the end of it while maintaining the r ...

  6. Leetcode 283 Move Zeroes python

    题目: Given an array nums, write a function to move all 0's to the end of it while maintaining the rel ...

  7. 11. leetcode 283. Move Zeroes

    Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...

  8. LeetCode 283 Move Zeroes 解题报告

    题目要求 Given an array nums, write a function to move all 0's to the end of it while maintaining the re ...

  9. [leetcode]283. Move Zeroes移零

    Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...

随机推荐

  1. Day5作业,商城+ATM机+后台管理

    晚来了....东西太多,需要写的blog内容太多,re讲的渣渣,不明白为什么oldboy经常换老师,吐槽下吧,真心不爱了.... github地址在这:https://github.com/ccorz ...

  2. LeetCode_28. Implement strStr()

    28. Implement strStr() Easy Implement strStr(). Return the index of the first occurrence of needle i ...

  3. Release报错Debug无错

    代码在Release模式下会crash,Debug模式下可以运行,最后定位到原因 for (size_t j = 0; j < ids.size()-1; ++j) { } 发现问题是Relea ...

  4. html设置多个div并排显示

    我这里以4个div为例,html代码如下: <body> <div id="column1" style="background-color: blue ...

  5. jQuery BlockUI Plugin Demo 4(Element Blocking Examples)

    Element Blocking Examples This page demonstrates how to block selected elements on the page rather t ...

  6. iOS-UITabbar自定义

    [self createCustomTabBar]; -(void)createCustomTabBar{    //创建一个UIImageView,作为底图    UIImageView *bgVi ...

  7. Spring mvc +ajax 发送邮件

    1.前端页面--form表单提交,通过发送按钮的id=“send”定位DOM,触发ajax请求 <form class="form-horizontal" id=" ...

  8. Laravel增加CORS中间件完成跨域请求

    原文地址: 跨域的请求 出于安全性的原因,浏览器会限制 Script 中的跨域请求.由于 XMLHttpRequest 遵循同源策略,所有使用 XMLHttpRequest 构造 HTTP 请求的应用 ...

  9. SPSS 2019年10月31日 20:20:53今日学习总结

    ◆描述性统计分析 概念:描述性统计分析方法是指应用分类.制表.图形及概括性数据指标(去均值,方差等)来概括数据分布特征的方法. 而推断性统计分析方法则是通过随机抽样,应用统计方法把从样本数据得到的结论 ...

  10. go os.State类用法

    参考文章: https://blog.csdn.net/weixin_43851310/article/details/87988648