Python闯LeetCode--第1题:两数之和

Problem: 1. 两数之和

文章目录

思路

看到这道题第一思路就是暴力破解,枚举,两个for循环遍历,直到找到满足要求的答案。主要因题目假设只有一组满足结果的答案,因此难度大大降低,作为第一道题,也能理解。

解题方法

第一层循环从第一个数 i 开始,第二层循环从i之后的第一个数开始,数组中两个数相加如果满足等于目标数target,则返回结果。

复杂度

时间复杂度:

O ( n 2 ) O(n^2) O(n2)

空间复杂度:

O ( 1 ) O(1) O(1)

Code

python 复制代码
class Solution:
    def twoSum(self, nums: List[int], target: int) -> List[int]:
        for i in range(len(nums)):
            for j in range(i+1, len(nums)):
                if nums[i] + nums[j] == target:
                    return [i, j]
相关推荐
ponponon22 分钟前
时代的眼泪,nameko 和 eventlet 停止维护后的项目自救,升级和替代之路
python
Flittly23 分钟前
【从零手写 ClaudeCode:learn-claude-code 项目实战笔记】(5)Skills (技能加载)
python·agent
敏编程36 分钟前
一天一个Python库:pyarrow - 大规模数据处理的利器
python
Flittly2 小时前
【从零手写 ClaudeCode:learn-claude-code 项目实战笔记】(4)Subagents (子智能体)
python·agent
xlp666hub3 小时前
Leetcode 第三题:用C++解决最长连续序列
c++·leetcode
xlp666hub7 小时前
Leetcode第二题:用 C++ 解决字母异位词分组
c++·leetcode
明月_清风9 小时前
Python 装饰器前传:如果不懂“闭包”,你只是在复刻代码
后端·python
明月_清风9 小时前
打破“死亡环联”:深挖 Python 分代回收与垃圾回收(GC)机制
后端·python
xlp666hub1 天前
Leetcode第一题:用C++解决两数之和问题
c++·leetcode
ZhengEnCi1 天前
08c. 检索算法与策略-混合检索
后端·python·算法