Skip to content

Instantly share code, notes, and snippets.

@kostoww
Created October 20, 2020 21:08
Show Gist options
  • Save kostoww/5f5c4f22f3f771dad84b5034e88195b0 to your computer and use it in GitHub Desktop.
Save kostoww/5f5c4f22f3f771dad84b5034e88195b0 to your computer and use it in GitHub Desktop.
package com.trendminer.identity.group.service;
public class Main {
private static final String COMMA = ",";
public static void main(String[] args) {
int[][] matrix = {
{0, 1, 2, 3},
{11, 12, 13, 4},
{10, 15, 14, 5},
{9, 8, 7, 6}
};
int i = 0;
int rowEnd = matrix.length;
int colEnd = matrix[0].length;
int rowStart = 0;
int colStart = 0;
while (rowStart < rowEnd && colStart < colEnd) {
for (i = colStart; i < colEnd; ++i)
System.out.print(matrix[rowStart][i] + COMMA);
rowStart++;
for (i = rowStart; i < rowEnd; ++i)
System.out.print(matrix[i][colEnd - 1] + COMMA);
colEnd--;
if (rowStart < rowEnd) {
for (i = colEnd - 1; i >= colStart; --i)
System.out.print(matrix[rowEnd - 1][i] + COMMA);
rowEnd--;
}
if (colStart < colEnd) {
for (i = rowEnd - 1; i >= rowStart; --i)
System.out.print(matrix[i][colStart] + COMMA);
colStart++;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment