Hackerrank:哈希表 - 赎金记录
Hackerrank: Hash tables- Ransom note
试图通过 Javascript 解决黑客排名上的这个问题:
https://www.hackerrank.com/challenges/ctci-ransom-note
希望得到一些帮助,因为我没有通过所有测试用例,我的代码是:
function main() {
var m_temp = readLine().split(' ');
var m = parseInt(m_temp[0]);
var n = parseInt(m_temp[1]);
magazine = readLine().split(' ');
ransom = readLine().split(' ');
var hashTable = {};
var counter = 0;
for (var i = 0; i < ransom.length; i++) {
hashTable[i] = ransom[i];
}
for (keys in hashTable) {
if (magazine.hasOwnProperty(keys)) {
counter +=1;
} else {
counter -=1;
}
}
console.log(counter >= n ? "Yes" : "No");
}
非常感谢!!
这是我完成的一个版本,它也在检查以确保你不会多次使用这个词,它不在规范中,但我也没有意义能够使用单词不止一次,就像从纸上剪下单词一样。
还要对参数进行一次小的健全性检查,以确保计数正确。
var lines = [
"6 4",
"give me one grand today night",
"give one grand today"
];
function readLine() {
return lines.shift();
}
function main() {
var m_temp = readLine().split(' ');
var m = parseInt(m_temp[0]);
var n = parseInt(m_temp[1]);
var i;
magazine = readLine().split(' ');
ransom = readLine().split(' ');
//sanity check..
if (magazine.length !== m)
throw new Error('Magazine count wrong');
if (ransom.length !== n)
throw new Error('Ransom count wrong');
//build a hash of all the words in magazine
var hashTable = {};
for(i = 0; i < magazine.length; i++){
var word = magazine[i];
if (!hashTable[word]) hashTable[word] = 1;
else hashTable[word] ++;
//keep a count, as a word surely can only be used once.
}
//now loop ransom and see if all are in magazine.
var counter = 0;
for(i = 0; i < ransom.length; i++) {
if (hashTable[ransom[i]]){
counter += 1;
hashTable[ransom[i]] --; //word has now been used.
}
}
console.log(counter >= n? "Yes":"No");
}
main();
您的代码存在以下问题:
你没有逻辑去统计一个词在magazine
中出现的次数,这是一个巨大的缺陷。
考虑 magazine
: give this
和 ransom
: give give
的示例。您的代码将 return Yes
而答案是 No
因为 magazine
.
中只有一个 give
我认为在这里使用 hasOwnProperty
不合适。您只想知道 magazine
中是否存在某个单词。您可以使用 magazine.includes(hashTable[keys])
而不是 magazine.hasOwnProperty(keys)
最后,你应该hash
杂志的词而不是ransom的词,并记录每个杂志词的出现次数。
尝试纠正这些问题,您的代码肯定会 运行。您可以阅读@Keith 的代码。
希望对您有所帮助!!!
try to use below code to check for correctness
static void checkMagazine(string[] magazine, string[] note)
{
Boolean isCorrect = true;
for (int i = 0; i < note.Length; i++)
{
if (magazine.Contains(note[i]))
{
if (magazine.Count(f => f == note[i]) != note.Count(f => f == note[i]))
{
isCorrect = false;
break;
}
}
else
{
isCorrect = false;
break;
}
}
if (isCorrect)
{
Console.WriteLine("Yes");
}
else
{
Console.WriteLine("No");
}
}
试图通过 Javascript 解决黑客排名上的这个问题: https://www.hackerrank.com/challenges/ctci-ransom-note
希望得到一些帮助,因为我没有通过所有测试用例,我的代码是:
function main() {
var m_temp = readLine().split(' ');
var m = parseInt(m_temp[0]);
var n = parseInt(m_temp[1]);
magazine = readLine().split(' ');
ransom = readLine().split(' ');
var hashTable = {};
var counter = 0;
for (var i = 0; i < ransom.length; i++) {
hashTable[i] = ransom[i];
}
for (keys in hashTable) {
if (magazine.hasOwnProperty(keys)) {
counter +=1;
} else {
counter -=1;
}
}
console.log(counter >= n ? "Yes" : "No");
}
非常感谢!!
这是我完成的一个版本,它也在检查以确保你不会多次使用这个词,它不在规范中,但我也没有意义能够使用单词不止一次,就像从纸上剪下单词一样。
还要对参数进行一次小的健全性检查,以确保计数正确。
var lines = [
"6 4",
"give me one grand today night",
"give one grand today"
];
function readLine() {
return lines.shift();
}
function main() {
var m_temp = readLine().split(' ');
var m = parseInt(m_temp[0]);
var n = parseInt(m_temp[1]);
var i;
magazine = readLine().split(' ');
ransom = readLine().split(' ');
//sanity check..
if (magazine.length !== m)
throw new Error('Magazine count wrong');
if (ransom.length !== n)
throw new Error('Ransom count wrong');
//build a hash of all the words in magazine
var hashTable = {};
for(i = 0; i < magazine.length; i++){
var word = magazine[i];
if (!hashTable[word]) hashTable[word] = 1;
else hashTable[word] ++;
//keep a count, as a word surely can only be used once.
}
//now loop ransom and see if all are in magazine.
var counter = 0;
for(i = 0; i < ransom.length; i++) {
if (hashTable[ransom[i]]){
counter += 1;
hashTable[ransom[i]] --; //word has now been used.
}
}
console.log(counter >= n? "Yes":"No");
}
main();
您的代码存在以下问题:
你没有逻辑去统计一个词在
magazine
中出现的次数,这是一个巨大的缺陷。考虑
magazine
:give this
和ransom
:give give
的示例。您的代码将 returnYes
而答案是No
因为magazine
. 中只有一个 我认为在这里使用
hasOwnProperty
不合适。您只想知道magazine
中是否存在某个单词。您可以使用magazine.includes(hashTable[keys])
而不是magazine.hasOwnProperty(keys)
最后,你应该
hash
杂志的词而不是ransom的词,并记录每个杂志词的出现次数。
give
尝试纠正这些问题,您的代码肯定会 运行。您可以阅读@Keith 的代码。
希望对您有所帮助!!!
try to use below code to check for correctness
static void checkMagazine(string[] magazine, string[] note)
{
Boolean isCorrect = true;
for (int i = 0; i < note.Length; i++)
{
if (magazine.Contains(note[i]))
{
if (magazine.Count(f => f == note[i]) != note.Count(f => f == note[i]))
{
isCorrect = false;
break;
}
}
else
{
isCorrect = false;
break;
}
}
if (isCorrect)
{
Console.WriteLine("Yes");
}
else
{
Console.WriteLine("No");
}
}