https://leetcode.com/problems/two-sum/
Algorithm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
Input: nums, target
n ← length(nums)
nums ← np.array(nums)
s_nums ← sorted nums
idx ← changed index list
for ix_1 ← 0 to n-1
for ix_2 ← ix_1 to n
e_1 ← s_nums[ix_1]
e_2 ← s_nums[ix_2]
if e_1 + e_2 > target
break
elif e_1 + e_2 = target
return [idx[ix_1], idx[ix_2]]
Code
import numpy as np
class Solution:
def twoSum(self, nums, target):
n = len(nums)
idx, nums = np.argsort(nums), np.sort(nums)
for ix_1 in range(n - 1):
for ix_2 in range(ix_1 + 1, n):
e_1, e_2 = nums[ix_1], nums[ix_2]
if e_1 + e_2 > target:
break
elif e_1 + e_2 == target:
return [idx[ix_1], idx[ix_2]]
172ms / 28.4MB
PREVIOUSEtc