LeetCode 100229.. 最长公共前缀的长度
原题链接
中等
作者:
我是java同学
,
2024-02-19 07:23:03
,
所有人可见
,
阅读 33
class Solution {
public:
int longestCommonPrefix(vector<int>& a, vector<int>& b) {
int n = a.size(), m = b.size();
int res = 0;
unordered_map<int, int> hash;
for (int i = 0; i < a.size(); i ++ ) {
string t;
for (auto c: to_string(a[i])) {
t += c;
hash[stoi(t)] ++ ;
}
}
for (int i = 0; i < b.size(); i ++ ) {
string t;
for (auto c: to_string(b[i])) {
t += c;
if (hash.count(stoi(t))) res = max(res, (int)t.size());
}
}
return res;
}
};