LeetCode算法入门(第三十五天)
字符串
49.字母异位词分组
class Solution {
public:
vector<vector<string>> groupAnagrams(vector<string>& strs) {
unordered_map<string, vector<string>> mp;
for(string& str: strs){
string key = str;
sort(key.begin(), key.end());
mp[key].push_back(str);
}
vector<vector<string>> ans;
for(auto it = mp.begin(); it != mp.end(); it++){
ans.push_back(it->second);
}
return ans;
}
};
43.字符串相乘
class Solution {
public:
string multiply(string num1, string num2) {
vector<int> A; vector<int> B;
int m = num1.size(); int n = num2.size();
for(int i = m - 1; i >= 0; i--){
A.push_back(num1[i] - '0');
}
for(int j = n - 1; j >= 0; j--){
B.push_back(num2[j] - '0');
}
vector<int> C;
int count = 0;
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
C[i + j] += A[i] * B[j];
}
}
for(int k = 0; k < C.size(); k++){
count += C[k];
C[k] = count % 10;
count = count / 10;
}
int l = C.size() - 1;
while(l > 0 && !C[l]){
l--;
}
string res;
while(l >= 0){
res += C[l] + '0';
l--;
}
return res;
}
};
|