首页
题库
面试
求职
学习
竞赛
More+
所有博客
搜索面经/职位/试题/公司
搜索
我要招人
去企业版
登录 / 注册
首页
>
试题广场
>
两数之和
[编程题]两数之和
热度指数:12233
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 256M,其他语言512M
算法知识视频讲解
给定一个整数序列
和目标整数
,寻找一对下标
满足
且
。
测试数据保证恰有一组解,你需要求出对应的下标对
作为序列并返回。
传入参数:
整数序列
,长度满足
且
;
目标整数
,满足
。
返回值:
一对整数下标
构成的序列,满足
。
示例1
输入
[0,7,2,1],9
输出
[2,3]
说明
。
马上挑战
算法知识视频讲解
提交运行
算法知识视频讲解
添加笔记
求解答(0)
邀请回答
收藏(6)
分享
纠错
提交结果有问题?
1个回答
2篇题解
添加回答
0
Python3
牛客83749222号
class
Solution
:
def
twoSum
(
self
,
nums
: List[
int
],
target
:
int
) -> List[
int
]:
n={}
for
i,j
in
enumerate
(nums):
c=target-j
if
c
in
n:
return
[n[c]+
1
,i+
1
]
n[j] = i
return
[]
发表于 2025-06-25 19:54:19
回复(0)
这道题你会答吗?花几分钟告诉大家答案吧!
提交观点
问题信息
难度:
1条回答
6收藏
22浏览
热门推荐
相关试题
请画出在包含 14 个结点的二项堆...
高级算法
评论
(1)
好串
栈
过关题目
评论
(1)
约瑟夫环
过关题目
语言题
评论
(2)
对于我们来说,谁是好的顾客?
销售常识
评论
(1)
小红书用户在不同使用场景下,对内容...
需求分析
评论
(1)
两数之和
扫描二维码,关注牛客网
意见反馈
下载牛客APP,随时随地刷题
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @param target int整型 * @return int整型一维数组 */ public int[] twoSum (int[] nums, int target) { // write code here } }
class Solution { public: /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型vector * @param target int整型 * @return int整型vector */ vector
twoSum(vector
& nums, int target) { // write code here } };
#coding:utf-8 # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param nums int整型一维数组 # @param target int整型 # @return int整型一维数组 # class Solution: def twoSum(self , nums , target ): # write code here
using System; using System.Collections.Generic; class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @param target int整型 * @return int整型一维数组 */ public List
twoSum (List
nums, int target) { // write code here } }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @param target int整型 * @return int整型一维数组 */ function twoSum( nums , target ) { // write code here } module.exports = { twoSum : twoSum };
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param nums int整型一维数组 # @param target int整型 # @return int整型一维数组 # class Solution: def twoSum(self , nums: List[int], target: int) -> List[int]: # write code here
package main import "fmt" /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @param target int整型 * @return int整型一维数组 */ func twoSum( nums []int , target int ) []int { // write code here }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @param numsLen int nums数组长度 * @param target int整型 * @return int整型一维数组 * @return int* returnSize 返回数组行数 */ int* twoSum(int* nums, int numsLen, int target, int* returnSize ) { // write code here }
# # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # # @param nums int整型一维数组 # @param target int整型 # @return int整型一维数组 # class Solution def twoSum(nums, target) # write code here end end
object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @param target int整型 * @return int整型一维数组 */ def twoSum(nums: Array[Int],target: Int): Array[Int] = { // write code here } }
object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @param target int整型 * @return int整型一维数组 */ fun twoSum(nums: IntArray,target: Int): IntArray { // write code here } }
import java.util.*; public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @param target int整型 * @return int整型一维数组 */ public int[] twoSum (int[] nums, int target) { // write code here } }
/** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @param target int整型 * @return int整型一维数组 */ export function twoSum(nums: number[], target: number): number[] { // write code here }
public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @param target int整型 * @return int整型一维数组 */ func twoSum ( _ nums: [Int], _ target: Int) -> [Int] { // write code here } }
struct Solution{ } impl Solution { fn new() -> Self { Solution{} } /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * * @param nums int整型一维数组 * @param target int整型 * @return int整型一维数组 */ pub fn twoSum(&self, nums: Vec
, target: i32) -> Vec
{ // write code here } }
[0,7,2,1],9
[2,3]