问题

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3907 访问。

给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。

输入: [0,1,0,3,12]

输出: [1,3,12,0,0]

说明:

  1. 必须在原数组上操作,不能拷贝额外的数组。
  2. 尽量减少操作次数。

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.

Input: [0,1,0,3,12]
Output: [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.

示例

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3907 访问。

public class Program {

    public static void Main(string[] args) {
int[] nums = null; nums = new int[] { 0, 1, 0, 3, 12 };
MoveZeroes(nums);
ShowArray(nums); nums = new int[] { 0, 0, 6, 0, 18, 22, 0, 0, 5 };
MoveZeroes2(nums);
ShowArray(nums); nums = new int[] { 1, 0, 4, 0, 0, 7, 0, 9, 0 };
MoveZeroes3(nums);
ShowArray(nums); Console.ReadKey();
} private static void ShowArray(int[] array) {
foreach(var num in array) {
Console.Write($"{num} ");
}
Console.WriteLine();
} private static void MoveZeroes(int[] nums) {
//在原数据中移动,有点类似于插入排序
bool zero = false;
for(int i = 0, count = 0; i < nums.Length - count; i++) {
if(nums[i] == 0) {
//记录下一个数是不是0,如果是零的话,要重新移动一次以确保所有0都被处理过
zero = false;
if(i + 1 < nums.Length && nums[i + 1] == 0) {
zero = true;
}
//移动数组
for(int j = i + 1; j < nums.Length - count; j++) {
nums[j - 1] = nums[j];
}
nums[nums.Length - count - 1] = 0;
count++;
//移动指针,确保所有0都被处理过
if(zero) {
i--;
}
}
}
} private static void MoveZeroes2(int[] nums) {
//快慢双指针法
int n = nums.Length;
int index = 0;
for(int i = index; i < n; i++) {
if(nums[i] != 0) {
Swap(ref nums[i], ref nums[index]);
index++;
}
}
} private static void Swap(ref int num1, ref int num2) {
int swap = num1;
num1 = num2;
num2 = swap;
} private static void MoveZeroes3(int[] nums) {
//把所有不是0的移动到数组前部,余下的部分全部置0
int index = 0;
for(int i = 0; i < nums.Length; i++) {
//使用index索引把所有不是0的数据移到前部
if(nums[i] != 0)
nums[index++] = nums[i];
}
//余下的部分置0
while(index < nums.Length)
nums[index++] = 0;
} }

以上给出2种算法实现,以下是这个案例的输出结果:

该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3907 访问。

1 3 12 0 0
6 18 22 5 0 0 0 0 0
1 4 7 9 0 0 0 0 0

分析:

显而易见,MoveZeroes的时间复杂度为:  ,MoveZeroes2和MoveZeroes3的时间复杂度均为:  。

C#LeetCode刷题之#283-移动零(Move Zeroes)的更多相关文章

  1. leetcode刷题-73矩阵置零

    题目 给定一个 m x n 的矩阵,如果一个元素为 0,则将其所在行和列的所有元素都设为 0.请使用原地算法. 示例 1: 输入: [  [1,1,1],  [1,0,1],  [1,1,1]]输出: ...

  2. C#LeetCode刷题-双指针

    双指针篇 # 题名 刷题 通过率 难度 3 无重复字符的最长子串   24.5% 中等 11 盛最多水的容器   43.5% 中等 15 三数之和   16.1% 中等 16 最接近的三数之和   3 ...

  3. C#LeetCode刷题-数组

    数组篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 43.1% 简单 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组 ...

  4. leetcode刷题目录

    leetcode刷题目录 1. 两数之和 2. 两数相加 3. 无重复字符的最长子串 4. 寻找两个有序数组的中位数 5. 最长回文子串 6. Z 字形变换 7. 整数反转 8. 字符串转换整数 (a ...

  5. LeetCode刷题专栏第一篇--思维导图&时间安排

    昨天是元宵节,过完元宵节相当于这个年正式过完了.不知道大家有没有投入继续投入紧张的学习工作中.年前我想开一个Leetcode刷题专栏,于是发了一个投票想了解大家的需求征集意见.投票于2019年2月1日 ...

  6. LeetCode刷题总结-数组篇(中)

    本文接着上一篇文章<LeetCode刷题总结-数组篇(上)>,继续讲第二个常考问题:矩阵问题. 矩阵也可以称为二维数组.在LeetCode相关习题中,作者总结发现主要考点有:矩阵元素的遍历 ...

  7. LeetCode刷题总结-数组篇(下)

    本期讲O(n)类型问题,共14题.3道简单题,9道中等题,2道困难题.数组篇共归纳总结了50题,本篇是数组篇的最后一篇.其他三个篇章可参考: LeetCode刷题总结-数组篇(上),子数组问题(共17 ...

  8. C#LeetCode刷题-贪心算法

    贪心算法篇 # 题名 刷题 通过率 难度 44 通配符匹配   17.8% 困难 45 跳跃游戏 II   25.5% 困难 55 跳跃游戏   30.6% 中等 122 买卖股票的最佳时机 II C ...

  9. C#LeetCode刷题-动态规划

    动态规划篇 # 题名 刷题 通过率 难度 5 最长回文子串   22.4% 中等 10 正则表达式匹配   18.8% 困难 32 最长有效括号   23.3% 困难 44 通配符匹配   17.7% ...

随机推荐

  1. ffmpeg源码编译环境搭建

    ffmpeg是视频开发最常用到的开源软件,FFmpeg功能强大,用途广泛,提供几乎所有你能够想到的与视频开发相关的操作,许多商业软件都以ffmpeg为基础进行开发定制. FFmpeg: FFmpeg ...

  2. Python Ethical Hacking - BeEF Framework(1)

    Browser Exploitation Framework. Allows us to launch a number of attacks on a hooked target. Targets ...

  3. 题解 CF785E 【Anton and Permutation】

    考虑用分块解决这个题,一次交换对当前逆序对个数的影响是,加上两倍的在区间\([l+1,r-1]\)中比\(a_r\)小的元素个数,减去两倍的在区间\([l+1,r-1]\)中比\(a_l\)小的元素个 ...

  4. 题解 UVA11865 【Stream My Contest】

    最小树形图(朱刘算法)\(+\) 二分答案. 由题意得,我们要在一些有向边中选出一些边,使\(0\)号节点能够到达其他节点,使距离之和\(\leqslant cost\),并且使每条边中的带宽的最小值 ...

  5. spring +ActiveMQ 实战 topic selecter指定接收

    spring +ActiveMQ 实战 topic selecter指定接收 queue:点对点模式,一个消息只能由一个消费者接受 topic:一对多,发布/订阅模式,需要消费者都在线(可能会导致信息 ...

  6. web自动化 -- 切换 iframe

    先看源码 switch_to_frame() frame() 具体用法

  7. BUUCTF-web EasySearch (服务端包含注入ssi)

    一打开就是登录页面 存在index.php.swp...(反正我是没有扫出来,题目没给提示),分析一波源码 <?php ob_start(); function get_hash(){ $cha ...

  8. 说出来也许你不信,我被 Linux 终端嘲笑了……

    人这一辈子,真的是非常不容易:读书时,被老师.同学嘲笑,工作时,被老板.同事嘲笑,就连出去撸个串儿,还可能被朋友嘲笑-- 这些也就算了,毕竟大家还都是同类,都是活生生的人.但是,你如果被 Linux ...

  9. C# 13位时间戳(unix时间戳)

    1.转义字符用在中间. "\"' 2.C#获取13位时间戳(unix时间戳) /// <summary>   /// 将c# DateTime时间格式转换为Unix时间 ...

  10. link小图标以及表格的用法基础

    一.网页小图标的实现 实例: 实现方式: 效果: 二.表格基础 1.表格的组合标签 常用: table tr td caption ①table属性 border  边框 width  宽度 默认按照 ...