classSolution { public: intlengthLongestPath(string input){ if (input.empty()) return0; int res = 0; stack<string> s; input += '\n'; int b = 0, e = input.find('\n'), lvl = 0, len = 0; for (; e != string::npos; e = input.find('\n', b)) { while (s.size() > lvl) { len -= s.top().length(); s.pop(); } s.push(input.substr(b, e - b + 1)); len += s.top().length(); if (s.top().find('.') != string::npos) { res = max(res, len - 1); } for (b = e + 1, lvl = 0; b < input.length() && input[b] == '\t'; ++b, ++lvl); } return res; } };
Posted onEdited onInLeetCodeDisqus: Symbols count in article: 447Reading time ≈1 mins.
O(n2) time O(1) space 因为题目要求任何解法都可以,所以用最直白的思路,先从大到小找到每一个数,然后把这个数翻转到最开始,再翻转到对应的位置即可 类似bubble sort
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
classSolution { public: vector<int> pancakeSort(vector<int>& A){ int n = A.size(); vector<int> res; for (int i = n - 1; i >= 0; --i) { int k = distance(begin(A), find(begin(A), begin(A) + i + 1, i + 1)); res.push_back(k + 1); reverse(begin(A), begin(A) + k + 1); res.push_back(i + 1); reverse(begin(A), begin(A) + i + 1); } return res; } };
classSolution { public: vector<string> topKFrequent(vector<string>& words, int k){ unordered_map<string, int> m; for (constauto &w : words) { ++m[w]; } int n = words.size(); vector<TrieNode *> buckets(n + 1, nullptr); for (auto&& b : buckets) { b = new TrieNode; } for (constauto &p : m) { insert(buckets[p.second], p.first); } vector<string> res; for (int i = n; i >= 0 && k > 0; --i) { // 题目要求高频到低频 getWords(buckets[i], k, res); } return res; }
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ classCodec { public:
// Encodes a tree to a single string. stringserialize(TreeNode* root){ return root ? serialize(root->left) + serialize(root->right) + to_string(root->val) + " " : ""; }
// Decodes your encoded data to tree. TreeNode* deserialize(string data){ istringstreaminput(data); int x; vector<int> v; while (input >> x) { v.push_back(x); } return insert(v, INT_MIN, INT_MAX); }
// build BST TreeNode *insert(vector<int> &v, int mn, int mx){ if (v.empty() || v.back() < mn || v.back() > mx) returnnullptr; auto root = new TreeNode(v.back()); v.pop_back(); root->right = insert(v, root->val, mx); root->left = insert(v, mn, root->val); return root; } };
// Your Codec object will be instantiated and called as such: // Codec codec; // codec.deserialize(codec.serialize(root));
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ classCodec { public:
// Encodes a tree to a single string. stringserialize(TreeNode* root){ return root ? serialize(root->left) + serialize(root->right) + toByte(root->val) : ""; }
stringtoByte(int x){ stringres(4, 0); for (int i = 3; i >= 0; --i, x >>= 4) { res[i] = x & 15; } return res; }
// Decodes your encoded data to tree. TreeNode* deserialize(string data){ vector<int> v; for (int i = 0; i < size(data); i += 4) { v.push_back(toInt(data, i)); } return insert(v, INT_MIN, INT_MAX); }
inttoInt(conststring &s, int b){ int res = s[b]; for (int i = 1; i < 4; ++i) { res <<= 4; // 注意只左移3次!! res |= s[b + i]; } return res; }
// build BST TreeNode *insert(vector<int> &v, int mn, int mx){ if (v.empty() || v.back() < mn || v.back() > mx) returnnullptr; auto root = new TreeNode(v.back()); v.pop_back(); root->right = insert(v, root->val, mx); root->left = insert(v, mn, root->val); return root; } };
// Your Codec object will be instantiated and called as such: // Codec codec; // codec.deserialize(codec.serialize(root));
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ classCodec { public:
// Encodes a tree to a single string. stringserialize(TreeNode* root){ return root ? serialize(root->left) + serialize(root->right) + toByte(root->val) : ""; }
stringtoByte(int x){ stringres(4, 0); for (int i = 3; i >= 0; --i, x >>= 4) { res[i] = x & 15; } return res; }
// Decodes your encoded data to tree. TreeNode* deserialize(string data){ string_view sv{data}; return insert(sv, INT_MIN, INT_MAX); }
// int toInt(string_view sv) { // int res = 0, n = size(sv); // for (int i = 0, shift = 0; i < 4; ++i, shift += 4) { // res |= (sv[n - i - 1] << shift); // } // return res; // }
inttoInt(string_view sv){ int res = 0; for (int i = 0, shift = 0; i < 4; ++i, shift += 4, sv.remove_suffix(1)) { res |= (sv.back() << shift); } return res; }
// build BST TreeNode *insert(string_view &sv, int mn, int mx){ if (sv.empty()) returnnullptr; int v = toInt(sv); if (v < mn || v > mx) returnnullptr; sv.remove_suffix(4); auto root = new TreeNode(v); root->right = insert(sv, root->val, mx); root->left = insert(sv, mn, root->val); return root; } };
// Your Codec object will be instantiated and called as such: // Codec codec; // codec.deserialize(codec.serialize(root));
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ classCodec { public:
// Encodes a tree to a single string. stringserialize(TreeNode* root){ return root ? to_string(root->val) + " " + serialize(root->left) + serialize(root->right) : ""; }
// Decodes your encoded data to tree. TreeNode* deserialize(string data){ istringstreaminput(data); string token; TreeNode dummy_root(INT_MIN); // 为了能让结果在右子树要选择INT_MIN while (input >> token) { insert(&dummy_root, stoi(token)); } return dummy_root.right; }
// build BST TreeNode *insert(TreeNode *root, int val){ if (!root) return root; if (val < root->val) { root->left = root->left ? insert(root->left, val) : new TreeNode(val); } else { root->right = root->right ? insert(root->right, val) : new TreeNode(val); } return root; } };
// Your Codec object will be instantiated and called as such: // Codec codec; // codec.deserialize(codec.serialize(root));
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) : val(x), left(NULL), right(NULL) {} * }; */ classCodec { public:
// Encodes a tree to a single string. stringserialize(TreeNode* root){ return root ? to_string(root->val) + " " + serialize(root->left) + serialize(root->right) : ""; }
// Decodes your encoded data to tree. TreeNode* deserialize(string data){ istringstreaminput(data); string token; TreeNode dummy_root(0); while (input >> token) { if (dummy_root.left) { insert(dummy_root.left, stoi(token)); } else { dummy_root.left = new TreeNode(stoi(token)); } } return dummy_root.left; }
// build BST voidinsert(TreeNode *root, int val){ if (!root) return; if (val < root->val) { if (root->left) { insert(root->left, val); } else { root->left = new TreeNode(val); } } else { if (root->right) { insert(root->right, val); } else { root->right = new TreeNode(val); } } } };
// Your Codec object will be instantiated and called as such: // Codec codec; // codec.deserialize(codec.serialize(root));
Posted onEdited onInLeetCodeDisqus: Symbols count in article: 343Reading time ≈1 mins.
O(n) time O(1) space
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution { public: stringreverseVowels(string s){ bool m[256] = {false}; for (char v : "aeiouAEIOU") { m[v] = true; } int n = size(s), l = 0, r = n - 1; while (l < r) { while (l < r && !m[s[l]]) ++l; while (l < r && !m[s[r]]) --r; swap(s[l++], s[r--]); } return s; } };