Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created May 1, 2021 18:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save parzibyte/fabe67cd6caf85d14943a69e8ec2ca1f to your computer and use it in GitHub Desktop.
Save parzibyte/fabe67cd6caf85d14943a69e8ec2ca1f to your computer and use it in GitHub Desktop.
static int sumarDiagonal(int[][] matriz, String diagonal) {
int suma = 0;
// Si es la principal
if (diagonal.equals("p")) {
int x = 0, y = 0;
while (x < matriz.length && y < matriz[0].length) {
suma += matriz[y][x];
x++;
y++;
}
} else {
int x = 0, y = matriz.length - 1;
while (x < matriz.length && y >= 0) {
suma += matriz[y][x];
x++;
y--;
}
}
return suma;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment