Crawler Log Folder LeetCode solution in C++
1// 1.using stack2class Solution {3public:4int minOperations(vector<string> &logs) {5if (logs.size() == 0) return 0;67stack<string> st;8for (auto x : logs) {9if (x[0] != '.') // Move to the child folder so add children10st.push(x);11else if (x == "../") { // Move to the parent folder of the current folder12// so pop13if (!st.empty())14st.pop();15else16continue; // don’t move the pointer beyond the main folder.17}18}19return st.size();20}21};22// 2.23class Solution {24public:25int minOperations(vector<string> &logs) {26int ans = 0;27for (string log : logs) {28if (log == "../") { // go deeper29ans--;30ans = max(ans, 0);31} else if (log != "./") // one level up32ans++;33}34return ans;35}36};37// 3.38class Solution {39public:40int minOperations(vector<string> &logs) {41int res = 0;42for (string s : logs) {43if (s == "../")44res = max(0, --res);45else if (s == "./")46continue;47else48res++;49}50return res;51}52};