首页 > 其他 > 详细

Reshape the Matrix

时间:2017-09-21 20:42:30      阅读:251      评论:0      收藏:0      [点我收藏+]

    这道题为简单题

  题目:

    技术分享

 

  思路:

    首先判断该列表是否满足‘reshape’,计算二维列表的长度和宽度并且判断是否长度宽度相乘等于r*c,如果相等就遍历列表每个元素将其加入新列表中,否则就返回原二维列表

  代码:

 1 class Solution(object):
 2     def matrixReshape(self, nums, r, c):
 3         """
 4         :type nums: List[List[int]]
 5         :type r: int
 6         :type c: int
 7         :rtype: List[List[int]]
 8         """
 9         a = len(nums)
10         b = len(nums[0])
11         d = [[None] * c for _ in xrange(r)]
12         if a * b == r * c:
13             for i in range(r*c):
14                 d[i//c][i%c]=nums[i//b][i%b]
15             return d
16         else: return nums

 

Reshape the Matrix

原文:http://www.cnblogs.com/liuxinzhi/p/7570694.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!