练习-二维数组旋转
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了练习-二维数组旋转相关的知识,希望对你有一定的参考价值。
#slist = [[i for i in range(4)] for l in range(8)] # slist = [ # [4, 5, 2, 7], # [8, 7, 4, 2], # [1, 9, 7, 3], # [0, 4, 5, 6], # ] # 4*4 slist = [ [4, 5, 2, 7], [8, 7, 4, 2], [1, 9, 7, 3], [0, 4, 5, 6], [1, 9, 7, 3], ] # 4*5 # slist = [ # [1, 0, 1, 8, 4], # [9, 4, 9, 7, 5], # [7, 5, 7, 4, 2], # [3, 6, 3, 2, 7] # ] # 5*4 # slist = [ # [1, 0, 1, 8, 4], # [9, 4, 9, 7, 5], # [7, 5, 7, 4, 2], # [3, 6, 3, 2, 7], # [7, 5, 7, 4, 2], # ] # 5*5 # 旋转90° def rotate_90(lists): row = len(lists) # 行 column = len(lists[0]) # 列 if row == column: # 行列相等 olist = [[i for i in range(column)] for l in range(row)] # 生成新列表 else: # 行列不等 olist = [[i for i in range(row)] for l in range(column)] # 生成新列表 for i in range(row): for l in range(column): #print(‘%d%d换%d%d‘ % (i, l, l, abs(i - row + 1))) olist[l][abs(i - row + 1)] = lists[i][l] return olist olist = rotate_90(slist) print(‘正常数组:%r\n转90°后数组:%r‘ % (slist, olist)) # 分解步骤如下,数字等于二维数组索引 # 原数组 4行4列 新数组 # [4, 5, 2, 7]00到03,01到13,02到23,03到33[0, 1, 8, 4] # [8, 7, 4, 2]10到02,11到12,12到22,13到32[4, 9, 7, 5] # [1, 9, 7, 3]20到01,21到11,22到21,23到31[5, 7, 4, 2] # [0, 4, 5, 6]30到00,31到10,32到20,33到30[6, 3, 2, 7] # 原数组 5行4列 新数组 # [4, 5, 2, 7]00到04,01到14,02到24,03到34[1, 0, 1, 8, 4] # [8, 7, 4, 2]10到03,11到13,12到23,13到33[9, 4, 9, 7, 5] # [1, 9, 7, 3]20到02,21到12,22到22,23到32[7, 5, 7, 4, 2] # [0, 4, 5, 6]30到01,31到11,32到21,33到31[3, 6, 3, 2, 7] # [1, 9, 7, 3]40到00,41到10,42到20,43到30 # 原数组 4行5列 新数组 # [1, 0, 1, 8, 4]00到03,01到13,02到23,03到33,04到43[3, 7, 9, 1] # [9, 4, 9, 7, 5]10到02,11到12,12到22,13到32,14到42[6, 5, 4, 0] # [7, 5, 7, 4, 2]20到01,21到11,22到21,23到31,24到41[3, 7, 9, 1] # [3, 6, 3, 2, 7]30到00,31到10,32到20,33到30,34到40[2, 4, 7, 8] # [7, 2, 5, 4] # 旋转180度 def rotate_180(lists): row = len(lists) # 行 column = len(lists[0]) # 列 olist = [[i for i in range(column)] for l in range(row)] # 生成新列表 remainder = row % 2 for i in range(row // 2 + remainder): for l in range(column): # 先判断奇偶行--是奇数行,再判断是否在操作中间行--列表是奇数行,当前也在中间行,超过中间列就停止 # 因为偶数中间行是上下行对角互换,所以要按列依次互换 # 奇数中间行是在中间行左右互换,所以超过中间列就不再继续互换 if remainder == 1 and i == (row - 1) // 2 and l > (column - 1) // 2: break # 原位置,互换位置依次取值 olist[abs(i - row + 1)][abs(l - column + 1)] = lists[i][l] olist[i][l] = lists[abs(i - row + 1)][abs(l - column + 1)] return olist olist = rotate_180(slist) print(‘正常数组:%r\n转180°后数组:%r‘ % (slist, olist)) # 分解步骤如下,数字等于二维数组索引 # 原数组 偶数行 新数组 # [4, 5, 2, 7]00换33,01换32,02换31,03换30[6, 5, 4, 0] # [8, 7, 4, 2]10换23,11换22,12换21,13换20[3, 7, 9, 1] # [1, 9, 7, 3] [2, 4, 7, 8] # [0, 4, 5, 6] [7, 2, 5, 4] # 原数组 奇数行 新数组 # [4, 5, 2, 7]00换43,01换42,02换41,03换40[3, 7, 9, 1] # [8, 7, 4, 2]10换33,11换32,12换31,13换30[6, 5, 4, 0] # [1, 9, 7, 3]20换23,21换22 [3, 7, 9, 1] # [0, 4, 5, 6] [2, 4, 7, 8] # [1, 9, 7, 3] [7, 2, 5, 4] # 旋转270度 def rotate_270(lists): row = len(lists) column = len(lists[0]) if row == column: # 行列相等 olist = [[i for i in range(column)] for l in range(row)] # 生成新列表 else: olist = [[i for i in range(row)] for l in range(column)] # 生成新列表 for i in range(row): for l in range(column): # print(‘%d%d换%d%d‘ % (i, l, abs(l - column + 1), i)) olist[abs(l - column + 1)][i] = lists[i][l] return olist olist = rotate_270(slist) print(‘正常数组:%r\n转270°后数组:%r‘ % (slist, olist)) # 分解步骤如下,数字等于二维数组索引 # 原数组 4行4列 新数组 # [4, 5, 2, 7]00到30,01到20,02到10,03到00[7, 2, 3, 6] # [8, 7, 4, 2]10到31,11到21,12到11,13到01[2 ,4, 7, 5] # [1, 9, 7, 3]20到32,21到22,22到12,23到02[5, 7, 9, 4] # [0, 4, 5, 6]30到33,31到23,32到13,33到03[4, 8, 1, 0] # 原数组 5行4列 新数组 # [4, 5, 2, 7]00到30,01到20,02到10,03到00[7, 2, 3, 6, 3] # [8, 7, 4, 2]10到31,11到21,12到11,13到01[2, 4, 7, 5, 7] # [1, 9, 7, 3]20到32,21到22,22到12,23到02[5, 7, 9, 4, 9] # [0, 4, 5, 6]30到33,31到23,32到13,33到03[4, 8, 1, 0, 1] # [1, 9, 7, 3]40到34,41到24,42到14,43到04 # 原数组 4行5列 新数组 # [1, 0, 1, 8, 4]00到40,01到30,02到20,03到10,04到00[4, 5, 2, 7] # [9, 4, 9, 7, 5]10到41,11到31,12到21,13到11,14到01[8, 7, 4, 2] # [7, 5, 7, 4, 2]20到42,21到32,22到22,23到12,24到02[1, 9, 7, 3] # [3, 6, 3, 2, 7]30到43,31到33,32到23,33到13,34到03[0, 4, 5, 6] # [1, 9, 7, 3] #正方形对角互换(左上到右下连线分成右上和左下两个区域互换) print(‘对角交换前数组:%r‘ % slist) for i in range(len(slist)): for l in range(len(slist[0])): if i < l: tint = slist[i][l] slist[i][l] = slist[l][i] slist[l][i] = tint print(‘对角交换后数组:%r‘ % slist) # 分解步骤如下,数字等于二维数组索引 # 原数组 新数组 # [4, 5, 2, 7]01换10,02换20,03换30[4, 8, 1, 0] # [8, 7, 4, 2]12换21,13换31 [5, 7, 9, 4] # [1, 9, 7, 3]23换32 [2, 4, 7, 5] # [0, 4, 5, 6] [7, 2, 3, 6]
以上是关于练习-二维数组旋转的主要内容,如果未能解决你的问题,请参考以下文章
算法零基础学习关于二维数组的一些基础练习题 | leetcode1672158283248题解
算法零基础学习关于二维数组的一些基础练习题 | leetcode1672158283248题解