当前位置:网站首页>29. print matrix clockwise
29. print matrix clockwise
2022-06-12 05:18:00 【Be your goat】
The finger of the sword Offer 29. Print matrix clockwise
Ideas : simulation
- Null processing : When matrix It's empty time , Return the empty list directly
- initialization
- Loop printing
- Returns a list of
class Solution {
public:
vector<pair<int,int>> mv={
{0,1},{1,0},{0,-1},{-1,0}};
vector<int> spiralOrder(vector<vector<int>>& matrix) {
int direction=0;
vector<int> ans;
if(matrix.empty()||matrix[0].empty())
return ans;
int m=matrix.size(),n=matrix[0].size();
int sz=m*n,k=0,i=0,j=0;
vector<vector<bool>> isVisited(m,vector<bool>(n,false));
for(int k=0;k<sz;k++){
ans.push_back(matrix[i][j]);
isVisited[i][j]=true;
int newi=i+mv[direction].first;
int newj=j+mv[direction].second;
if(newi<0||newi>=m||newj<0||newj>=n||isVisited[newi][newj]){
direction=(direction+1)%4;
}
i+=mv[direction].first;
j+=mv[direction].second;
}
return ans;
}
};
Time complexity O(mn)
Spatial complexity O(1)
边栏推荐
- Computer network connected but unable to access the Internet
- Some problems of Qinglong panel
- JS how to get the date
- Object class not ended
- Rv1109 lvgl UI development
- Ecosystem type distribution data, land use data, vegetation type distribution and nature reserve distribution data
- Ten trends of Internet Security in 2022 industry released
- LabVIEW about TDMS and binary storage speeds
- Sentinel-2 data introduction and download
- ShanMeng and Beijing Adoption Day start NFT digital collection public offering
猜你喜欢

C asynchronous programming (async and await) and asynchronous method synchronous invocation

Classes and objects, methods and encapsulation

Yolov5 realizes road crack detection

Harris corner detection principle-

When the build When gradle does not load the dependencies, and you need to add a download path in libraries, the path in gradle is not a direct downloadable path
![[GIS tutorial] land use transfer matrix](/img/89/c5b55262e39405547c46538355e278.jpg)
[GIS tutorial] land use transfer matrix

Some problems of Qinglong panel

National land use data of 30m precision secondary classification

Data processing and data set preparation

Quickly get PCA (principal component analysis) (principle code case)
随机推荐
Minigui3 runs on Hisilicon hi3520d/hi3531 platform
When the build When gradle does not load the dependencies, and you need to add a download path in libraries, the path in gradle is not a direct downloadable path
Simulation of array implementation stack
Save the object in redis, save the bean in redis hash, and attach the bean map interoperation tool class
ESP8266 Arduino OLED
[cjson] precautions for root node
Multi thread learning 4. Sleep, wait, yield, join (), ThreadGroup control the running of threads
Main business objects of pupanvr record (5)
Day17 array features array boundary array application traversal array multidimensional array creation and traversal arrays operation array bubble sort
Abstract methods and interfaces
Normalized vegetation index (NDVI) data, NPP data, GPP data, evapotranspiration data, vegetation type data, ecosystem type distribution data
Yolo opencv scale identification scale reading identification water gauge identification water level identification source code
BI 如何让SaaS产品具有 “安全感”和“敏锐感”(上)
Spatial distribution data of national multi-year average precipitation 1951-2021, temperature distribution data, evapotranspiration data, evaporation data, solar radiation data, sunshine data and wind
[C language] realize string interception function
How to deploy dolphin scheduler Apache dolphin scheduler 1.2.0 in cdh5.16.2
Shandong University network security range experimental platform -- team and project introduction
MySQL5.7.21 Build For ARM
Variables and data types
Difference between thread and task