Hackerrank稀疏数组

huangapple go评论64阅读模式
英文:

Hackerrank Sparse Arrays

问题

import java.io.*;
import java.util.*;

class Solution {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int nStrings = sc.nextInt();
        sc.nextLine(); // Consume the newline character after reading nStrings

        String entries[] = new String[nStrings];
        for (int i = 0; i < nStrings; i++)
            entries[i] = sc.nextLine();

        int nQueries = sc.nextInt();
        sc.nextLine(); // Consume the newline character after reading nQueries

        String queries[] = new String[nQueries];
        for (int i = 0; i < nQueries; i++)
            queries[i] = sc.nextLine();

        int result[] = new int[nQueries];
        for (int i = 0; i < nQueries; i++)
            result[i] = 0;

        for (int i = 0; i < nQueries; i++) {
            for (int j = 0; j < nStrings; j++) {
                if (queries[i].equals(entries[j]))
                    result[i]++;
            }
        }
        System.out.println(Arrays.toString(result));
    }
}

问题已修复。主要问题在于没有消耗输入中的换行符。现在代码已经正确处理了输入,输出应该是正确的 [2, 1, 0]。

英文:
import java.io.*;
import java.util.*;

class Solution {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int nStrings = sc.nextInt();
        String entries[] = new String[nStrings];
        for (int i=0;i&lt;nStrings;i++)
            entries[i] = sc.nextLine();

        sc.nextLine();

        int nQueries = sc.nextInt();
        String queries[] = new String[nQueries];
        for (int i=0;i&lt;nQueries;i++)
            queries[i] = sc.nextLine();

        sc.nextLine();

        int result[] = new int[nQueries];
        for (int i=0;i&lt;nQueries;i++)
            result[i] = 0;

        for (int i=0;i&lt;nQueries;i++){
            for (int j=0;j&lt;nStrings;j++){
                if (queries[i].equals(entries[j]))
                    result[i]++;
                else
                    continue;
            }
        }
        System.out.println(Arrays.toString(result));
    }
}

Can someone help me fix this problem the output is not quite correct but code seems to be right. It seems that there is some problem in comparing queries and entries because of which the the correct elements in the particular index are exchanged that is for this input

4
aba
baba
aba
xbxa
3
aba
xbxa
s

output is [1, 2, 0]
but it should've been [2,1,0]

答案1

得分: 0

你在错误的步骤使用了 sc.nextLine(); 指令... 你应该在 sc.nextInt(); 之后使用它。

在你的代码中进行了一些更改后,以下代码将通过所有测试用例:

import java.io.*;
import java.util.*;

class Solution {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int nStrings = sc.nextInt();
        sc.nextLine();
        String entries[] = new String[nStrings];
        for (int i=0;i<nStrings;i++)
            entries[i] = sc.nextLine();

        int nQueries = sc.nextInt();
        sc.nextLine();
        String queries[] = new String[nQueries];
        for (int i=0;i<nQueries;i++)
            queries[i] = sc.nextLine();

        int result[] = new int[nQueries];
        for (int i=0;i<nQueries;i++)
            result[i] = 0;

        for (int i=0;i<nQueries;i++){
            for (int j=0;j<nStrings;j++){
                if (queries[i].equals(entries[j]))
                    result[i]++;
            }
        }

        for(int i=0;i<nQueries;i++)
            System.out.println(result[i]);
    }
}
英文:

You are using sc.nextLine(); instruction at wrong steps... You should use it just after sc.nextInt();.

The code below will pass all the test cases... after some changes in your code.

import java.io.*;
import java.util.*;

class Solution {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int nStrings = sc.nextInt();
        sc.nextLine();
        String entries[] = new String[nStrings];
        for (int i=0;i&lt;nStrings;i++)
            entries[i] = sc.nextLine();


        int nQueries = sc.nextInt();
        sc.nextLine();
        String queries[] = new String[nQueries];
        for (int i=0;i&lt;nQueries;i++)
            queries[i] = sc.nextLine();

        int result[] = new int[nQueries];
        for (int i=0;i&lt;nQueries;i++)
            result[i] = 0;



        for (int i=0;i&lt;nQueries;i++){
            for (int j=0;j&lt;nStrings;j++){
                if (queries[i].equals(entries[j]))
                    result[i]++;
            }
        }
        
        for(int i=0;i&lt;nQueries;i++)
            System.out.println(result[i]);
    }
}

huangapple
  • 本文由 发表于 2020年8月2日 23:39:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/63217922.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定