` ````
```

Loading Question ...

In MATLAB, there is a very useful function called 'reshape', which can reshape a matrix into a new one with different size but keep its original data.

You're given a matrix represented by a two-dimensional array, and two **positive** integers **r** and **c** representing the **row** number and **column** number of the wanted reshaped matrix, respectively.

The reshaped matrix need to be filled with all the elements of the original matrix in the same **row-traversing** order as they were.

If the 'reshape' operation with given parameters is possible and legal, output the new reshaped matrix; Otherwise, output the original matrix.

**Example 1:**

Input:nums = [[1,2], [3,4]] r = 1, c = 4Output:[[1,2,3,4]]Explanation:

Therow-traversingof nums is [1,2,3,4]. The new reshaped matrix is a 1 * 4 matrix, fill it row by row by using the previous list.

**Example 2:**

Input:nums = [[1,2], [3,4]] r = 2, c = 4Output:[[1,2], [3,4]]Explanation:

There is no way to reshape a 2 * 2 matrix to a 2 * 4 matrix. So output the original matrix.

**Note:**

- The height and width of the given matrix is in range [1, 100].
- The given r and c are all positive.

Seen this question in a real interview before?

Yes

No

When did you encounter this question?

last week

last month

last 3 month

last 6 month

more than 6 months

other

Which company?

Adobe

Aetion

Affirm

Airbnb

Alation

Alibaba

Amazon

AppDynamics

Apple

Arista

Baidu

Bank of America

BlackRock

Blend Labs

Blizzard

Bloomberg

Booking

Box

Bungie

Capital One

CareerBuilder

Cisco

Citadel

Coinbase

Concur

Conviva

Coupang

Coursera

CreditEase

CVTE

Dell

Deutsche Bank

DoorDash

Dropbox

Duolingo

EasyNet

eBay

Electronic Arts

EMC

Epic Systems

Equinix

Evernote

Expedia

Facebook

FactSet

Fitbit

Flipkart

Fortinet

FourSquare

FreeWheel

GoDaddy

Goldman Sachs

Google

GrabTaxi

Groupon

HBO

Hedvig

HomeAway

HTC

Huawei

Hulu

IBG

IBM

Indeed

InnovatureLabs

Intel

IXL

Jane Street

JPMorgan

Jump Trading

Lending Club

LinkedIn

LiveRamp

Loovee

Marvel

Matlab

McKesson

Microsoft

Morgan Stanley

NetEase

Nintendo

Nutanix

Nvidia

Oracle

Orbitz

Palantir

Paypal

Pinterest

Pocket Gems

Point72

Qualcomm

Qualtrics

Qumulo

Quora

Rackspace

Redfin

Rubrik

Salesforce

Samsung

SAP

ServiceNow

Sina

Snapchat

SoftwareOne

Sony

SoundHound

Square

Sumologic

SurveyMonkey

Symantec

Tableau

Tencent

Tesorio

TinyCo

Tradeshift

TripAdvisor

Twilio

Twitter

Two Sigma

Uber

Veritas

Visa

VMware

Walmart

Wealthfront

Whitepages

Works Applications

Yahoo

Yandex

Yelp

Zappos

Zenefits

Zillow

Zynga

How can we help you?