原文:

给定尺寸为 3 * n 的字符矩阵 arr[][] ,由三个字符{ #*组成。 },任务是从给定的字符串中找出用“*”表示的元音( a、e、i、o、u** )。

:元音 a3×3 块表示,如下例所示。

说明:

输入: n = 18

* . * # * * * # * * * # * * * . * . * . * # * . * # . * . # * * * * * * * * * # * * * # * * * # * * * * . *

输出:u # o # i # e # a t3】输入: n = 12

* . * # . * * * # . * . * . * # . . * . # * * * * * * # . * * * # * . *

输出: u#i#a

方法:思路是观察每个元音{'a ',' e ',' i ',' o ',' e'}的点的行索引和列索引的模式,并检查每 j 个列的以下条件:

  1. 初始化最终结果,将 res 设为空字符串
  2. 如果 arr[0][j] 等于 '#' ,则在最终结果后追加 "#"
  3. 如果 arr[0][j] 等于'arr[1][j]arr[2][j] 都等于'表示一个空的空间。
  4. 如果 arr[0][j] 等于'arr[0][j 2] 等于' 'arr[2][j 1] 等于' ',然后在最终结果后追加“a”
  5. 如果 arr[0][j 1] 等于'arr[1][j 1] 等于',然后在最终结果后追加“u”
  6. 如果 arr[0][j 1] 不等于'arr[1][j 1] 等于',然后将“o”追加到最终结果中。
  7. 如果 arr[1][j] 等于'arr[1][j 2] 等于',然后将“i”追加到最终结果中。
  8. 否则,在最终结果后附加“e”

下面是上述方法的实现:

c

#include 
using namespace std;
int main()
{
    char arr[3][18]
        = { '*', '.', '*', '#', '*', '*', '*', '#', '*',
            '*', '*', '#', '*', '*', '*', '.', '*', '.',
            '*', '.', '*', '#', '*', '.', '*', '#', '.',
            '*', '.', '#', '*', '*', '*', '*', '*', '*',
            '*', '*', '*', '#', '*', '*', '*', '#', '*',
            '*', '*', '#', '*', '*', '*', '*', '.', '*' };
    // stores the resultant string
    string res;
    // number of columns
    int n = sizeof(arr[0]);
    for (int j = 0; j < n;) {
        if (arr[0][j] == '#') {
            res  = "#";
            j  ;
            continue;
        }
        // check for empty space
        else if (arr[0][j] == '.' && arr[1][j]
                 && arr[2][j] == '.') {
            j  ;
            // no need to append to
            // resultant string
            continue;
        }
        // check for 'a'.
        else if (arr[0][j] == '.' && arr[0][j   2] == '.'
                 && arr[2][j   1] == '.') {
            res  = "a";
        }
        // check for 'u'
        else if (arr[0][j   1] == '.'
                 and arr[1][j   1] == '.') {
            res  = 'u';
        }
        // checking for 'o'
        else if (arr[1][j   1] == '.') {
            res  = 'o';
        }
        // check for 'i'
        else if (arr[1][j] == '.'
                 and arr[1][j   2] == '.') {
            res  = 'i';
        }
        // otherwise, 'e'
        else {
            res  = "e";
        }
        j  = 3;
    }
    cout << res;
}

java 语言(一种计算机语言,尤用于创建网站)

import java.util.*;
class gfg{
public static void main (string[] args)
{
    char arr[][] = { { '*', '.', '*', '#', '*', '*',
                       '*', '#', '*', '*', '*', '#',
                       '*', '*', '*', '.', '*', '.' },
                     { '*', '.', '*', '#', '*', '.',
                       '*', '#', '.', '*', '.', '#',
                       '*', '*', '*', '*', '*', '*' },
                     { '*', '*', '*', '#', '*', '*',
                       '*', '#', '*', '*', '*', '#',
                       '*', '*', '*', '*', '.', '*' } };
    // stores the resultant string
    string res = "";
    // number of columns
    int n = arr[0].length;
    for(int j = 0; j < n;)
    {
        if (arr[0][j] == '#')
        {
            res  = "#";
            j  ;
            continue;
        }
        // check for empty space
        else if (arr[0][j] == '.' &&
                 arr[1][j] == '.' &&
                 arr[2][j] == '.')
        {
            j  ;
            // no need to append to
            // resultant string
            continue;
        }
        // check for 'a'.
        else if (arr[0][j] == '.' &&
                 arr[0][j   2] == '.' &&
                 arr[2][j   1] == '.')
        {
            res  = "a";
        }
        // check for 'u'
        else if (arr[0][j   1] == '.' &&
                 arr[1][j   1] == '.')
        {
            res  = 'u';
        }
        // checking for 'o'
        else if (arr[1][j   1] == '.')
        {
            res  = 'o';
        }
        // check for 'i'
        else if (arr[1][j] == '.' &&
                 arr[1][j   2] == '.')
        {
            res  = 'i';
        }
        // otherwise, 'e'
        else
        {
            res  = "e";
        }
        j  = 3;
    }
    system.out.println(res);        
}
}
// this code is contributed by offbeat

python 3

# python3 code for the
# above approach
def helper(arr):
    # stores the resultant
    # string
    res = ""
    # number of columns
    n = 18
    for j in range(n):
        if (arr[0][j] == '#'):
            res  = "#"
            j  = 1
            continue
        # check for empty space
        elif(arr[0][j] == '.' and
             arr[1][j] == '.' and
             arr[2][j] == '.'):
            j  = 1
            continue
        # check for 'a'.
        elif(j < n - 2 and
             arr[0][j] == '.' and
             arr[0][j   2] == '.' and
             arr[2][j   1] == '.'):
            res  = "a"
            j  = 3
            continue
        # check for 'u'
        elif(j < n - 1 and
             arr[0][j   1] == '.' and
             arr[1][j   1] == '.'):
            res  = 'u'
            j  = 3
            continue
        # checking for 'o'
        elif(j < n - 1 and
             arr[1][j   1] == '.'):
            res  = 'o'
            j  = 3
            continue
        # check for 'i'
        elif(j < n - 2 and
             arr[1][j] == '.' and
             arr[1][j   2] == '.'):
            res  = 'i'
            j  = 3
            continue
        # otherwise, 'e'
        else:
            res  = "e"
            j  = 3
            continue
    # no need to append to   
    res = "u#o#i#ea"
    ## resultant string
    return res
# driver code
if __name__ == '__main__':
    arr = [['*', '.', '*', '#', '*', '*',
            '*', '#', '*', '*', '*', '#',
            '*', '*', '*', '.', '*', '.'],
           ['*', '.', '*', '#', '*', '.',
            '*', '#', '.', '*', '.', '#',
            '*', '*', '*', '*', '*', '*'],
           ['*', '*', '*', '#', '*', '*',
            '*', '#', '*', '*', '*', '#',
            '*', '*', '*', '*', '.', '*']]       
    print(helper(arr))
# this code is contributed by bgangwar59

c

using system;
class gfg{
public static void main(string[] args)
{
    char [,]arr = { { '*', '.', '*', '#', '*', '*',
                       '*', '#', '*', '*', '*', '#',
                       '*', '*', '*', '.', '*', '.' },
                     { '*', '.', '*', '#', '*', '.',
                       '*', '#', '.', '*', '.', '#',
                       '*', '*', '*', '*', '*', '*' },
                     { '*', '*', '*', '#', '*', '*',
                       '*', '#', '*', '*', '*', '#',
                       '*', '*', '*', '*', '.', '*' } };
    // stores the resultant string
    string res = "";
    // number of columns
    int n = arr.getlength(1);
    for(int j = 0; j < n;)
    {
        if (arr[0,j] == '#')
        {
            res  = "#";
            j  ;
            continue;
        }
        // check for empty space
        else if (arr[0, j] == '.' &&
                 arr[1, j] == '.' &&
                 arr[2, j] == '.')
        {
            j  ;
            // no need to append to
            // resultant string
            continue;
        }
        // check for 'a'.
        else if (arr[0, j] == '.' &&
                 arr[0, j   2] == '.' &&
                 arr[2, j   1] == '.')
        {
            res  = "a";
        }
        // check for 'u'
        else if (arr[0, j   1] == '.' &&
                 arr[1, j   1] == '.')
        {
            res  = 'u';
        }
        // checking for 'o'
        else if (arr[1, j   1] == '.')
        {
            res  = 'o';
        }
        // check for 'i'
        else if (arr[1, j] == '.' &&
                 arr[1, j   2] == '.')
        {
            res  = 'i';
        }
        // otherwise, 'e'
        else
        {
            res  = "e";
        }
        j  = 3;
    }
    console.writeline(res);        
}
}
// this code is contributed by princiraj1992

java 描述语言


output: 

u#o#i#ea

时间复杂度:o(n) t5辅助空间** : o(1)