Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am writing leetcode question 1019 (Next Greater Node In Linked List), using the class template provided by the leetcode website. At present, the code can run correctly on the official website. Today I want to execute it in my own compiler, but I don’t know how to write this type of print (cout). I would like to ask how to write print (cout) when encountering this type of class.

Below is my class code:

class Solution {
public:
    vector<int> nextLargerNodes(ListNode* head) {
        stack<int> s;
        vector<int> a;
        for (auto p = head; p; p = p->next) {
            a.emplace_back(p->val);
        }
        vector<int> ans(a.size());
        for (int i = 0; i < a.size(); ++i) {
            while (s.size() && a[s.top()] < a[i]) {
                auto top = s.top(); s.pop();
                ans[top] = a[i];
            }
            s.push(i);
        }
        return ans;
    }
};


Beginners sincerely ask, maybe you can also attach your writing so that I can learn, thank you very much.

What I have tried:

Today I want to execute it in my own compiler, but I don’t know how to write this type of print (cout). I would like to ask how to write print (cout) when encountering this type of class. Beginners sincerely ask, maybe you can also attach your writing so that I can learn, thank you very much.
Posted
Updated 16-Sep-20 22:52pm
Comments
Amy Zhou 17-Sep-20 4:16am    
Like the example input and output of Leetcode's official website.

Example 1:

Input: [2,1,5]
Output: [5,5,0]

Example 2:

Input: [2,7,4,3,5]
Output: [7,0,5,5,0]

To print an integer use the insertion operator << with cout:
C++
cout << i << endl;

To print a vector of integers use the same tecnique while iterating on all the vector items, e.g.
C++
for (size_t n = 0; n < v.size(); ++n)
{
  cout << v[n] << " ";
}
cout << endl;

or
C++
for (auto  i : v)
{
  cout << i << " ";
}
cout << endl;
 
Share this answer
 
v2
 
Share this answer
 
Comments
Amy Zhou 17-Sep-20 4:18am    
I know this, but how to match the title requirements is presented in "vector<int> nextLargerNodes(ListNode* head)" I don't understand...
Amy Zhou 17-Sep-20 4:20am    
https://leetcode.com/problems/next-greater-node-in-linked-list/

This is the input and output required by this question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900