Skip to content

Instantly share code, notes, and snippets.

@Daniiarz
Last active May 25, 2020 06:44
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 Daniiarz/d9b8c63a056e9c4189b4b3286ed6c444 to your computer and use it in GitHub Desktop.
Save Daniiarz/d9b8c63a056e9c4189b4b3286ed6c444 to your computer and use it in GitHub Desktop.
import java.util.ArrayList;
import java.util.List;
public class Flattener {
public List flatten(List list) {
List result = new ArrayList();
for (Object o : list) {
if (o == null) {
continue;
} else if (o instanceof List) {
result.addAll(flatten((List) o));
} else {
result.add(o);
}
}
return result;
}
}
import java.util.Arrays;
class Matrix {
private int[][] a;
Matrix(String matrixAsString) {
String[] rows = matrixAsString.split("\\n+");
int ncols = rows[0].split("\\s+").length;
a = new int[rows.length][ncols];
for (int i = 0; i < rows.length; i++) {
String[] row = rows[i].split("\\s+");
for (int j = 0; j < row.length; j++)
a[i][j] = Integer.parseInt(row[j]);
}
}
int[] getRow(int rowNumber) {
return a[rowNumber-1];
}
int[] getColumn(int columnNumber) {
int[] col = new int[a.length];
for (int i = 0; i < a.length; i++)
col[i] = a[i][columnNumber-1];
return col;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment