Description Two sum Given a list of integers nums and an integer target, return a list containing the indices of the two numbers such that they add up to the target. You may assume that each input would have exactly one solution, and you may not use the same element twice. When you return the answer, make sure the smallest index comes first in the list. Here are some examples. Input: nums = [2, 7, 11, 15] • target=9 Output: [0, 1] • The indices of the two numbers that add up to the target 9 are [0, 1] because nums[0] + nums[1] =2+7= 9. In this example, we have a list nums containing integers, and the target value is 9. The function should return the indices of the two numbers (2 and 7) in the array that add up to the target. Example 2: Input: nums [3, 2, 4] • target = 6 Output: [1,2] • The indi Challenge + scaffold.py 1 def solution (nums, targe 2 #your code goes here /home/scaffold.py Spaces: 4 (Auto) Terminal Sub All changes Click here to activate the terminal

icon
Related questions
Question
Can’t figure this out can you pls explain your solution I need to know the thought process.
xt
Description
Two sum
Given a list of integers nums and an integer target, return a list containing the indices of the two
numbers such that they add up to the target. You may assume that each input would have exactly
one solution, and you may not use the same element twice. When you return the answer, make
sure the smallest index comes first in the list.
Here are some examples.
Input:
•nums = [2, 7, 11, 15]
• target=9
Two sum
Output: [0, 1]
• The indices of the two numbers that add up to the target 9 are [0, 1] because nums[0] +
nums[1] = 2 + 7 = 9.
In this example, we have a list nums containing integers, and the target value is 9. The function
should return the indices of the two numbers (2 and 7) in the array that add up to the target.
Example 2:
Input:
nums [3, 2, 4]
• target = 6
Output: [1, 2]
The indices of the two numbers that add up to the target 6 are [1, 2] because nums[1] +
-1
Challenge
+
scaffold.py
1 def solution (nums, target):
#your code goes here
2
/home/scaffold.py Spaces: 4 (Auto)
Terminal
Submis
All changes save
Click here to activate the terminal
✓Ma
Transcribed Image Text:xt Description Two sum Given a list of integers nums and an integer target, return a list containing the indices of the two numbers such that they add up to the target. You may assume that each input would have exactly one solution, and you may not use the same element twice. When you return the answer, make sure the smallest index comes first in the list. Here are some examples. Input: •nums = [2, 7, 11, 15] • target=9 Two sum Output: [0, 1] • The indices of the two numbers that add up to the target 9 are [0, 1] because nums[0] + nums[1] = 2 + 7 = 9. In this example, we have a list nums containing integers, and the target value is 9. The function should return the indices of the two numbers (2 and 7) in the array that add up to the target. Example 2: Input: nums [3, 2, 4] • target = 6 Output: [1, 2] The indices of the two numbers that add up to the target 6 are [1, 2] because nums[1] + -1 Challenge + scaffold.py 1 def solution (nums, target): #your code goes here 2 /home/scaffold.py Spaces: 4 (Auto) Terminal Submis All changes save Click here to activate the terminal ✓Ma
Expert Solution
steps

Step by step

Solved in 4 steps with 2 images

Blurred answer