diff --git a/31. Next Permutation/31. Next Permutation.csproj b/31. Next Permutation/31. Next Permutation.csproj new file mode 100644 index 0000000..dfe3535 --- /dev/null +++ b/31. Next Permutation/31. Next Permutation.csproj @@ -0,0 +1,11 @@ + + + + Exe + net10.0 + _31._Next_Permutation + enable + enable + + + diff --git a/31. Next Permutation/Program.cs b/31. Next Permutation/Program.cs new file mode 100644 index 0000000..69128ca --- /dev/null +++ b/31. Next Permutation/Program.cs @@ -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; +} diff --git a/31. Next Permutation/Solution.cs b/31. Next Permutation/Solution.cs new file mode 100644 index 0000000..1fbb09b --- /dev/null +++ b/31. Next Permutation/Solution.cs @@ -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); + } +} \ No newline at end of file diff --git a/Leetcode.sln b/Leetcode.sln index 3193445..ea36914 100644 --- a/Leetcode.sln +++ b/Leetcode.sln @@ -189,6 +189,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "23. Merge k Sorted Lists", 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}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "31. Next Permutation", "31. Next Permutation\31. Next Permutation.csproj", "{DC76DEDE-AF9D-429B-B53B-204E89E90EA0}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution 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|x86.ActiveCfg = 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 GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE