Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Given a matrix of M x N elements, return all elements of the matrix in spiral order.
- // Input: [
- // [1, 2, 3],
- // [4, 5, 6],
- // [7, 8, 9]
- // ]
- // Output: [1, 2, 3, 6, 9, 8, 7, 4, 5]
- // Input: [
- // [1, 2, 3, 4],
- // [5, 6, 7, 8],
- // [9, 10, 11, 12]
- // ]
- // Output: [1, 2, 3, 4, 8, 12, 11, 10, 9, 5, 6, 7]
- #include <iostream>
- #include <vector>
- using namespace std;
- void calculateVector(vector<vector<int>> functionInput) {
- int x = 0, y = 0;
- char direction = 'e';
- int numberOfSteps = 0;
- int stepsTaken = 0;
- /*for(auto elementA: functionInput){
- numberOfSteps = elementA.size();
- for(auto elementB : elementA){
- switch(direction){
- case 'e': {
- //subtract at direction change to south
- if(numberOfSteps == stepsTaken){
- direction = 's';
- x++;
- stepsTaken = 0;
- numberOfSteps--;
- }else{
- stepsTaken++;
- cout << functionInput[x][y] << endl;
- }
- }
- case 's': {
- if(numberOfSteps == stepsTaken){
- direction = 'w';
- y++;
- stepsTaken = 0;
- }else{
- stepsTaken++;
- cout << functionInput[x][y] << endl;
- }
- }
- case 'w': {
- //subtract at direction change to north
- if(numberOfSteps == stepsTaken){
- direction = 'n';
- x--;
- stepsTaken = 0;
- numberOfSteps--;
- }else{
- stepsTaken++;
- cout << functionInput[x][y] << endl;
- }
- }
- case 'n': {
- if(numberOfSteps == stepsTaken){
- direction = 'e';
- y--;
- stepsTaken = 0;
- }else{
- stepsTaken++;
- cout << functionInput[x][y] << endl;
- }
- }
- }
- }
- */
- }
- }
- int main() {
- vector<int> tempVectorOfInts;
- vector<vector<int>> vectorHolder;
- tempVectorOfInts.push_back(1);
- tempVectorOfInts.push_back(2);
- tempVectorOfInts.push_back(3);
- vectorHolder.push_back(tempVectorOfInts);
- tempVectorOfInts.erase(tempVectorOfInts.begin(), tempVectorOfInts.end());
- tempVectorOfInts.push_back(4);
- tempVectorOfInts.push_back(5);
- tempVectorOfInts.push_back(6);
- vectorHolder.push_back(tempVectorOfInts);
- tempVectorOfInts.erase(tempVectorOfInts.begin(), tempVectorOfInts.end());
- tempVectorOfInts.push_back(7);
- tempVectorOfInts.push_back(8);
- tempVectorOfInts.push_back(9);
- vectorHolder.push_back(tempVectorOfInts);
- tempVectorOfInts.erase(tempVectorOfInts.begin(), tempVectorOfInts.end());
- calculateVector(vectorHolder);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment