牛客网在线编程专题《剑指offer-面试题42》和为S的两个数字

我的个人微信公众号:Microstrong

微信公众号ID:MicrostrongAI

微信公众号介绍:Microstrong(小强)同学主要研究机器学习、深度学习、计算机视觉、智能对话系统相关内容,分享在学习过程中的读书笔记!期待您的关注,欢迎一起学习交流进步!

知乎主页:https://www.zhihu.com/people/MicrostrongAI/activities

Github:https://github.com/Microstrong0305

个人博客:https://blog.csdn.net/program_developer

 题目链接:

https://www.nowcoder.com/practice/390da4f7a00f44bea7c2f3d19491311b?tpId=13&tqId=11195&tPage=3&rp=3&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking

题目描述:

解题思路:

(1)数组中的数两两相加,找出乘积最小的解

时间复杂度为O(n^2)

已经AC的代码:

# -*- coding:utf-8 -*-

class Solution:

    def FindNumbersWithSum(self, array, tsum):
        result_list = []
        for index, value in enumerate(array):
            for j in range(index, len(array)):
                if value + array[j] == tsum:
                    result_list.append(value)
                    result_list.append(array[j])
                    break
            if len(result_list) == 2:
                break
        return result_list

    def FindNumbersWithSum2(self, array, tsum):


if __name__ == "__main__":
    sol = Solution()
    array = [1, 3, 5, 15, 17]
    tsum = 20
    print(sol.FindNumbersWithSum(array, tsum))

Reference:

【1】《剑指offer》,何海涛著。

【2】https://blog.nowcoder.net/n/0935efd26ade435497dcbe407cfc94ec

发布了289 篇原创文章 · 获赞 1000 · 访问量 120万+

猜你喜欢

转载自blog.csdn.net/program_developer/article/details/104912091