Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

spiral traversal of matrix leetcode


/*
	This is an implementation that demonstrates
	how to efficiently spiral traverse a two
	dimensional array. In particular, the program
	acts on an M*N 2-D array, returning 
	its elements in spiral order.

	Let M be the number of rows of 2-D array and
	N be its number of columns.

	Time complexity: O(M*N) 
	Space complexity: O(1)
*/
import java.util.List;
import java.util.ArrayList;

public class LargestRectangleArea {
	public static void main(String[] args) {
		int[][] twoDArray = {
				{ 1, 2, 3, 4 },
				{ 12, 13, 14, 5 },
				{ 11, 16, 15, 6 },
				{ 10, 9, 8, 7 }
		};
		// Below prints:
		// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]
		System.out.println(spiralOrder(twoDArray));
	}

	private static List<Integer> spiralOrder(int[][] matrix) {
		List<Integer> result = new ArrayList<>();
		int rows = matrix.length;
		int columns = matrix[0].length;
		int up = 0;
		int left = 0;
		int right = columns - 1;
		int down = rows - 1;

		while (result.size() < rows * columns) {
			// Left to right direction.
			for (int col = left; col <= right; col++) {
				result.add(matrix[up][col]);
			}
			// Top down direction
			for (int row = up + 1; row <= down; row++) {
				result.add(matrix[row][right]);
			}
			// Make sure we are on a different row
			if (up != down) {
				// Right to left direction
				for (int col = right - 1; col >= left; col--) {
					result.add(matrix[down][col]);
				}
			}
			// Make sure we are on a different column
			if (left != right) {
				// Bottom up direction
				for (int row = down - 1; row > up; row--) {
					result.add(matrix[row][left]);
				}
			}

			left++;
			right--;
			up++;
			down--;
		}

		return result;
	}

}
Comment

spiral matrix leetcode

class Solution {
public:
    vector<int> spiralOrder(vector<vector<int>>& matrix) {
        int n = matrix.size();
        int m = matrix[0].size();
        cout<<n<<" "<<m<<endl;
        vector<int> v(m, -1);
        vector<vector<int>> visitedmatrix(n, v);
        vector<int> result;
        int pos = 0;
        for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            cout << matrix[i][j] << " ";
        }
        cout << "
";
    }
    cout << "--------------------"
         << "
";
    int top = 0, right = m - 1, left = 0, down = n - 1;
    int count = n * m;
      
    while (count > 0)
    {
        // move right
         
        for (int i = top; i <= right; i++)
        {
            cout<<matrix[top][i]<<" "<<top<<i<<" "<<right<<endl;
            if (visitedmatrix[top][i] == -1)
            {
                // cout << matrix[top][i] << " ";
                result.insert(result.begin() + pos ,matrix[top][i]);
                visitedmatrix[top][i] = 0;
                count--;pos++;
            }
        }

        // move down
        for (int i = top; i <= down; i++)
        {   
           
            if (visitedmatrix[i][right] == -1)
            {
                cout << matrix[i][right] << " ";
                                result.insert(result.begin() + pos,matrix[i][right]);

                visitedmatrix[i][right] = 0;
                count--;pos++;
            }
        }

        // move left
        for (int i = right; i >= left; i--)
        {
            if (visitedmatrix[down][i] == -1)
            {
                cout << matrix[down][i] << " ";
                                result.insert(result.begin() + pos,matrix[down][i]);

                visitedmatrix[down][i] = 0;
                count--;pos++;
            }
        }

        // move up
        for (int i = down; i >= top; i--)
        {
            if (visitedmatrix[i][top] == -1)
            {
                cout << matrix[i][top] << " ";
                                result.insert(result.begin() + pos,matrix[i][top] );

                visitedmatrix[i][top] = 0;
                count--;pos++;
            }
        }

        top++;
        right--;
        down--;
        left++;
    }
    cout << "--------------------------"
         << "
";
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            cout << visitedmatrix[i][j] << " ";
        }
        cout << "
";
    }
    return result;
    }
};
Comment

PREVIOUS NEXT
Code Example
Java :: java create arraylist with size 
Java :: java date equals other date 
Java :: vue composition watch 
Java :: java rgb to color 
Java :: android get sdk version 
Java :: cant change button color when app run android studio 
Java :: copy linked list 
Java :: java find duplicate element in list 
Java :: better way to check string on null and empty java 
Java :: Design a class ‘Complex ‘with data members for real and imaginary part. Provide default and Parameterized constructors. Write a program to perform arithmetic operations of two complex numbers. 
Java :: how to make a text field required in android studio 
Java :: array of arrays java 
Java :: how to get orientation lock to portrait android stackoverflow 
Java :: call fragment method from activity 
Java :: declaring variables views in java android 
Java :: startactivity not working in android adapter 
Java :: How to find the maximum occurring character in a given String? using hashmap java 
Java :: java convert double to boolean 
Java :: do function after time android studio 
Java :: app not showing in share menu android 
Java :: javafx list view does not update 
Java :: how to generate random large string in java 
Java :: how to remove letters from string java 
Java :: Implement the static keyword – static variable, static block, static function and static class with following conditions 
Java :: python discord embed generator 
Java :: java string 
Java :: display 2d array 
Java :: create thread java 
Java :: Could not resolve project :app 
Java :: java double 
ADD CONTENT
Topic
Content
Source link
Name
1+3 =