刷题记录第二十二天-螺旋矩阵

#include
#include 
using namespace std;
int main(){
	vector<vector<int>> nums({{1,2,3,4},{5,6,7,8},{9,10,11,12}});
	vector<int> result;
	int up=0,left=0,right=nums[0].size()-1,down=nums.size()-1;
	while(result.size()<nums.size()*nums[0].size()){
		if(up<=down){
			for(int i=left;i<=right;i++){
			result.push_back(nums[up][i]);	
			}
			up++;
		}
		if(left<=right){
			for(int i=up;i<=down;i++){
			result.push_back(nums[i][right]);	
			}
			right--;
		}
		if(up<=down){
			for(int i=right;i>=left;i--){
			result.push_back(nums[down][i]);	
			}
			down--;	
		}
		if(left<=right){
			for(int i=down;i>=up;i--){
			result.push_back(nums[i][left]);	
			}
			left++;	
		}
	}
	for(int i=0;i<result.size();i++){
		cout<<result[i]<<" ";
	}
	system("pause");
}

你可能感兴趣的:(每日一题,算法,c++,数据结构)