class Solution:
"""
@param num: a non-negative integer
@return: one digit
"""
def addDigits(self,num):
while True:
if len(str(num)) == 1:
return num
num = self.getsum(num)
def getsum(self,num):
res= 0for n in str(num):
res += int(n)
return res
class TwoSum:
"""
@param number: An integer
@return: nothing
"""
def __init__(self):
self.list = []
def add(self, number):
# write your code here
self.list.append(number)
return self.list
"""
@param value: An integer
@return: Find if there exists any pair of numbers which sum is equal to the value.
"""
def find(self, value):
# write your code here
for i in range(len(self.list)):
find_num = value - self.list[i]
##如果符合条件的话,要求:前提,第二个数在self.list里面,但是又得区分是不是同一个索引的数字
#1.如果find_num ==value,判断是否存在多个该数值,如果count>1,则返回True
#2.如果find_num != value,直接返回True
#否则返回False,循环完毕,没有找到符合条件的
print(find_num)
if find_num in self.list and (self.list.count(find_num)>1 or find_num != self.list[i] ):
return True
return False
第二种方法:字典的方式,可取,{5:6,1:5}
class TwoSum:
"""
@param number: An integer
@return: nothing
"""
def __init__(self):
self.dic ={}
def add(self, number):
# write your code here
if number not in self.dic:
self.dic[number] = 1else:
self.dic[number] += 1"""
@param value: An integer
@return: Find if there exists any pair of numbers which sum is equal to the value.
"""
def find(self, value):
# write your code here
for column in self.dic:
find_num = value -column
if find_num in self.dic and (self.dic[find_num]>1 or find_num != column):
return True
return False