array
은 다음과 같습니다. {{1,3,5,7},{2,4,6,8,10,12},{2,3,5,7,11,13,17}}
입니다.지그재그 배열에서 행과 열을 제거 하시겠습니까?
Java에서 하나의 행과 하나의 열이 제거 된 것을 제외하고는 정확히 동일한 새 배열을 만드는 방법은 무엇입니까?
이 작업을 짝수 크기의 배열로 수행 할 수 있지만 지그재그 배열로 인해 약간의 문제가 발생합니다. 불특정 컬럼 수를 가진 새로운 배열을 처음으로 만들려고 생각했는데, 거기에서 어디로 가야합니까?
/**
* Creates a new array that is a copy of the input matrix, except that one
* row and one column have been altered.
* Precondition: the row index is between 0 (inclusive) and the number of
* rows of matrix (not inclusive)
* @param matrix the input two dimensional array
* @param row the index of the row to remove
* @param col the index of the column to remove
*/
public static int[][] removeRowAndCol(int[][] matrix, int row, int col) {
int[][] altered = new int[(matrix.length - 1)][];
int x = 0;
for(int i = 0; i < matrix.length; i++){
if(matrix[i].length < col + 1 && i != row){
altered[x] = new int[matrix[i].length];
for(int j = 0; j < altered[x].length; j++){
altered[x][j] = matrix[i][j];
}
if(x < matrix.length - 1){
x++;
}
}
else if(matrix[i].length > col && i != row){
altered[x] = new int[matrix[i].length - 1];
int y = 0;
for(int z = 0; z < matrix[i].length - 1; z++){
if(z != col){
altered[x][y] = matrix[i][z];
y++;
}
else{
z--;
}
}
if(x < matrix.length - 1){
x++;
}
}
}
return altered;
}
}
테스트 케이스 실행 등 : removeRowAndCol (새 INT [] [] {{1, 2}, {3, 4}}, 1, 1)에있어서, 리턴 {{1} } 맞습니다.
int [] [] array = {{1, 2, 3, 4}, {11,12,13,14,15,16}, {21, 22, 23 , 24}, {31, 32, 33}}; removeRowAndCol (array, 0, 0) removeRowAndCol (array, 2, 3) 메서드가 중단되었습니다.
누군가 코드를보고 내가 뭘 잘못했는지 말해 줄 수 있습니까?
가장 작은 서브 어레이보다 보장 행 또는 열의인가? 즉 행 1에 열 5가 없으므로 열 5를 제거하지 마십시오. – Tyler
제거 할 열과 행은 무엇입니까? –
들쭉날쭉하지 않다는 사실 때문에 그렇게 어렵지는 않습니다. 정규 사례에 대한 코드를 포함 할 수 있습니까? –