【问题标题】:Print occurrence of characters in an array to 2D array将数组中出现的字符打印到二维数组
【发布时间】:2019-10-25 05:55:41
【问题描述】:

我在 char 数组中有一些字符,从 a-z 开始。

  1. 我们如何验证输入数组中的字符是否介于 a-z 之间?
  2. 打印二维数组中每个字符的出现次数,并打印输入数组中不存在的 a-z 之间的所有字符,出现次数为 0。(Col1 - 字符,Col2 - 出现次数)

例如:

Input - {'c','b','e','x','h'};

Output -

Col1 Col2

a    0

b    1

c    1

d    0

依此类推,直到 z...

我已尝试解决问题的第一部分,如下所示:

public static boolean charValidation(char[] arr) {

       char[] charArr=new char[122];

        for(char c : arr)
        {
            int ascii = (int)c;
            charArr[ascii]=c;
        }

        for(int i=0;i<charArr.length;i++) {
            if(i<97 || i>122) {
                if((int)charArr[i]!=0) {
                    return false;
                }
            }
        }
        return true;        
    }

这似乎是一个 hacky 解决方案。第一和第二的任何优化解决方案?

【问题讨论】:

    标签: java arrays multidimensional-array


    【解决方案1】:

    这是一个简单的实现,它使用字母表中的所有小写字母填充哈希图,并将它们的计数初始化为零。

    public static boolean charValidation(char[] input) {
        boolean valid = true;
        String letters = "abcdefghijklmnopqrstuvwxyz";
        Map<Character, Integer> counts = new HashMap<>();
        for (char letter : letters.toCharArray()) {
            counts.put(letter, 0);
        }
        for (char letter : input) {
            if (letter < 'a' || letter > 'z') {
                valid = false;
            }
            else {
                int count = counts.get(letter);
                counts.put(letter, count+1);
            }
        }
    
        counts.entrySet().forEach(entry->{
            System.out.println(entry.getKey() + " " + entry.getValue());  
        });
    
        return valid;
    }
    
    public static void main(String args[]) {
        char[] input = {'c','b','e','x','h'};
        System.out.println("All inputs were valid: " + charValidation(input));
    }
    

    打印出来:

    a 0
    b 1
    c 1
    d 0
    e 1
    f 0
    g 0
    h 1
    i 0
    j 0
    k 0
    l 0
    m 0
    n 0
    o 0
    p 0
    q 0
    r 0
    s 0
    t 0
    u 0
    v 0
    w 0
    x 1
    y 0
    z 0
    All inputs were valid: true
    

    【讨论】:

      【解决方案2】:

      您可以只使用一个 for 循环来完成第一部分。

      for (int i = 0; i < arr.length; i++)
      {
          int ascii = (int) arr[i];
      
          // if anyone of the ascii codes is outside this range...
          if (ascii < 97 || ascii > 122)
          { 
              // ... then the array has something other than a-z
              // ... so the array is not valid
               return false;
          }
      }
      
      // if we made it through the entire for loop,
      // this means we never came across an invalid char,
      // so return true 
      
      return true;
      

      【讨论】:

        【解决方案3】:

        您可以在您的第一个循环本身中执行所需的逻辑,如下所示:

                for(char c : arr)
                {
                   int ascii = (int)c;
                   if(ascii < 97 || ascii > 122)
                   {
                       return false; 
                   }
                   return true;
                }
        

        【讨论】:

          【解决方案4】:

          对于您的第一个问题,您的阵列有多大?如果大小不是问题,我会使用正则表达式来实现它。

          替换不在 a-z 中的任何内容,如果剩余字符串长度为零,则表示所有字符都来自 a-z。

          private static boolean validateChar(char[] arr) {
              if (arr == null || arr.length == 0) {
                  return false;
              }
              return new String(arr).replaceAll("[!a-z]", "").length() == 0;
          }
          

          对于您的第二个问题,我认为 HashMap 会比二维数组做得更好,除非您有真正需要二维数组的特定要求。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2018-09-29
            • 1970-01-01
            • 2015-02-03
            • 2017-12-29
            • 2014-12-21
            • 2015-12-13
            相关资源
            最近更新 更多