Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.You may assume that each input would have exactly one solution, and you may not use the same element twice.You can return the answer in any order
Question
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.You may assume that each input would have exactly one solution, and you may not use the same element twice.You can return the answer in any order
Solution
Sure, here is a Python solution using a dictionary to store the numbers and their indices.
def twoSum(nums, target):
num_dict = {}
for i, num in enumerate(nums):
if target - num in num_dict:
return [num_dict[target - num], i]
num_dict[num] = i
Here's how it works:
-
We define a function
twoSumthat takes a list of numbers (nums) and a target number (target) as input. -
We create an empty dictionary
num_dictto store the numbers from the list and their indices. -
We start a loop that goes through each number in the list. For each number, we do the following:
- We check if
target - numis innum_dict. If it is, that means we
- We check if
Similar Questions
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
1. Two SumEasy53.1K1.8KCompaniesGiven an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.You may assume that each input would have exactly one solution, and you may not use the same element twice.You can return the answer in any order. Example 1:Input: nums = [2,7,11,15], target = 9Output: [0,1]Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].Example 2:Input: nums = [3,2,4], target = 6Output: [1,2]Example 3:Input: nums = [3,3], target = 6Output: [0,1] Constraints:2 <= nums.length <= 104
Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.You must write an algorithm with O(log n) runtime complexity. Example 1:Input: nums = [1,3,5,6], target = 5Output: 2Example 2:Input: nums = [1,3,5,6], target = 2Output: 1Example 3:Input: nums = [1,3,5,6], target = 7Output: 4 Constraints:1 <= nums.length <= 104-104 <= nums[i] <= 104nums contains distinct values sorted in ascending order.-104 <= target <= 104
Given an array of integers nums sorted in non-decreasing order, find the starting and ending position of a given target value.If target is not found in the array, return [-1, -1].You must write an algorithm with O(log n) runtime complexity. Example 1:Input: nums = [5,7,7,8,8,10], target = 8Output: [3,4]Example 2:Input: nums = [5,7,7,8,8,10], target = 6Output: [-1,-1]Example 3:Input: nums = [], target = 0Output: [-1,-1] Constraints:0 <= nums.length <= 105-109 <= nums[i] <= 109nums is a non-decreasing array.-109 <= target <= 109
Given an integer array nums of length n and an integer target, find three integers in nums such that the sum is closest to target.Return the sum of the three integers.You may assume that each input would have exactly one solution. Example 1:Input: nums = [-1,2,1,-4], target = 1Output: 2Explanation: The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).Example 2:Input: nums = [0,0,0], target = 1Output: 0Explanation: The sum that is closest to the target is 0. (0 + 0 + 0 = 0). Constraints:3 <= nums.length <= 500-1000 <= nums[i] <= 1000-104 <= target <= 104
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.