#coding=utf8
"""
来自leetcode 求两数之和
给定一个整数数组nums和一个目标值target,请你在该数组中找出和为目标值的那两个整数,
并返回他们的数组下标
你可以假设每种输入只会对应一个答案。但是,你不能重复利用这个数组中的同样的元素
程序写法粗糙,算法优先
"""
list = [23,12,34,56,13,57,24]
# 先用target减去某一元素值,得到结果在遍历匹配元素是否相同
def mechod1(number):
ok,a,b = False,0,0
index = range(len(list))
for i in index:
num = number - list[i]
print('num:',num)
for no in index[i+1:len(index)]:
if num == list[no]:
a = i
b = no
ok = True
return (ok,a,b)
return (ok,a,b)
# 先过滤比结果大的值,在用target减去某一元素值,得到结果在遍历匹配元素是否相同
def mechod2(number):
ok,a,b = False,0,0
temperary = []
for num in list:
if num <= number:
temperary.append((num,list.index(num)))
index = range(len(temperary))
for i in index:
num = number - temperary[i][0]
print('num:',num)
for no in index[i+1:len(index)]:
if num == temperary[no][0]:
a = temperary[i][1]
b = temperary[no][1]
ok = True
return (ok,a,b)
return (ok,a,b)
if __name__ == '__main__':
print(mechod2(25))
#coding=utf8
"""
给出两个非空的链表用来表示两个非负的整数。其中,他们各自的位数是按照逆序的方式
存储的。并且他们的每个节点只能存储一位数字。
如果,我们将这两个数相加起来,则返回一个新的链表来表示它们的和。
可以假设除了数字0之外,这个两个数都不会以0开头
输入:(2 -> 4 -> 3)+ (5 -> 6 -> 4)
输出:7 -> 0 -> 8
原因:342 + 465 = 807
"""
list1 = [3,5,2]
list2 = [9,7,8]
def method():
a = ''.join('%s'%index for index in list1)[::-1]
b = ''.join('%s'%index for index in list2)[::-1]
c = str(int(a) + int(b))
print('%s = %s + %s' % (c,a,b))
c = c[::-1]
return list(c)
if __name__ == '__main__':
print(method())