-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path566.py
More file actions
37 lines (31 loc) · 879 Bytes
/
566.py
File metadata and controls
37 lines (31 loc) · 879 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
__author__ = "vcancy"
#/usr/bin/python
#-*-coding:utf-8-*-
"""
分析:
给的r 和 c 能不能用来塑造一个2d array。比较一下总数就可以了。
遍历二维数组,每c个数值添加到一个1维数组中,再将这个1维数组添加到数组中。
"""
class Solution:
def matrixReshape(self, nums, r, c):
"""
:type nums: List[List[int]]
:type r: int
:type c: int
:rtype: List[List[int]]
"""
index = 0
temp = []
res = []
if len(nums) * len(nums[0]) != r * c:
return nums
for i in nums:
for j in i:
if index < c:
temp.append(j)
index += 1
if index==c:
res.append(temp)
temp = []
index = 0
return res