C++ code with simple logic


  • 0

    First time posting, and I don't know how well and how to improve this code.
    Thanks for your feedback!

    class Solution {
    public:
        vector<int> nextGreaterElement(vector<int>& findNums, vector<int>& nums) {
            
            vector<int> result;
            int j = 0;
            int i = 0;
            bool found = false;
            
            for(i = 0; i < findNums.size(); i++)
            {
                std::vector<int>::iterator it;
                it = find(nums.begin(), nums.end(), findNums.at(i));
                int index = distance(nums.begin(), it);
                
                for(j = index+1; j < nums.size(); j++)
                {
                   if(nums.at(j) > findNums.at(i))
                   {
                       result.push_back(nums.at(j));
                       found = true;
                       break;
                   }
                }
                
                // not found in the search vector
                if(j >= nums.size()-1 && !found)
                {
                    result.push_back(-1);
                }
                
                found = false;
            }
            
            return result;
        }
    };
    

Log in to reply
 

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