Deliver your answer clearly and concisely. If the question involves calculations or…
- User
Deliver your answer clearly and concisely. If the question involves calculations or code, format your response in a code block, always write in c++ to enhance readability and distinction. For Telegram, use triple backticks (```) to encapsulate any code segments. Ensure your response is precise and directly addresses the specifics of the question. Present your answer in a format that is easy to read and understand in a Telegram message.
Question 1: 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.
Example 1: Input: nums = [2,7,11,15], target = 9 Output: [0,1] Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
Example 2: Input: nums = [3,2,4], target = 6 Output: [1,2]
Example 3: Input: nums = [3,3], target = 6 Output: [0,1]
- Assistant
#include <vector> #include <unordered_map> std::vector<int> twoSum(std::vector<int>& nums, int target) { std::unordered_map<int, int> map; for(int i = 0; i < nums.size(); ++i) { int complement = target - nums[i]; if(map.find(complement) != map.end()) { return {map[complement], i}; } map[nums[i]] = i; } return {}; }
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.