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% ...
随机推荐
- Python Ethical Hacking - Malware Analysis(3)
Stealing WiFi Password Saved on a Computer #!/usr/bin/env python import smtplib import subprocess im ...
- Go的100天之旅-07条件和循环
目录 条件语句 if条件语句 switch条件语句 循环语句 for循环 goto语句 条件语句 Go的条件语句和其它语言类似,主要是不支持三目运算符所以?:这种条件判断是不支持的.Go提供的条件判断 ...
- idea 自动生成try/catch代码块的快捷键
好像每个人的快捷键可能不同:我的是 Alt+Shift+Z 网上查的是 Ctrl+Alt+T 如果都不是可以点选工具栏生成try/catch(并可查看到自己的快捷键是什么):Code->Su ...
- APP自动化 -- TouchAction(触屏)
- 初步知道scss 简化css复杂层级
简介:今天在调试前端样式的时候,el-button组件位置需要调整并且 需要改字体大小 .直接上了一个 font-text:20px; 发现没作用,谷歌调试发现并未作用到组件里的<span> ...
- springcloud之简介
springcloud官方文档翻译网站:https://springcloud.cc/ 一.网站架构的演变过程.(这些架构描述的不是很到位,之后需要从新学习) 传统架构 —> 分布式架构 —&g ...
- table标签用法
<table>标签 HTML中表格由 <table> 标签来定义. 每个表格均有若干行(由 <tr> 标签定义),每行被分割为若干单元格(由 <td> ...
- 手写Vuex源码
Vuex原理解析 Vuex是基于Vue的响应式原理基础,所以无法拿出来单独使用,必须在Vue的基础之上使用. 1.Vuex使用相关解析 main.js import store form './s ...
- Apple产品价钱分析
- matpltlib 示例
matplotlib https://matplotlib.org/index.html