diff --git a/0043_multiply_strings/multiply_strings.cc b/0043_multiply_strings/multiply_strings.cc index 77436e4..0ca6c59 100644 --- a/0043_multiply_strings/multiply_strings.cc +++ b/0043_multiply_strings/multiply_strings.cc @@ -5,23 +5,29 @@ using namespace std; class Solution { public: string multiply(string num1, string num2) { - string res(num1.length() + num2.length(), '0'); + vector v(num1.length() + num2.length()); for (int i = num2.length() - 1; i >= 0; i--) { - int j, carry = 0; - for (j = num1.length() - 1; j >= 0; j--) { - carry += (num1[j] - '0') * (num2[i] - '0') + (res[i + j + 1] - '0'); - res[i + j + 1] = carry % 10 + '0'; - carry /= 10; + for (int j = num1.length() - 1; j >= 0; j--) { + int a = num2[j] - '0'; + int b = num1[i] - '0'; + v[i + j + 1] = a * b; } - res[i + j + 1] = carry + '0'; } - int i; + int i, carry = 0; + string res(v.size(), '0'); + for (i = v.size() - 1; i >= 0; i--) { + carry += v[i]; + res[i] += carry % 10; + carry /= 10; + } + for (i = 0; i < res.length() - 1; i++) { if (res[i] != '0') { break; } } + return res.substr(i); } }; diff --git a/0131_palindrome_patitioning/palindrome_partition.cc b/0131_palindrome_patitioning/palindrome_partition.cc new file mode 100644 index 0000000..9897ddb --- /dev/null +++ b/0131_palindrome_patitioning/palindrome_partition.cc @@ -0,0 +1,42 @@ +#include + +using namespace std; + +class Solution { +public: + vector> partition(string s) { + vector> res; + vector> isPalindrome(s.size(), vector(s.size(), true)); + + // From bottom to up + for (int i = s.length() - 1; i >= 0; i--) { + // From left to right + for (int j = i + 1; j < s.length(); j++) { + // notebook for palindrome substring judgement + isPalindrome[i][j] = s[i] == s[j] && isPalindrome[i + 1][j - 1]; + } + } + + dfs(s, 0, isPalindrome, res); + return res; + } + +private: + vector ans; + void dfs(const string& s, int start, vector>& isPalindrome, vector>& res) { + // DFS for combination. When the start index reaches to the end, all + // the substrings are collected. + if (start == s.length()) { + res.push_back(ans); + return; + } + + for (int i = start; i < s.length(); i++) { + if (isPalindrome[start][i]) { + ans.push_back(s.substr(start, i - start + 1)); + dfs(s, i + 1, isPalindrome, res); + ans.pop_back(); + } + } + } +}; diff --git a/0133_clone_graph/clone_graph.cc b/0133_clone_graph/clone_graph.cc new file mode 100644 index 0000000..ca7d8e2 --- /dev/null +++ b/0133_clone_graph/clone_graph.cc @@ -0,0 +1,65 @@ +#include + +using namespace std; + +// Definition for a Node. +class Node { +public: + int val; + vector neighbors; + Node() { + val = 0; + neighbors = vector(); + } + Node(int _val) { + val = _val; + neighbors = vector(); + } + Node(int _val, vector _neighbors) { + val = _val; + neighbors = _neighbors; + } +}; + +class Solution { +public: + Node* cloneGraph(Node* node) { + if (node == nullptr) { + return node; + } +#if 1 // DFS + if (cloned.find(node) != cloned.end()) { + return cloned[node]; + } + + cloned[node] = new Node(node->val); + + for (auto& neighbor : node->neighbors) { + cloned[node]->neighbors.emplace_back(cloneGraph(neighbor)); + } +#else // BFS + queue q; + q.push(node); + cloned[node] = new Node(node->val); + + while (!q.empty()) { + int size = q.size(); + for (int i = 0; i < size; i++) { + auto n = q.front(); + q.pop(); + + for (auto& neighbor : n->neighbors) { + if (cloned.find(neighbor) == cloned.end()) { + cloned[neighbor] = new Node(neighbor->val); + q.push(neighbor); + } + cloned[n]->neighbors.emplace_back(cloned[neighbor]); + } + } + } +#endif + return cloned[node]; + } +private: + unordered_map cloned; +}; diff --git a/0567_permutation_in_string/permutation_in_string.cc b/0567_permutation_in_string/permutation_in_string.cc index 541b1cb..1061ae6 100644 --- a/0567_permutation_in_string/permutation_in_string.cc +++ b/0567_permutation_in_string/permutation_in_string.cc @@ -10,18 +10,20 @@ class Solution { count[c]++; } - int l = 0, r = 0, len = 0; + int l = 0, r = 0, hits = 0; while (r < s2.length()) { if (--count[s2[r++]] >= 0) { - len++; + hits++; } + // When the window length equals to the hit length, + // the permutation is contained. if (r - l >= s1.length()) { - if (len == s1.length()) { + if (hits == s1.length()) { return true; } if (++count[s2[l++]] > 0) { - len--; + hits--; } } }