题目1:括号匹配,思路就是左括号进栈,右括号就出栈且匹配。
# coding=utf-8
str = raw_input("Enter the str:")
a = list(str)
left_lst = []
for i in a:
# 左括号就进stack
if i in ['(','[','{']:
left_lst.append(i)
# 右括号就出stack 且匹配
elif i in [')',']','}']:
# 如果遇到右括号,但是stack没有左括号,出错
if len(left_lst) == 0:
print 'false'
break
l = left_lst.pop()
r = i
if (l == '(' and r ==')') or (l == '[' and r ==']') or (l == '{' and r =='}'):
continue
else:
print 'false'
break
else:
# 如果循环跑完,还有((((
if len(left_lst) > 0:
print 'false'
else:
print 'true'
# coding=utf-8
# 输入
line = raw_input("Enter:")
line = line.replace(' ', '')
lst = line.split(',')
if lst == ['']:
print ''
else:
lst = [(index, int(v)) for index,v in enumerate(lst)]
lst = sorted(lst, key=lambda x: x[1], reverse=True)
ret = ','.join([str(v[0]) for v in lst])
print ret
这道题不知为何,过不了,题目也很简单,不知道为啥不AC。
3、平安果获得最大数量什么的,简单的动态规划
# coding=utf-8
# 输入m,n
m_n_line = raw_input("m,n:")
m,n = int(m_n_line.split(' ')[0]), int(m_n_line.split(' ')[1])
# 输入矩阵
a = []
for i in range(m):
line = raw_input("line:")
a.append([int(v) for v in line.split(' ')])
# 动态规划
sum_list = [[],[],[]]
for i in range(m):
for j in range(n):
if i==0 and j==0:
sum = a[i][j]
sum_list[i].append(sum)
elif i==0:
sum = a[i][j] + sum_list[i][j-1]
sum_list[i].append(sum)
elif j==0:
sum = a[i][j] + sum_list[i-1][j]
sum_list[i].append(sum)
else:
sum_1 = a[i][j] + sum_list[i-1][j]
sum_2 = a[i][j] + sum_list[i][j-1]
if sum_1 > sum_2:
sum_list[i].append(sum_1)
else:
sum_list[i].append(sum_2)
# 找出最大的那个
print sum_list[m-1][n-1]
这道题,已经不AC,尴尬。想当年,x为面试题三道都很快就A过了,只是挂在性格测试,哎,现在去做这种题完全退步了。。
洗洗睡吧。