首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何解决HackerRank的Gemstone问题的最终测试用例

如何解决HackerRank的Gemstone问题的最终测试用例
EN

Stack Overflow用户
提问于 2021-09-05 05:46:43
回答 1查看 565关注 0票数 0

我在HackerRank上尝试了一个问题:

  • 实践>算法>字符串> 宝石

基本上是想让我寻找在每个字符串数组元素中重复的字符元素。

描述

有一组岩石,每个岩石都有不同的矿物嵌入其中。每种矿物都是在ascii(a - z)范围内用小写字母指定的。

岩石中可能有多处矿点。一种矿物被称为宝石,如果它发生至少一次在每个岩石在集合中。

给出嵌在每块岩石中的矿物清单,显示收藏中宝石种类的数量。

示例

代码语言:javascript
复制
arr = ['abc','abc','bc']

矿物bc出现在每块岩石中,因此有2宝石。

我的密码

代码语言:javascript
复制
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;

class Result {

    /*
     * Complete the 'gemstones' function below.
     *
     * The function is expected to return an INTEGER.
     * The function accepts STRING_ARRAY arr as parameter.
     */

    public static int gemstones(List<String> arr) {
    // Write your code here
     String str[] = new String[arr.size()];
  
        // ArrayList to Array Conversion
        for (int j = 0; j < arr.size(); j++) {
            str[j] = arr.get(j);
        }
    int c=0;
    int count[]=new int[26];
     for(int i=0;i<str.length;i++){
      String x=str[i];
       for(int j=0;j<26;j++){
           char y=(char)(j+97);
           if(x.indexOf(y)!=-1)
           count[j]++;
       }   
      }
      for(int i=0;i<26;i++){
          if(count[i]==str.length)
           c++;
       }
       
       return c;
        }
}

public class Solution {
    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        int n = Integer.parseInt(bufferedReader.readLine().trim());

        List<String> arr = IntStream.range(0, n).mapToObj(i -> {
            try {
                return bufferedReader.readLine();
            } catch (IOException ex) {
                throw new RuntimeException(ex);
            }
        })
            .collect(toList());

        int result = Result.gemstones(arr);

        bufferedWriter.write(String.valueOf(result));
        bufferedWriter.newLine();

        bufferedReader.close();
        bufferedWriter.close();
    }
}

这是我只能设计public static int gemstones(List<String> arr)的代码。

我的方法很简单

我为这26个字母创建了一个整数数组,并检查了所有数组元素中是否存在任何字母。如果是的话,我就把计数器增加1。

我的问题

但由于某种原因,我未能通过https://hr-testcases-us-east-1.s3.amazonaws.com/2223/input00.txt?AWSAccessKeyId=AKIAR6O7GJNX5DNFO3PV&Expires=1630827098&Signature=947w1lu817SBUBfk8G0vLCRRdVQ%3D&response-content-type=text%2Fplain考试。

在使用自定义输入选项时,答案应该是:0

然而,我无法通过这个。

EN

回答 1

Stack Overflow用户

发布于 2021-09-05 09:20:24

要验证您的算法,我们所能做的就是:

  • 理论检验
  • 解构与单元测试

我们所知道的

  • 力学:根据岩石中当前的位置测试字母表中的每个字母。
  • 决定:你声称自己选择了它,因为你不想写太多的代码。
  • 风格:非常紧凑,没有空格,混合缩进,1行-只注释。

剖析代码

下面,我补充了一些评论,以表达我所理解的意愿:

代码语言:javascript
复制
String str[] = new String[arr.size()];  // array of rocks 
// ArrayList to Array Conversion
for (int j = 0; j < arr.size(); j++) {
    str[j] = arr.get(j);
}

int c=0;  // counter for gemstones

int count[]=new int[26];  // 26 counts (for each of the 26 letters in alphabet)
for(int i=0;i<str.length;i++){  // for each rock, count letters
  String x=str[i];  // rock to test

  for(int j=0;j<26;j++){  // for each letter in alphabet
    char y=(char)(j+97);  // from decimal ASCII code to lower-case char
    if(x.indexOf(y)!=-1)  // if current letter-char found in rock
        count[j]++;  // increment letter-count
  }

}  // test next rock against alphabet (and count)

for(int i=0;i<26;i++){ // test each letter counter if it satisfies (at least once per rock)
  if(count[i]==str.length)
    c++;  // if satisfied (gemstone found) then increment counter
}
       
return c;  // count of gemstones found
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69060855

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档