Reshape the Matrix

来源:互联网 发布:郭德纲网络剧 编辑:程序博客网 时间:2024/06/14 14:55
class Solution(object):    def matrixReshape(self, nums, r, c):        """        :type nums: List[List[int]]        :type r: int        :type c: int        :rtype: List[List[int]]        """        if not nums or r*c != len(nums[0])*len(nums):            return nums        result = [[0 for _ in xrange(c)] for _ in xrange(r)]        count = 0        for i in xrange(len(nums)):            for j in xrange(len(nums[0])):                result[count/c][count%c] = nums[i][j]                count +=1        return result


原创粉丝点击