当前位置:网站首页>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

边栏推荐
- LeetCode #283.移动零
- 动态规划总结
- FPGA based: moving target detection (supplementary simulation results, available)
- From entry to soul: how to use tb6600 single chip microcomputer to control stepping motor with high precision (42/57)
- STM32 检测信号频率
- 多线程和并发
- 封装——super关键字
- mavan中的plugin位置
- 关于时间复杂度的个人看法
- LeetCode #19.删除链表的倒数第N个结点
猜你喜欢
随机推荐
【Leetcode刷题】数组2——二分查找
LoRa开启物联网新时代-ASR6500S、ASR6501/6502、ASR6505、ASR6601
leetcode刷题笔记 763.划分字母区间(中等)
Joiner.on和stream().map联合使用技巧
Eight sorts ----------- bubble sort
clickhouse 导入CSV失败 不报错但是无数据
STM32 串口乱码
利用云打码来破解登录遇到验证码的问题
【软件工程之美 - 专栏笔记】“一问一答”第3期 | 18个软件开发常见问题解决策略
给二维表添加时间序列索引
leetcode---技巧
Ml9 self study notes
FPGA based: multi-target motion detection (hand-in-hand teaching ①)
SQLyog 安装和配置教程
低功耗蓝牙5.0芯片nrf52832-QFAA
爬虫Requests库的一些简单用法
抽象类以及接口
EPS32+Platform+Arduino 跑马灯
【软件工程之美 - 专栏笔记】13 | 白天开会,加班写代码的节奏怎么破?
2.4G频段的无线收发芯片 SI24R1 问题汇总解答









