Day5
This commit is contained in:
11
283. Move Zeroes/283. Move Zeroes.csproj
Normal file
11
283. Move Zeroes/283. Move Zeroes.csproj
Normal file
@@ -0,0 +1,11 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
<RootNamespace>_283._Move_Zeroes</RootNamespace>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
12
283. Move Zeroes/Program.cs
Normal file
12
283. Move Zeroes/Program.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
namespace _283._Move_Zeroes
|
||||
{
|
||||
internal class Program
|
||||
{
|
||||
static void Main(string[] args)
|
||||
{
|
||||
var test = new int[] { 0, 1, 0, 3, 12 };
|
||||
new Solution().MoveZeroes(test);
|
||||
Console.WriteLine(String.Join(" ", test));
|
||||
}
|
||||
}
|
||||
}
|
||||
20
283. Move Zeroes/Solution.cs
Normal file
20
283. Move Zeroes/Solution.cs
Normal file
@@ -0,0 +1,20 @@
|
||||
namespace _283._Move_Zeroes;
|
||||
|
||||
public class Solution
|
||||
{
|
||||
public void MoveZeroes(int[] nums)
|
||||
{
|
||||
for(int i = 0; i < nums.Length-1; i++)
|
||||
{
|
||||
if (nums[i] != 0)
|
||||
continue;
|
||||
int nonZeroIndex = i + 1;
|
||||
while (nonZeroIndex < nums.Length && nums[nonZeroIndex] == 0)
|
||||
nonZeroIndex++;
|
||||
if (nonZeroIndex >= nums.Length)
|
||||
break;
|
||||
nums[i] = nums[nonZeroIndex];
|
||||
nums[nonZeroIndex] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user