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 Hacking Tools - Port Scanner
Socket Programming 1. Scan the target Vulnerable Server. And test it by telnet. 2. Write the scanne ...
- 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 ...
- Eclipse默认快捷键说明
Ctrl+1 快速修复(最经典的快捷键,就不用多说了)Ctrl+D: 删除当前行 Ctrl+Alt+↓ 复制当前行到下一行(复制增加)Ctrl+Alt+↑ 复制当前行到上一行(复制增加)Alt+↓ 当 ...
- 动手实现一个较为简单的MQTT服务端和客户端
项目地址:https://github.com/hnlyf168/DotNet.Framework 昨天晚上大致测试了下 ,490个客户端(一个收一个发) 平均估计每个每秒60个包 使用mqtt协 ...
- JAVA集合一:ArrayList和LinkedList
JAVA集合一:ArrayList和LinkedList 参考链接: HOW2J.CN 前言 这几篇博客重点记录JAVA的几个重要的集合框架:ArrayList.LinkedList.HashMap. ...
- MySQL数据库---表的操作
存储引擎 表就是文件,表的存储引擎就是文件的存储格式,即数据的组织存储方式. 字段类型 1.整数类型 整数类型:TINYINT SMALLINT MEDIUMINT INT BIGINT 作用:存储年 ...
- 题解 洛谷 P4143 【采集矿石】
对于一个固定的左端点,右端点向右移动时,其子串权值和不断增大,字典序降序排名不断减小,因此对于一个左端点,最多存在一个右端点使其满足条件. 所以可以枚举左端点,然后二分右端点的位置,权值和通过前缀和来 ...
- 题解 洛谷 P4112 【[HEOI2015]最短不公共子串】
给定两个字符串\(A\)和\(B\),我们需要找出一个串,其在\(A\)中出现且不在\(B\)中出现,这个串为子串或者子序列,求在每种情况下,该串的最短长度. 考虑到后缀自动机可以识别一个字符串的所有 ...
- Spring事务管理接口定义
Spring事务管理接口介绍 Spring事务管理接口: PlatformTransactionManager: (平台)事务管理器 TransactionDefinition: 事务定义信息(事务隔 ...
- SPRING 阅读--JdkDynamicAopProxy
一.简介 JdkDynamicAopProxy 代理类是spring 默认的JDK动态的代理类实现.它实现了Java 动态代理接口InvocationHandler接口和Spring定义的AopPro ...