Online Compiler - CodeBook Solution
</>
CodeBook
.
Home
Services
DSA Practice
Compiler
Blog
Contact
ā°
ā” CodeBook Compiler
ā Back to DSA
C++ (GCC)
Python 3
Java
JavaScript (Node)
C (GCC)
Go
Ruby
Clear
ā¶ Run Code
š Code Editor
// Two Sum - LeetCode #1 // Given an array of integers nums and an integer target, // return indices of the two numbers such that they add up to target. #include <bits/stdc++.h> using namespace std; class Solution { public: vector<int> twoSum(vector<int>& nums, int target) { unordered_map<int, int> mp; for (int i = 0; i < nums.size(); i++) { int complement = target - nums[i]; if (mp.count(complement)) { return {mp[complement], i}; } mp[nums[i]] = i; } return {}; } }; int main() { Solution sol; vector<int> nums = {2, 7, 11, 15}; int target = 9; auto result = sol.twoSum(nums, target); cout << "Output: [" << result[0] << ", " << result[1] << "]" << endl; return 0; }
š Output
// Your output will appear here...
š„ Standard Input (optional)