当前位置:网站首页>[LeetCode] 两数之和【1】
[LeetCode] 两数之和【1】
2022-07-01 00:37:00 【山茶花开时。】
问题: 给定一个整数数组nums和一个整数目标值target,请你在该数组中找出和为目标值target的那两个整数,并返回它们的数组下标(注意: 数组中同一个元素在答案里不能重复出现)
示例1
输入: nums = [2,7,11,15], target = 9
输出: [0,1]
解释: 因为nums[0] + nums[1] == 9,返回[0, 1]
示例2
输入: nums = [3,2,4], target = 6
输出: [1,2]
示例3
输入: nums = [3,3], target = 6
输出: [0,1]
python3解题
def twoSum(nums, target):
for i in range(len(nums)):
for j in range(i+1,len(nums)):
if nums[i] + nums[j] == target:
return [i, j]
twoSum([2,7,11,15],9) # [0, 1]
twoSum([3,2,4],6) # [1, 2]
twoSum([3,3],6) # [0, 1]
边栏推荐
- CMU15445 (Fall 2019) 之 Project#1 - Buffer Pool 详解
- Exercise and health
- Multi graph explanation of resource preemption in yarn capacity scheduling
- Tibetan poem PTA
- [DaVinci developer topic] -37- detail IRV: introduction to inter runnable variable + configuration
- Usage of C set
- C#生成putty格式的ppk文件(支持passphrase)
- 实验八 T-sql,存储过程
- The girlfriend said: if you want to understand the three MySQL logs, I will let you heiheihei!
- Metauniverse and virtual reality (II)
猜你喜欢
2022-2028 global PTFE lined valve industry research and trend analysis report
C#生成putty格式的ppk文件(支持passphrase)
NE555波形发生器手把手教程之NE555内部结构(一)
Vnctf 2022 cm CM1 re reproduction
Chapter 53 overall understanding of procedures from the perspective of business logic implementation
2022-2028 global retro glass industry research and trend analysis report
HDU 2488 A Knight's Journey(DFS)
2022-2028 global elevator emergency communication system industry research and trend analysis report
NE555 waveform generator handle tutorial NE555 internal structure (I)
MySQL variables, stored procedures and functions
随机推荐
From January 11, 2007 to January 11, 2022, I have been in SAP Chengdu Research Institute for 15 years
Hoo research | coinwave production - nym: building the next generation privacy infrastructure
Experiment 8 T-SQL, stored procedure
第53章 从业务逻辑实现角度整体性理解程序
Search rotation sort array
2022-2028 global encrypted external hard disk industry research and trend analysis report
CMU15445 (Fall 2019) 之 Project#1 - Buffer Pool 详解
What is product thinking
对libco的一点看法
Cmu15445 (fall 2019) project 1 - buffer pool details
2022-2028 global public address fire alarm system industry research and trend analysis report
Self examination before school starts
20220215 CTF misc buuctf Xiaoming's safe binwalk analysis DD command separate rar file archpr brute force password cracking
ArrayList analysis 1-cycle, capacity expansion, version
2022-2028 global ethylene oxide scrubber industry research and trend analysis report
IBL预计算的疑问终于解开了
C # generates PPK files in putty format (supports passphrase)
2022-2028 global retro glass industry research and trend analysis report
Longest valid bracket
魔王冷饭||#101 魔王解惑数量多与质量;员工管理;高考志愿填报;游戏架构设计