肖sir__面试笔试题__阿里笔试题

第一题:

#给定一个无序数组nums和一个目标值target,返回数组中两个元素的和为target的算法,时间复杂度为O(1);
def func1(nums,target):
    dict1 = {}
    for i in range(len(nums)):
        num = target - nums[i]
        if num not in dict1:
            dict1[nums[i]] = i
        else:
            return (dict1[num],i)

 

===================================

//给定一个整数数组 nums 和一个整数目标值 target,请在该数组中找 和为目标值 target 的三个整数,并返回它们的数组下标?

方法1:

class Foo(object):
    @staticmethod
    def sum(nums, target):
        result = []
        n = len(nums)
        for i in range(n):
            for j in range(i+1,n):
                for k in range(j+1,n):
                    num = nums[i] + nums[j] +nums[k]
                    if num == target:
                        result.append(i)
                        result.append(j)
                        result.append(k)
                        break
        return result

n = [5,1,2,4]
print(Foo.sum(n,7))

方法2:

可以用列表推导式
nums = [1,2,3,4,5,6,7,8,9]
target=9
list1 = [(x,y,z) for x,x1 in enumerate(nums)  for y,y1 in enumerate(nums) for z,z1 in enumerate(nums) if x1!=y1!=z1 and x1+y1+z1==9]
print(list1)

方法3:

def func1(nums,target):
    i = 0
    while i < len(nums)-2:
        j = i+1
        k = len(nums)-1
        while j < k:
            if nums[i]+nums[j]+nums[k] > target:
                k -= 1
            elif nums[i] + nums[j] + nums[k] < target:
                j += 1
            else:
                return [i,j,k]
        i += 1

nums = [1,2,2,3,5]
a = func1(nums,7)

===================================================

1、一个字典中嵌套另一个字典,值里面里面有无限字典?(递归)

第一种情况:

wx_dict={"aa": {"bb": {"cc":{"dd": {"mm":"f"}}}}}
class analyse_data1:
key_list = []
def get_key(self, data):
if isinstance(data, dict):
for key in data.keys():
if isinstance(data[key], dict):
self.get_key(data[key])
if isinstance(data[key], (tuple, list)):
for i in range(len(data[key])):
if isinstance(data[key][i], dict):
self.get_key(data[key][i])
self.key_list.append(key)
return self.key_list
if __name__ == '__main__':
print(analyse_data1().get_key(wx_dict))

======================================================
第二种情况:
raw_dict= {"a": 1, "b": {"kk": {"nn": 111, "pp": "ppoii"}, "yy": "123aa", "uu": "777aa"}, "c": [{"a": 1, "b": 2}, {"a": 3, "b": 4}, {"a": 5, "b": 6}]}
方法一:
class analyse_data1:
key_list = []
def get_key(self, data):
if isinstance(data, dict):
for key in data.keys():
if isinstance(data[key], dict):
self.get_key(data[key])
if isinstance(data[key], (tuple, list)):
for i in range(len(data[key])):
if isinstance(data[key][i], dict):
self.get_key(data[key][i])
self.key_list.append(key)
return self.key_list
方法二:
class analyse_data2:
key_list = []

def get_key(self, data):
if isinstance(data, dict):
for key in data.keys():
self.get_key(data[key])
self.key_list.append(key)
if isinstance(data, (tuple, list)):
for i in range(len(data)):
self.get_key(data[i])
return self.key_list
方法三:
class analyse_data3:
key_list = []

def get_key(self, dict_a):
"""多维/嵌套字典数据无限遍历,获取json返回结果的所有key值集合"""
if isinstance(dict_a, dict):
for x in range(len(dict_a)):
temp_key = list(dict_a.keys())[x]
temp_value = dict_a[temp_key]
self.key_list.append(temp_key)
self.get_key(temp_value) # 自我调用实现无限遍历
elif isinstance(dict_a, list):
for k in dict_a:
if isinstance(k, dict):
for x in range(len(k)):
temp_key = list(k.keys())[x]
temp_value = k[temp_key]
self.key_list.append(temp_key)
self.get_key(temp_value)
return self.key_list
if __name__ == '__main__':
print(analyse_data1().get_key(raw_dict))
print(analyse_data2().get_key(raw_dict))
print(analyse_data3().get_key(raw_dict))

 

posted @ 2023-02-08 17:09  xiaolehua  阅读(45)  评论(0编辑  收藏  举报