当前位置:网站首页>Leetcode 1. sum of two numbers
Leetcode 1. sum of two numbers
2022-07-29 06:22:00 【Zhangchuming ZCM】
1. Sum of two numbers
Power button :1. Sum of two numbers

Method 1 : Enumeration ( Brute force )
Time complexity 
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
n = len(nums)
for i in range(n):
for j in range(i + 1, n):
if nums[i] + nums[j] == target:
return [i, j]
return []utilize len() Function to get the length of the array and assign it to n
Then two layers of circulation .
Because the title requires that the same element cannot appear twice , therefore i and j Must be different . So the inner loop j The range is from i+1 Start .
When the loop finds the array nums[i]+nums[j] be equal to target when , Returns the subscript i, and j.
Because there may not be a suitable i and j, And the function must have a return value , So finally add return[]
LeetCode Medium
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:stay pycharm An error will be displayed in ,NameError: name 'List' is not defined

Import typing Bag can
from typing import Listbecause python Too casual , It's easy to forget to pass parameter types . So it introduces typing modular , Variable types can be specified .
- typing Role of module :
- Type checking , Prevent non conformance of parameter and return value types at runtime .
- As a development document , It is convenient for the user to pass in and return parameter types when calling .
- The addition of this module will not affect the operation of the program , Don't report a formal mistake , Only reminders .
pycharm Running test results

Method 2 :
Hash table method
Time complexity 
hash function
function : input data , Generating integers
The main features :1. Different parameters -> Different integers
2. The same parameters -> Same integer
Some functions used
Python enumerate() function
enumerate() Function is used to traverse a data object ( As listing 、 Tuples or strings ) Combined into an index sequence , List both data and data index , Generally used in for Cycle of .
grammar enumerate(sequence, [start=0])

Python dict() function
dict() Function to create a dictionary .
Python dict() function | Novice tutorial
Using hash table algorithm
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
hash_table = dict()
for i, num in enumerate(nums):
if target - num in hash_table:
return [hash_table[target-num], i]
hash_table[nums[i]] = i
return []pycharm test result

边栏推荐
- ArduinoIDE + STM32Link烧录调试
- Encapsulation - Super keyword
- leetcode---技巧
- Based on STM32: couple interactive doll (design scheme + source code +3d drawing +ad circuit)
- Huawei cloud 14 days Hongmeng device development -day1 environment construction
- JUC collection class is unsafe
- synchronized八锁现象理解
- EPS32+Platform+Arduino 跑马灯
- QT learning notes - Import and export of Excel
- 抽象封装继承多态
猜你喜欢
随机推荐
2.4G频段的无线收发芯片 SI24R1 问题汇总解答
JUC集合类不安全
DP1332E 多协议高度集成非接触式读写芯片
SQLyog 安装和配置教程
LeetCode #344.反转字符串
QT learning notes QT model/view
ML10 self study notes SVM
LoRa开启物联网新时代-ASR6500S、ASR6501/6502、ASR6505、ASR6601
关于时间复杂度的个人看法
Redshift还原SP效果 - SP贴图导出设置及贴图导入配置
一些工具,插件,软件链接分享给大家~
【软件工程之美 - 专栏笔记】13 | 白天开会,加班写代码的节奏怎么破?
NoClassDefFoundError 处理
链表--------------------尾插法
计算机网络面试题
SimpleFOC调参2-速度、位置控制
【软件工程之美 - 专栏笔记】26 | 持续交付:如何做到随时发布新版本到生产环境?
【RoboMaster】从零开始控制RM电机(2)-CAN通信原理及电调通信协议
Ml9 self study notes
MySql-面试题










