【问题标题】:In a matrix of characters, find a particular String在一个字符矩阵中,找到一个特定的字符串
【发布时间】:2015-08-04 08:50:19
【问题描述】:

我有一个字符矩阵,假设是 4*5 矩阵

A-C-P-F

X-S–O-P

V-U-Q-N

W-G-N-M

D-A-T-I

我想查找该矩阵中是否存在单词 FOUND

我的矩阵如下(JAVA代码)

char matrixOfChars[][]={
{'A','C','P','F'},
{'X','S','O','P'},
{'V','U','Q','N'},
{'W','G','N','M'},
{'D','A','T','I'}};

注意:在矩阵中,第一个词总是在第一行,第二个词总是在第二行,依此类推

目前为单个字符串完成的代码如下

private static void generateCombinations(String original, String combination) {
        if (original.length() == 0) {
            System.out.println(combination);
        } else {
            for(int i=0;i<original.length();i++){
                generateCombinations(original.substring(0,i) + original.substring(i+1,original.length()),combination + original.charAt(i));
            }
        }
    }

请帮我在 JAVA 中找到上述解决方案。如果使用java递归给出解决方案会更好。

谢谢。

【问题讨论】:

  • 1k+ 代表并且仍然发布问题而没有表现出任何努力......令人失望!
  • @OomphFortuity 检查答案,使用递归。

标签: java arrays matrix


【解决方案1】:

使用循环遍历 FOUND 的字符,在矩阵的每一行中使用 Arrays.asListindexOf 来获取位置并创建一个新数组或您需要的内容

String word = "FOUND";
int[] sollution = new int[work.length()];

for (int i = 0; i < word.length(); i++) {
    int index = Arrays.asList(matrixOfChars[i]).indexOf(word.charAt(i));
    System.out.print(index + "\n");       
    sollution[i] = index;
}

Arrays.toString(sollution);

你会得到这个输出:

 3
 2
 1
 2
 0

还有sollution = {3 , 2 , 1 , 2 , 0 }

【讨论】:

    【解决方案2】:

    试试这个代码

    public class Test {
    
    
    public static void main(String[] args) {
        char matrixOfChars[][]={
                {'A','C','P','F'},
                {'X','S','O','P'},
                {'V','U','Q','N'},
                {'W','G','N','M'},
                {'D','A','T','I'}};
    
        System.out.println(find(matrixOfChars,5,4, "FOUND"));
    }
    
    public static boolean find(char matrixOfChars[][],int rowsSize,int columnSize,String word)
    {
        if(word.length()!=rowsSize)
            return false;
        String temp="";
        for (int i = 0; i < rowsSize; i++) {
            for (int j = 0; j < columnSize; j++) {
                if(word.charAt(i)==matrixOfChars[i][j])
                {
                    temp+=word.charAt(i);
                    break;
                }
            }
        }
        System.out.println(temp);
        if(temp.equals(word))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    
    }
    

    递归解:

    public static void main(String[] args) {
        char matrixOfChars[][] = { { 'A', 'C', 'P', 'F' },
                { 'X', 'S', 'O', 'P' }, { 'V', 'U', 'Q', 'N' },
                { 'W', 'G', 'N', 'M' }, { 'D', 'A', 'T', 'I' } };
        String word="AGUNC";
        String res=find2(matrixOfChars, 5, 4, word);
        System.out.println(res);
        System.out.println(res.equals(word));
    
    }
    
    static String temp = "";
    static int n = 0;
    
    public static String find2(char matrixOfChars[][], int rowsSize,
            int columnSize, String word) {
    
        if (word.length() != rowsSize || n == rowsSize)
            return temp;
        else {
    
            for (int j = 0; j < columnSize; j++) {
                if (word.charAt(n) == matrixOfChars[n][j]) {
                    temp += word.charAt(n);
                    n++;
                    return find2(matrixOfChars, rowsSize, columnSize, word);
                }
    
            }
            n++;
            return "";
        }
    }
    

    【讨论】:

      【解决方案3】:

      使用递归:

      class Test {
      
          static char matrixOfChars[][]={
                  {'A','C','P','F'},
                  {'X','S','O','P'},
                  {'V','U','Q','N'},
                  {'W','G','N','M'},
                  {'D','A','T','I'}};
      
          static int n = 0;
      
          public static void main(String args[]) {
              n = matrixOfChars.length - 1;
              find();
          }
      
          public static void find() {
              String word = "";
      
              if(n >= 0) {
      
                  for(int i = 0 ; i < matrixOfChars[n].length ; i ++) {
                      word += matrixOfChars[n][i];
                  }
      
                  if(word.equals("FOUND")) {
                      System.out.println("FOUND is found");
      
                  } else {
      
                      n--;
                      find();
                  }
      
              } else {
                  System.out.println("FOUND is not found");
              }
      
          }
      }
      

      【讨论】:

      • 代码不工作,请检查。它给出了未找到的结果
      • @OomphFortuity,找不到结果,因为该数组中现在有 {'F', 'O', 'U', 'N', 'D'}。你还有别的意思吗?
      【解决方案4】:

      试试下面的代码:

          char matrixOfChars[][]={
          {'A','C','P','F'},
          {'X','S','O','P'},
          {'V','U','Q','N'},
          {'W','G','N','M'},
          {'D','A','T','I'}};
      
          String word = "FOUND";
          int[] sol = new int[word.length()];
      
          for (int i = 0; i < word.length(); i++) {
      
              char y = word.charAt( i );
              int z = new String( matrixOfChars[i] ).indexOf( y );
      
              System.out.print( z + "\n" );
              sol[i] = z;
          }
      
          System.out.println( Arrays.toString( sol ) );
      

      【讨论】:

        猜你喜欢
        • 2017-09-29
        • 1970-01-01
        • 2013-05-07
        • 2013-12-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多