[英]Javascript Mini-Max Sum HackerRank problem returns wrong sum?
[英]Dictionaries and Maps hackerrank problem (javaScript)
我试图在 HackerRank 中解决这个问题,我只通过了第一个案例,其他所有案例都失败了。 所以拜托,我想帮忙。
目标今天,我们正在学习使用 Map 或字典数据结构的键值对映射。 查看教程选项卡以获取学习材料和教学视频!
任务给定姓名和电话号码,组装一个电话簿,将朋友的姓名映射到他们各自的电话号码。 然后,您将获得未知数量的姓名来查询您的电话簿。 对于每个查询,将电话簿中的相关条目以 name=phoneNumber 形式打印在新行上; 如果找不到条目,则打印 Not found 代替。
注意:您的电话簿应该是 Dictionary/Map/HashMap 数据结构。
输入格式第一行包含一个 integer, ,表示电话簿中的条目数。 随后的每一行都以单行上的空格分隔值的形式描述了一个条目。 第一个值是朋友的名字,第二个值是一个数字电话号码。 在电话簿条目的行之后,有未知数量的查询行。 每行(查询)包含一个要查找的内容,您必须继续阅读行,直到没有更多输入。
注意:名称由小写英文字母组成,仅是名字。
约束
1≤n≤10 ^5
1 ≤查询≤ 10^5
Output 格式每次查询换行,如果电话簿中没有对应的姓名,则打印Not found
; 否则,以name=phoneNumber
格式打印全名和phoneNumber 。
样本输入
3
sam 99912222
tom 11122222
harry 12299933
sam
edward
harry
样品 Output
sam=99912222
Not found
harry=12299933
我的代码
function processData(input) {
const number = parseInt(input[0])
if(number >= 1 && number <= 100000) {
const allData = input.split('\n')
const contactData = allData.splice(0, number + 1)
queries = [...allData]
const phoneBook = new Map()
for(let i = 0; i < contactData.length -1 ; i++) {
splitContact = contactData[i + 1].split(' ')
phoneBook.set(splitContact[0], splitContact[1])
}
if(1 <= queries.length && queries.length <= 100000) {
queries.filter((key) => {
if(phoneBook.has(key)) {
console.log(key + '=' +phoneBook.get(key))
} else {
console.log('Not found')
}
})
}
}
}
问题链接https://www.hackerrank.com/challenges/30-dictionaries-and-maps/problem?isFullScreen=true
const input = `3 sam 99912222 tom 11122222 harry 12299933 sam edward harry`; function processData(input) { // Split the input into lines let allLines = input.split('\n'); // Remove the first element from lines as the number of contacts const n = parseInt(allLines.shift()); // Remove n number of lines from the lines as contacts const contactData = allLines.splice(0, n); // Transform contact lines into a phone book const phoneBook = new Map(contactData.map((contact) => contact.split(' '))); // The only thing left in lines is queries, iterate over each of them // and print the result allLines.forEach(query => { const queryResult = phoneBook.get(query)? `${query}=${phoneBook.get(query)}`: 'Not found'; console.log(queryResult); }); } processData(input);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.