#P105. Find two sum
Find two sum
題目描述
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.
請使用 升序 輸出答案
輸入格式
There are two parts in one input line:
- The first past is the target to sum (9 in Sample 01)
- 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))
輸出格式
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
Samples
["9 2 7 11 15","20 9 10 5 15"]
["0 1","2 3"]
提示
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")
原始資料
- Zero1 題號:
a105 - Hydro 題號:
Z0105 - Locale:
zh_TW - Display:
open