31. Next Permutation
This commit is contained in:
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;
|
||||
}
|
||||
Reference in New Issue
Block a user