C#LeetCode刷题之#283-移动零(Move Zeroes)
问题
该文章的最新版本已迁移至个人博客【比特飞】,单击链接 https://www.byteflying.com/archives/3907 访问。
给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。
输入: [0,1,0,3,12]
输出: [1,3,12,0,0]
说明:
- 必须在原数组上操作,不能拷贝额外的数组。
- 尽量减少操作次数。
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:
- You must do this in-place without making a copy of the array.
- 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)的更多相关文章
- leetcode刷题-73矩阵置零
题目 给定一个 m x n 的矩阵,如果一个元素为 0,则将其所在行和列的所有元素都设为 0.请使用原地算法. 示例 1: 输入: [ [1,1,1], [1,0,1], [1,1,1]]输出: ...
- C#LeetCode刷题-双指针
双指针篇 # 题名 刷题 通过率 难度 3 无重复字符的最长子串 24.5% 中等 11 盛最多水的容器 43.5% 中等 15 三数之和 16.1% 中等 16 最接近的三数之和 3 ...
- C#LeetCode刷题-数组
数组篇 # 题名 刷题 通过率 难度 1 两数之和 C#LeetCode刷题之#1-两数之和(Two Sum) 43.1% 简单 4 两个排序数组的中位数 C#LeetCode刷题之#4-两个排序数组 ...
- leetcode刷题目录
leetcode刷题目录 1. 两数之和 2. 两数相加 3. 无重复字符的最长子串 4. 寻找两个有序数组的中位数 5. 最长回文子串 6. Z 字形变换 7. 整数反转 8. 字符串转换整数 (a ...
- LeetCode刷题专栏第一篇--思维导图&时间安排
昨天是元宵节,过完元宵节相当于这个年正式过完了.不知道大家有没有投入继续投入紧张的学习工作中.年前我想开一个Leetcode刷题专栏,于是发了一个投票想了解大家的需求征集意见.投票于2019年2月1日 ...
- LeetCode刷题总结-数组篇(中)
本文接着上一篇文章<LeetCode刷题总结-数组篇(上)>,继续讲第二个常考问题:矩阵问题. 矩阵也可以称为二维数组.在LeetCode相关习题中,作者总结发现主要考点有:矩阵元素的遍历 ...
- LeetCode刷题总结-数组篇(下)
本期讲O(n)类型问题,共14题.3道简单题,9道中等题,2道困难题.数组篇共归纳总结了50题,本篇是数组篇的最后一篇.其他三个篇章可参考: LeetCode刷题总结-数组篇(上),子数组问题(共17 ...
- C#LeetCode刷题-贪心算法
贪心算法篇 # 题名 刷题 通过率 难度 44 通配符匹配 17.8% 困难 45 跳跃游戏 II 25.5% 困难 55 跳跃游戏 30.6% 中等 122 买卖股票的最佳时机 II C ...
- C#LeetCode刷题-动态规划
动态规划篇 # 题名 刷题 通过率 难度 5 最长回文子串 22.4% 中等 10 正则表达式匹配 18.8% 困难 32 最长有效括号 23.3% 困难 44 通配符匹配 17.7% ...
随机推荐
- 太实用了!自己动手写软件——GUI编程
这几天我有一个想法就是将我之前做测试写的一些协议脚本(如:ssh.FTP.SMTP.MySQL.Oracle等)综合在一起做一个密码PJ器,这么多的协议放在一起,每个协议都有自己特殊的参数,如果还是和 ...
- 史上最全的 jmeter 获取 jdbc 数据使用的4种方法——(软件测试Python自动化)
周五,下班了吗?软件测试人. 明天是周末了!给大家推荐一个技术干货好文.史上最全的 jmeter 获取 jdbc 数据使用的四种方法.我也精剪了jmeter的自动化接口测试的视频放在了同名UP主,周末 ...
- js 对象数组根据某个名称删除数组中的对象
delArrayItem: function (array,item) { const index = array.findIndex(text => text.name === item.na ...
- Python Ethical Hacking - BACKDOORS(5)
File Download: A file is a series of characters. Therefore to transfer a file we need to: 1. Read th ...
- 集训作业 洛谷P1135 奇怪的电梯
这个题我见过!!! 我之前在石油大学的网站上做练习赛,提高了很多,这个题是我第一次在比赛里见到深搜. 当时蒙蔽的一批,现在发现好简单…… 这个题和普通的深搜没什么区别,甚至可以说简单了,因为这个是1维 ...
- onehot编码检测
‘’16bits位宽寄存器,用五(六也行)级逻辑判断其中有15个0和1个1‘’,这么道题目,面试无数,几乎没有人能答出来,连给我衍生‘’14个0和2个1‘’的机会都没有. 今天的问题来源于知乎,某大牛 ...
- DeviceEventEmmiter使用
发送广播一个事件 DeviceEventEmitter.emit('updatePlantList', '创建工厂成功');//通知刷新工厂列表 接收处,添加监听(监听要再事件发生之前添加,否则无法回 ...
- python基础day4_列表list
list列表 li = ['alex',[1,2,3],'hjh','nvshen '] l1= li[0] print(l1) # alex l3= li[0:3]#['alex', [1, 2, ...
- Python os.fpathconf() 方法
概述 os.fpathconf() 方法用于返回一个打开的文件的系统配置信息.高佣联盟 www.cgewang.com Unix上可用. 语法 fpathconf()方法语法格式如下: os.fpat ...
- PHP preg_quote() 函数
preg_last_error 函数用于转义正则表达式字符.高佣联盟 www.cgewang.com 语法 string preg_quote ( string $str [, string $del ...