Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR SWIFT

1. Two Sum

class Solution {
    public:
        vector<int> twoSum(vector<int>& nums, int target) {
            unordered_map<int, int> indices;
            for(int i=0; i<nums.size(); i++) indices[nums[i]] = i;

            for(int i=0; i<nums.size(); i++){
                int left = target - nums[i];
                if (indices.count(left) && indices[left]!=i) return {i, indices[left]};
            }
            return {};
        }
    };
Source by hackmd.io #
 
PREVIOUS NEXT
Tagged: #Two #Sum
ADD COMMENT
Topic
Name
2+6 =