Beats 88.87% C++ submissions


  • 0
    I
    class Solution {
    public:
        vector<int> twoSum(vector<int>& nums, int target) {
            vector<int> result;
            vector<int> nums2(nums);
            sort(nums.begin(), nums.end());
            for (int i = 0; i < nums2.size(); i ++) {
                bool found = false;
                if (binary_search(nums.begin(), nums.end(), target - nums2.at(i))) {
                    for (int j = 0; j < nums2.size(); j ++) {
                        if (nums2.at(j) == target - nums2.at(i) and j != i) {
                            result.push_back(i);
                            result.push_back(j);
                            found = true;
                            break;
                        }
                    }
                }
                if (found) break;
            }
            return result;
        }
    };
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.