문제
답안
파이썬
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
for idx, num in enumerate(nums):
diff = target - num
if diff in nums[idx+1:]:
return [idx, idx+1+nums[idx+1:].index(diff)]
러스트
impl Solution {
pub fn two_sum(nums: Vec<i32>, target: i32) -> Vec<i32> {
for (idx, num) in nums.iter().enumerate() {
let diff = target - num;
match nums[idx + 1..].iter().position(|&x| x == diff) {
Some(i) => {
return vec![idx as i32, (idx + 1 + i) as i32];
}
None => {}
};
}
vec![0,0]
}
}