你好,我是辰chen,本文旨在准备考研复试或就业
文章题目大多来自于 leetcode,当然也可能来自洛谷或其他刷题平台
欢迎大家的关注,我的博客主要关注于考研408以及AIoT的内容
仅给出C++版代码
以下的几个专栏是本人比较满意的专栏(大部分专栏仍在持续更新),欢迎大家的关注:
ACM-ICPC算法汇总【基础篇】
ACM-ICPC算法汇总【提高篇】
AIoT(人工智能+物联网)
考研
CSP认证考试历年题解
题目链接:亲密字符串
C++版AC代码:
class Solution {
public:
bool buddyStrings(string s, string goal) {
/*
字符串长度不相等 return false;
分别去算s和goal中的元素出现的次数,不相等(元素组成不同)return false;
用cnt记录两个字符串对应位置不匹配的次数,同时遍历两个字符串,不匹配的次数>2 或者 = 1 return false;
不匹配的次数 = 2 return true;
不匹配的次数 = 0, 统计s串或者goal串的元素个数,如果有元素重复次数 >= 2, return true;
否则 return false;
*/
if (s.size() != goal.size()) return false;
unordered_map<char, int> m1, m2;
for (int i = 0; i < s.size(); i ++ ) m1[s[i]] ++;
for (int i = 0; i < goal.size(); i ++ ) m2[goal[i]] ++;
bool flag = false; // 用来判断重复元素的个数
for (auto i = m1.begin(); i != m1.end(); i ++ ) {
char c = i -> first;
if (m1[c] != m2[c]) return false;
if (m1[c] >= 2) flag = true; // 存在重复元素个数>=2
}
int cnt = 0;
for (int i = 0; i < s.size(); i ++ )
if (s[i] != goal[i]) cnt ++;
if (cnt > 2 || cnt == 1) return false;
else if (cnt == 2) return true;
else return flag;
}
};
题目链接:两句话中的不常见单词
C++版AC代码:
class Solution {
public:
vector<string> getwords(string s) {
vector<string> words;
for (int i = 0; i < s.size(); i ++ ) {
int j = i;
while (j < s.size()) {
if (s[j] >= 'a' && s[j] <= 'z') j ++;
else break;
}
string word = s.substr(i, j - i);
words.push_back(word);
i = j;
}
return words;
}
vector<string> uncommonFromSentences(string s1, string s2) {
vector<string> words1, words2;
words1 = getwords(s1);
words2 = getwords(s2);
unordered_map<string, int> m1, m2;
for (int i = 0; i < words1.size(); i ++ ) m1[words1[i]] ++;
for (int i = 0; i < words2.size(); i ++ ) m2[words2[i]] ++;
vector<string> res;
for (int i = 0; i < words1.size(); i ++ ) { // s1 中的不常用单词
string word = words1[i];
if (m1[word] == 1 && !m2[word])
res.push_back(word);
}
for (int i = 0; i < words2.size(); i ++ ) { // s2 中的不常用单词
string word = words2[i];
if (m2[word] == 1 && !m1[word])
res.push_back(word);
}
return res;
}
};
题目链接:公平的糖果交换
C++版AC代码:
class Solution {
public:
vector<int> fairCandySwap(vector<int>& aliceSizes, vector<int>& bobSizes) {
/*
爱丽丝糖果总大小:suma, 鲍勃糖果总大小:sumb
爱丽丝大小为alice的糖果与鲍勃大小为bob的糖果交换
suma - alice + bob = sumb - bob + alice
alice = bob + (suma - sumb) / 2
*/
int suma = 0, sumb = 0;
for (int i = 0; i < aliceSizes.size(); i ++ ) suma += aliceSizes[i];
for (int i = 0; i < bobSizes.size(); i ++ ) sumb += bobSizes[i];
unordered_set<int> s(aliceSizes.begin(), aliceSizes.end()); // 存储爱丽丝的所有糖果大小
int alice, bob, num = (suma - sumb) / 2;
for (int i = 0; i < bobSizes.size(); i ++ ) { // 对每一个bob去查是否有alice符合题意
bob = bobSizes[i];
alice = bob + num;
if (s.count(alice)) break;
}
return {alice, bob};
}
};
题目链接:卡牌分组
C++版AC代码:
n 个数的最大公约数,最大公约数博客见:数学知识:约数
class Solution {
public:
// 求解最大公约数板子:
int gcd(int a, int b) {
return b ? gcd(b, a % b) : a;
}
bool hasGroupsSizeX(vector<int>& deck) {
if (deck.size() < 2) return false;
unordered_map<int, int> m;
for (int i = 0; i < deck.size(); i ++ ) m[deck[i]] ++;
int cnt = m[deck[0]];
for (auto i = m.begin(); i != m.end(); i ++ ) {
int k = i -> second;
int x = gcd(cnt, k);
cnt = x; // 更新cnt以求解n个数的最大公约数
if (x < 2) return false;
}
return true;
}
};
题目链接:独特的电子邮件地址
C++版AC代码:
class Solution {
public:
int numUniqueEmails(vector<string>& emails) {
unordered_set<string> s;
for (int i = 0; i < emails.size(); i ++ ) {
string email = emails[i];
string realemail = "";
int j;
for (j = 0; j < email.size() && email[j] != '@'; j ++ ) { // 仅遍历本地名
if (email[j] == '.') continue;
else if (email[j] == '+') break;
else realemail += email[j];
}
for (; email[j] != '@' && j < email.size(); j ++ ); // 找到域名起始位置
realemail += email.substr(j, email.size() - j);
s.insert(realemail);
}
return s.size();
}
};