Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR SWIFT

two sum solution

       vector<int> twoSum(vector<int> &nums, int target){ // Brute Force Approach
            for(int i = 0; i < nums.size(); i++){
                for(int j = i+1; j < nums.size(); j++){
                    if(nums[i] + nums[j] == target){
                        vector<int> result;
                        result.push_back(i);
                        result.push_back(j);
                        return result;
                    }
                }
            }
            return vector<int>();  // return empty vector if no two sum solution
        }
Source by shareablecode.com #
 
PREVIOUS NEXT
Tagged: #sum #solution
ADD COMMENT
Topic
Name
2+9 =