我不明白为什么只使用我的Hash Tables: Ransom Note HakerRank challenge解决方案传递了8/17个测试用例。
代码:
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int m = in.nextInt();
int n = in.nextInt();
HashMap<String, Integer> magazine = new HashMap<String, Integer>();
for(int magazine_i=0; magazine_i < m; magazine_i++){
String word = in.next();
Integer wordCounter = magazine.get(word);
if(wordCounter != null){
magazine.put(word, ++wordCounter);
}else{
magazine.put(word, 1);
}
}
HashMap<String, Integer> ransom = new HashMap<String, Integer>();
for(int ransom_i=0; ransom_i < n; ransom_i++){
String word = in.next();
Integer wordCounter = ransom.get(word);
if(wordCounter != null){
ransom.put(word, ++wordCounter);
}else{
ransom.put(word, 1);
}
}
boolean ok = true;
breakpoint:
for(Map.Entry<String, Integer> ransomEntry: ransom.entrySet()){
if(magazine.get(ransomEntry.getKey()) != null){
if(magazine.get(ransomEntry.getKey()) < ransomEntry.getValue()){
ok = false;
break breakpoint;
}
}else{
ok = false;
break breakpoint;
}
}
if(ok){
System.out.println("Yes");
}
}
}
答案 0 :(得分:1)
使用原始代码(不一定是编辑版本)的问题(我可以看到)是否定结果的双重输出。如果传感器无法记录,则代码会打印“否”和“是”。您需要做的就是在breakpoint
标签后面添加大括号,然后在“是”打印行后关闭。学习测试代码。