七牛云笔试题-2019-10

1、两数和问题

"""

def solution(arr,length,sum):
dic = {}
for i in range(length):
t = sum-arr[i]
if arr[i] in dic:
return "{} {}".format(dic[arr[i]],i)
else:
dic[t] = i
return -1

length = int(input())
arr = list(map(int,input().split(" ")))
sum = int(input())
print(solution(arr,length,sum))

"""

2、括号匹配问题

"""

def solution(s):
li = []
for i in s:
if i == "(" or i == "(" or i == "[" or i == "{" or i == "<":
li.append(i)
else:
if not li:
return False
pre = li.pop()
if (pre == "(" and i == ")") or (pre == "[" and i == "]") or (pre == "{" and i == "}") or (
pre == "<" and i == ">"):
continue
else:
return False
if not li:
return True

s = input()
print(solution(s))
"""

3、数字出现的第一个位置和最后一个位置

"""

def solution(arr,n,target):
pre = -1
next = -1
for i in range(n):
if arr[i] == target:
if pre == -1:
pre = i
next = i
else:
next = i
return "%d %d" %(pre,next)

n = int(input())
arr = list(map(int,input().split(" ")))
target = int(input())
print(solution(arr,n,target))

"""

原文地址:https://www.cnblogs.com/ChangAn223/p/11694743.html