a105: Find two sum
Tags :
Accepted rate : 61人/63人 ( 97% ) [非即時]
評分方式:
Strictly

最近更新 : 2022-08-18 01:57

Content

Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.

給定一個整數數列nums 和一個整數 target

返回兩個數字,這兩個數加起來就等於target

 

You may assume that each input would have exactly one solution and each number can only be used once.

您可以假設每個輸入只有一個解決方案,並且每個數字只能使用一次

 

You should return the answer in ascending order.

請使用 升序 輸出答案

Input

There are two parts in one input line:

  1. The first past is the target to sum (9 in Sample 01)
  2. The second part is the list of nums (2, 7, 11, 15 in Sample 01)

The input is well parsed in two variable: target and nums in the given template code

nums[i] is integer < 30000 for all i in range(size(num))

Output

Two integers represent the index from the list (location of list start from 0)

If you use list or numpy array, you can directly print the list or numpy array

Sample Input #1
9 2 7 11 15
Sample Output #1
0 1
Sample Input #2
20 9 10 5 15
Sample Output #2
2 3
測資資訊:
記憶體限制: 64 MB
不公開 測資點#0 (33%): 1.0s , <1K
不公開 測資點#1 (33%): 1.0s , <1K
不公開 測資點#2 (34%): 1.0s , <1K
Hint :

Brute force:

simply go through twice loops to find a pair which sum to target, the "time complexity" is O(N^2)

Can you think out some method to accelerate the program to reduce the time complexity to O(NlogN) or even O(N)? (such as "sort","hashing"or "set")

Tags:
出處:
[管理者:
kichan@g.pui... (陳嘉賢CHAN KA IN)
]


ID User Problem Subject Hit Post Date
沒有發現任何「解題報告」