2016-11-06 53 views
0

我從左下方和行進在順時針方向開始直到沒有字符被保留。這是我的代碼。我有字符的2D矩陣,以及具有麻煩搜索矩陣螺旋

 import java.io.*; 
     import java.util.*; 

public class Solution { 
    static int count = 0; 

public static void main(String[] args) { 
    Scanner sc = new Scanner(System.in); 
    int m = sc.nextInt(); 
    int n = sc.nextInt(); 

    char[][] matrix = new char[n][m]; 
    char[] temp = new char[n*m]; 
    for(int r=0;r<n;r++){ 
     for(int col=0;col<m;col++){ 
      matrix[r][col] = sc.next().charAt(col); 
     } 
    } 
    int k=0, l = 0; 


    while(k < n && l < m){ 
     if(l<m){ 
      for(int i = n-1;i>=k;i--){ 
       temp[count] = matrix[i][l]; 
       count++; 
      } 
      l++; 
     } 

     for(int i = l;i<m;i++){ 
      temp[count] = matrix[k][i]; 
      count++; 
     } 
     k++; 

     for(int i = k;i<n;i++){ 
      temp[count] = matrix[i][m-1]; 
      count++; 
     } 
     m--; 

     if(k < n){ 
      for(int i = m-1;i>=l;i--){ 
       temp[count] = matrix[n-1][i]; 
      } 
      n--; 
     } 
    } 


    String code = String.valueOf(temp); 
    String[] dec = code.split("#"); 
    //System.out.println(dec); 
    int count2 = dec.length; 
    System.out.println(count2); 
    } 
} 

所以任何人都可以指出我要去哪裏錯了嗎?我從左下角開始,爬上去,向右走,然後下去,向左走,繼續,直到沒有元素離開。

回答

0

這裏有兩個問題:不正確的輸入和遺漏計數器遞增。

不正確的輸入:

for(int r=0;r<n;r++){ 
    for(int col=0;col<m;col++){ 
     // The following reads a new line each time a new character is needed 
     matrix[r][col] = sc.next().charAt(col); 
    } 
} 

這可以通過從內環起重sc.next()被固定:

for (int r = 0; r < n; r++) { 
    String line = sc.next(); 
    for (int col = 0; col < m; col++) { 
     matrix[r][col] = line.charAt(col); 
    } 
} 

或(優選)除去完全內環:

for (int r = 0; r < n; r++) { 
    matrix[r] = sc.next().toCharArray(); 
} 

缺少增量(螺旋的較低部分):

for(int i = m-1;i>=l;i--){ 
    temp[count] = matrix[n-1][i]; 
    // Counter increment is missing. Add 
    // counter++; 
} 

一般來說,最好是使用StringBuilder來逐步構建字符串。在你區分它的外觀如下:

StringBuilder temp = new StringBuilder(); 
<...> 
temp.append(matrix[n-1][i]); 
<...> 
String code = temp.toString(); 

在這段代碼中你沒有估算可能的字符串是規模還是手動跟蹤當前插入位置。

+0

下螺旋狀的一部分嗎? –

+0

你可以通過循環頭文件for(int i = m-1; i> = l; i - )'在源代碼中找到它。 ' – kgeorgiy

+0

狗屎錯過了,感謝指出一個出 –

0

開始在矩陣左下方是

matrix[0][m]; 

去上山將通過降低米到哪裏,你已經有了一個字符插入點進行。 喜歡提出我會用4 while循環中循環:

while (usedRow < (int)0.5*n && usedCol < (int)0.5*m) 
    { 
     //usedRow and usedCol start with the value of 0 and will be raised 
     // by the end of the loop 
     int i, j; 
     for (i = m - usedCol; i<=(m-usedCol); i++) 
      { 
       matrix[usedRow][m-i] = sc.next().charAt(0); 
      } 
      for (j = usedRow; j <= (n-usedRow); j++) 
      { 
       matrix[n + j][usedCol] = sc.next.charAt(0); 
      } 
      for (i = usedCol; i <= (m-usedCol); i++) 
      { 
       matrix [n - usedRow][m+i] = sc.next().chatAt(0); 
      } 
      for (j = n - usedRow; j <= (n - usedRow); j++) 
      { 
       matrix[n - j][m - usedCol] = sc.next().charAt(0); 
      } 
      usedRow++; 
      usedCol++; 
    } 

這樣你去順時針和記住這是不使用的行列數內循環。

希望它在某種程度上幫助了你。