Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions LeetCode/606. Construct String from Binary Tree
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Solution {
public:
string str = "";

void dfs(TreeNode* node, TreeNode* root) {
if (node == NULL)
return;

if (node != root)
str += '(';
str += to_string(node->val);
dfs(node->left, root);
if (node->left==NULL && node->right!=NULL)
str += "()";

dfs(node->right, root);
if (node != root)
str += ')';
}

string tree2str(TreeNode* root) {
dfs(root, root);

return str;
}
};
40 changes: 40 additions & 0 deletions LeetCode/609. Find Duplicate File in System
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class Solution {
public:
vector<vector<string>> findDuplicate(vector<string>& paths) {
vector<vector<string>> ans;
unordered_map<string, vector<string>> m;

for (auto i: paths) {
string chunk = "", path = "";
stringstream ss(i);

while(ss >> chunk) {
// cout<<chunk<<endl;

if (path == "") { // this will run, if the chunk is a path
path = chunk;
continue;
}

// this will run, if the chunk is a file
string reversedContent = "";
for (int j=chunk.length()-1; chunk[--j] != '(';)
reversedContent += chunk[j];
// cout<<"reversed content = "<<reversedContent<<endl;

string fileName = "";
for (int j=0; chunk[j] != '('; j++)
fileName += chunk[j];
// cout<<"fileName = "<<fileName<<endl;

m[reversedContent].push_back(path + '/' + fileName);
}
}

for (auto &[revContent, pathAndFile]: m)
if (pathAndFile.size() > 1)
ans.push_back(pathAndFile);

return ans;
}
};