31. Next Permutation
This commit is contained in:
11
31. Next Permutation/31. Next Permutation.csproj
Normal file
11
31. Next Permutation/31. Next Permutation.csproj
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<TargetFramework>net10.0</TargetFramework>
|
||||||
|
<RootNamespace>_31._Next_Permutation</RootNamespace>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
30
31. Next Permutation/Program.cs
Normal file
30
31. Next Permutation/Program.cs
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
var sol = new Solution();
|
||||||
|
|
||||||
|
var cases = new (int[] input, int[] expected)[]
|
||||||
|
{
|
||||||
|
(new[] { 1, 2, 3 }, new[] { 1, 3, 2 }),
|
||||||
|
(new[] { 3, 2, 1 }, new[] { 1, 2, 3 }),
|
||||||
|
(new[] { 1, 1, 5 }, new[] { 1, 5, 1 })
|
||||||
|
};
|
||||||
|
|
||||||
|
foreach (var (input, expected) in cases)
|
||||||
|
{
|
||||||
|
var nums = (int[])input.Clone();
|
||||||
|
sol.NextPermutation(nums);
|
||||||
|
var status = AreEqual(nums, expected) ? "ok" : "fail";
|
||||||
|
Console.WriteLine($"{Format(input)} -> {Format(nums)} (expected: {Format(expected)}) {status}");
|
||||||
|
}
|
||||||
|
|
||||||
|
static string Format(int[] nums) => $"[{string.Join(",", nums)}]";
|
||||||
|
|
||||||
|
static bool AreEqual(int[] left, int[] right)
|
||||||
|
{
|
||||||
|
if (left.Length != right.Length)
|
||||||
|
return false;
|
||||||
|
for (var i = 0; i < left.Length; i++)
|
||||||
|
{
|
||||||
|
if (left[i] != right[i])
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
17
31. Next Permutation/Solution.cs
Normal file
17
31. Next Permutation/Solution.cs
Normal file
@@ -0,0 +1,17 @@
|
|||||||
|
public class Solution
|
||||||
|
{
|
||||||
|
public void NextPermutation(int[] nums)
|
||||||
|
{
|
||||||
|
var i = nums.Length - 2;
|
||||||
|
while(i >= 0 && nums[i] >= nums[i+1])
|
||||||
|
i--;
|
||||||
|
if(i >= 0)
|
||||||
|
{
|
||||||
|
var j = nums.Length - 1;
|
||||||
|
while(nums[i] >= nums[j])
|
||||||
|
j--;
|
||||||
|
(nums[i], nums[j]) = (nums[j], nums[i]);
|
||||||
|
}
|
||||||
|
Array.Reverse(nums, i+1, nums.Length-i-1);
|
||||||
|
}
|
||||||
|
}
|
||||||
14
Leetcode.sln
14
Leetcode.sln
@@ -189,6 +189,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "23. Merge k Sorted Lists",
|
|||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "26. Remove Duplicates from Sorted Array", "26. Remove Duplicates from Sorted Array\26. Remove Duplicates from Sorted Array.csproj", "{5CE01D6E-7C12-45DC-8C26-DFE67F16AD3F}"
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "26. Remove Duplicates from Sorted Array", "26. Remove Duplicates from Sorted Array\26. Remove Duplicates from Sorted Array.csproj", "{5CE01D6E-7C12-45DC-8C26-DFE67F16AD3F}"
|
||||||
EndProject
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "31. Next Permutation", "31. Next Permutation\31. Next Permutation.csproj", "{DC76DEDE-AF9D-429B-B53B-204E89E90EA0}"
|
||||||
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
Debug|Any CPU = Debug|Any CPU
|
Debug|Any CPU = Debug|Any CPU
|
||||||
@@ -1315,6 +1317,18 @@ Global
|
|||||||
{5CE01D6E-7C12-45DC-8C26-DFE67F16AD3F}.Release|x64.Build.0 = Release|Any CPU
|
{5CE01D6E-7C12-45DC-8C26-DFE67F16AD3F}.Release|x64.Build.0 = Release|Any CPU
|
||||||
{5CE01D6E-7C12-45DC-8C26-DFE67F16AD3F}.Release|x86.ActiveCfg = Release|Any CPU
|
{5CE01D6E-7C12-45DC-8C26-DFE67F16AD3F}.Release|x86.ActiveCfg = Release|Any CPU
|
||||||
{5CE01D6E-7C12-45DC-8C26-DFE67F16AD3F}.Release|x86.Build.0 = Release|Any CPU
|
{5CE01D6E-7C12-45DC-8C26-DFE67F16AD3F}.Release|x86.Build.0 = Release|Any CPU
|
||||||
|
{DC76DEDE-AF9D-429B-B53B-204E89E90EA0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{DC76DEDE-AF9D-429B-B53B-204E89E90EA0}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{DC76DEDE-AF9D-429B-B53B-204E89E90EA0}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||||
|
{DC76DEDE-AF9D-429B-B53B-204E89E90EA0}.Debug|x64.Build.0 = Debug|Any CPU
|
||||||
|
{DC76DEDE-AF9D-429B-B53B-204E89E90EA0}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||||
|
{DC76DEDE-AF9D-429B-B53B-204E89E90EA0}.Debug|x86.Build.0 = Debug|Any CPU
|
||||||
|
{DC76DEDE-AF9D-429B-B53B-204E89E90EA0}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{DC76DEDE-AF9D-429B-B53B-204E89E90EA0}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{DC76DEDE-AF9D-429B-B53B-204E89E90EA0}.Release|x64.ActiveCfg = Release|Any CPU
|
||||||
|
{DC76DEDE-AF9D-429B-B53B-204E89E90EA0}.Release|x64.Build.0 = Release|Any CPU
|
||||||
|
{DC76DEDE-AF9D-429B-B53B-204E89E90EA0}.Release|x86.ActiveCfg = Release|Any CPU
|
||||||
|
{DC76DEDE-AF9D-429B-B53B-204E89E90EA0}.Release|x86.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
|
|||||||
Reference in New Issue
Block a user