Funcia
Lv.1arrayhash

Two Sum

Memory limit
256 MB
Correctness time limit
10000 ms
Acceptance
25.0%
Solvers
1
0

Two Sum

Given an integer array nums and an integer target, return the two distinct indices i < j such that nums[i] + nums[j] == target, as the array [i, j].

Exactly one answer exists. You may not use the same element twice.

Constraints

  • 2≤2 \le nums.length ≤100,000\le 100{,}000
  • −109≤-10^9 \le nums[i] ≤109\le 10^9
  • −2×109≤-2 \times 10^9 \le target ≤2×109\le 2 \times 10^9

Examples

numstargetresult
[2, 7, 11, 15]9[0, 1]
[3, 2, 4]6[1, 2]

Expected time complexity: O(N)O(N). The O(N2)O(N^2) pairwise scan exceeds the performance time limit.

Function signature

solution(nums: int[], target: int) → int[]

Expected complexity

Time
O(N)
Space
O(N)

Examples

numstargetExpected
[2,7,11,15]9[0,1]
[3,2,4]6[1,2]

Time limits per language

LanguageEfficiency time limit
C (GCC, gnu17)200 msderived
C++ (GCC, gnu++23)200 msderived
Python 3.131000 msderived
Java 251000 msderived
Kotlin 2.4 (JVM)1030 msderived
C# 14 (.NET 10)1000 msderived

Test groups

GroupWeightTestsVisibility
sample02inputs and outputs
correctness4010verdict only
performance604verdict only
Loading editor…
Log in to run or submit code.

No result yet. Run or submit your code.