c++ easy solution using std::find.


  • 0
    C
    vector<int> nextGreaterElement(vector<int>& findNums, vector<int>& nums) {
            vector<int> res;
            for (int a : findNums) {
                bool find = false;
                for (auto i = std::find(nums.begin(), nums.end(), a) +1 ; i < nums.end(); i++) {
                    if (*i >a) {
                        res.push_back(*i);
                        find = true;
                        break;
                    }
                }
                if (!find) res.push_back(-1);
            }
            return res;
        }
    

Log in to reply
 

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