美文网首页
HackerRank:Hash Tables: Ice Crea

HackerRank:Hash Tables: Ice Crea

作者: 流浪山人 | 来源:发表于2019-12-12 18:24 被阅读0次

题目

https://www.hackerrank.com/challenges/ctci-ice-cream-parlor/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=search

题目解析

使用字典,找到相关的值即可,但是要注意要把自己排除掉,可能出现自己加自己相等的情况

ANSWER

#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the whatFlavors function below.
def whatFlavors(cost, money):
    #cost_m={v:k for k,v in enumerate(cost)}
    cost_m={}
    for k,v in enumerate(cost):
        if money-v in cost_m.keys():
            print(cost_m[money-v]+1,k+1)
        cost_m[v]=k


if __name__ == '__main__':
    t = int(input())

    for t_itr in range(t):
        money = int(input())

        n = int(input())

        cost = list(map(int, input().rstrip().split()))

        whatFlavors(cost, money)

相关文章

网友评论

      本文标题:HackerRank:Hash Tables: Ice Crea

      本文链接:https://www.haomeiwen.com/subject/tywtnctx.html