2016-11-06 59 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++; 
    } 

这样你去顺时针和记住这是不使用的行列数内循环。

希望它在某种程度上帮助了你。