旋转数组(python、c++)

旋转数组

给一个 n×n 的数组,旋转90度。
主要思路:

  1. left ----> top : 把左边的一列数,转到顶部。
  2. bottom ----> left: 把底部的一行数,转到左边的一列数。
  3. right ----> bottom:把右边的一列数, 转到底部的行数。
  4. top ----> right:把顶部的一行数,转到右边的一列数。
  5. 有多个层数,循环依次执行上面 1- 4 步。

Python实现

def rotate(matrix):
    n = len(matrix)
    # 分层,比如,5*5 数组,外面正方形一层,里面小正方形一层,中间的数不变。
    for layer in range(n//2):
        # 定义多一个,便于理解,层跟第一个数处理有关。
        # 比如处理外面一层,就是从小标 0 开始处理, 里面一层就是从下标 1 开始
        first = layer
        last = n - 1 - layer
        
        # 处理层数相关的数。
        for i in range(first, last):
            # 先保存top, 是为了接下来的赋值覆盖消失。
            top = matrix[first][i]
            # 所要移动的下标
            offset = i - first
            
            # top:		matrix[first][i]
            # bottom: 	matrix[last][last-offset]
            # left: 	matrix[last-offset][first]
            # right:	matrix[i][last]
            
            # left -> top, 把 left 的值转换到 top 的位置
            matrix[first][i] = matrix[last-offset][first]
            
            # bottom -> left, 把 bottom 的值转换到 left 的位置
            matrix[last-offset][first] = matrix[last][last-offset]
            
            # right -> bottom, 把 right 的值转换到 bottom 的位置
            matrix[last][last-offset] = matrix[i][last]
            
            # top -> right, 把 top 的值转换到 right 的位置
            matrix[i][last] = top
	# 打印输出  
    for x in matrix:
        print(x)

python调用实现

matrix = [[i*5+j for j in range(5)] for i in range(5)]
for x in matrix:
    print(x)
print("\n---------------------------------------------\n")
rotate(matrix)

执行结果

Python运行结果

C++ 实现

#include <iostream>
using namespace std;

void rotate(int n, int **array)
{
	int first = 0;
	int last = 0;
	int top = 0;
	int left = 0;
	int right = 0;
	int bottom = 0;
	int offset = 0;

	for(int layer = 0; layer < n / 2; layer++)
	{
		first = layer;
		last = n - 1 - first; 
		for(int i = first; i < last; i++) 
		{
			offset = i - first;
			// 先保存对应的变量。下面地址赋值,转换,才不会出现问题
			top = *((int *)array + n * first + i); 
			left = *((int *)array + n * (last - offset) + first);
			right = *((int *)array + n * i + last); 	
			bottom = *((int *)array + n * last + last - offset);

			// left --> top
			// top = left;
			*((int *)array + n * first + i) = left;

			// bottom --> left
			// left = bottom;
			*((int *)array + n * (last - offset) + first) = bottom;

			// right --> bottom
			// bottom = right;
			*((int *)array + n * last + last - offset) = right;

			// top --> right
			// right = top;
			*((int *)array + n * i + last) = top;
		}
		
	}
	// 输出打印
	for(int i = 0; i < n; i++)
	{
		for(int j = 0; j < n; j++)
		{
			cout << *((int *)array + n * i + j) << ' ';
		}
		cout << endl;
	}		
}

int main(int argc, char **argv)
{
	int matrix[5][5] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24};
	cout << "旋转之前:" << endl;
	for(int i = 0; i < 5; i++)
	{
		for(int j = 0; j < 5; j++)
		{
			cout << matrix[i][j] << ' ';
		}
		cout << endl;
	}
	cout << endl << "旋转之后:" << endl;
	rotate(5, (int **)matrix);
	return 0;
}

运行结果:
c++运行结果

猜你喜欢

转载自blog.csdn.net/qq_35200479/article/details/86698148