2017-10-24 18 views
0

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) 메서드가 중단되었습니다.

누군가 코드를보고 내가 뭘 잘못했는지 말해 줄 수 있습니까?

+0

가장 작은 서브 어레이보다 보장 행 또는 열의인가? 즉 행 1에 열 5가 없으므로 열 5를 제거하지 마십시오. – Tyler

+0

제거 할 열과 행은 무엇입니까? –

+0

들쭉날쭉하지 않다는 사실 때문에 그렇게 어렵지는 않습니다. 정규 사례에 대한 코드를 포함 할 수 있습니까? –

답변

0

두 치수가있는 지그재그 형 배열은 지그재그 형 배열이 아닌 다른 배열보다 더 많은 배열입니다. 각 행을 직접 작성해야하므로 각 행마다 크기를 선택할 수 있습니다.

import java.util.Arrays; 

public class Temp { 
    public static void main(String[] args) { 
     int[][] jagged = {{1, 2, 3}, {4, 5, 6, 7, 8}, {9, 10, 11, 12, 13, 14, 15, 16}}; 
     System.out.println("Jagged: " + Arrays.deepToString(jagged)); 
     System.out.println("Smaller 1: " + Arrays.deepToString(removeRowAndCol(jagged, 0, 0))); 
     System.out.println("Smaller 2: " + Arrays.deepToString(removeRowAndCol(jagged, 1, 1))); 
     System.out.println("Smaller 3: " + Arrays.deepToString(removeRowAndCol(jagged, 2, 2))); 
    } 

    private static int[][] removeRowAndCol(int[][] jagged, int i, int j) { 
     int[][] smaller = new int[jagged.length - 1][]; 

     // WARN: outofbounds checks are not implemented! 
     for (int smallerI = 0; smallerI < smaller.length; smallerI++) { 
      int sourcedI = smallerI; 
      if (smallerI >= i) { 
       sourcedI++; 
      } 

      smaller[smallerI] = new int[jagged[sourcedI].length - 1]; 

      for (int smallerJ = 0; smallerJ < smaller[smallerI].length; smallerJ++) { 
       int sourcedJ = smallerJ; 
       if (smallerJ >= j) { 
        sourcedJ++; 
       } 
       smaller[smallerI][smallerJ] = jagged[sourcedI][sourcedJ]; 
      } 
     } 

     return smaller; 
    } 
} 

출력한다 :

Jagged: [[1, 2, 3], [4, 5, 6, 7, 8], [9, 10, 11, 12, 13, 14, 15, 16]] 
Smaller 1: [[5, 6, 7, 8], [10, 11, 12, 13, 14, 15, 16]] 
Smaller 2: [[1, 3], [9, 11, 12, 13, 14, 15, 16]] 
Smaller 3: [[1, 2], [4, 5, 7, 8]] 
+0

내가 이해하지 못하는 것 : 하나의 행만 제거하면 왜 더 작은 배열에 jagged.length - 2의 행 길이가 있어야합니까? - 1일까요? – Isaac

+0

참으로 너무 빠르며 충분히 생각하지 못했습니다. – spi

+0

나는 내 자신의 구현을 시도했고, 나는 가지고있는 코드로 질문을 업데이트했습니다. 하지만 루프에 문제가있는 것 같습니다. 제게 그것을 들여다 볼 수 있습니까? – Isaac