CShap 数组的左右旋转 源代码
时间: 2023-10-23 11:11:01 浏览: 82
以下是C#中数组的左右旋转的源代码示例:
左旋转:
```csharp
public static void RotateLeft(int[] arr, int d)
{
int n = arr.Length;
Reverse(arr, 0, d - 1);
Reverse(arr, d, n - 1);
Reverse(arr, 0, n - 1);
}
public static void Reverse(int[] arr, int start, int end)
{
while (start < end)
{
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
```
右旋转:
```csharp
public static void RotateRight(int[] arr, int d)
{
int n = arr.Length;
Reverse(arr, n - d, n - 1);
Reverse(arr, 0, n - d - 1);
Reverse(arr, 0, n - 1);
}
public static void Reverse(int[] arr, int start, int end)
{
while (start < end)
{
int temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
```
阅读全文