💫你好,我是辰chen,本文旨在准备考研复试或就业
💫文章题目大多来自于 leetcode,当然也可能来自洛谷或其他刷题平台
💫欢迎大家的关注,我的博客主要关注于考研408以及AIoT的内容
🌟 仅给出C++版代码
以下的几个专栏是本人比较满意的专栏(大部分专栏仍在持续更新),欢迎大家的关注:
💥ACM-ICPC算法汇总【基础篇】
💥ACM-ICPC算法汇总【提高篇】
💥AIoT(人工智能+物联网)
💥考研
💥CSP认证考试历年题解
题目链接:字符串中第二大的数字
C++版AC代码:
class Solution {
public:
int a[10];
int secondHighest(string s) {
for (auto x : s)
if (x >= '0' && x <= '9')
a[x - '0'] = 1;
int cnt = 0, res = -1;
for (int i = 9; i >= 0; -- i ) {
if (!cnt && a[i]) cnt ++;
else if (cnt && a[i]) {
res = i;
break;
}
}
return res;
}
};
题目链接:字符串中不同整数的数目
C++版AC代码:
class Solution {
public:
int numDifferentIntegers(string word) {
unordered_set<string> s;
for (int i = 0; i < word.size(); i ++ ) {
if (isdigit(word[i])) {
if (i == word.size() - 1 ) { // 特判最后一个字母
s.insert(string(1, word[i]));
continue;
}
if (word[i] == '0' && isdigit(word[i + 1])) continue;
int j = i;
while (j < word.size() && isdigit(word[j]))
j ++;
string num = word.substr(i, j - i);
i = j - 1;
s.insert(num);
}
}
return s.size();
}
};
题目链接:判断句子是否为全字母句
C++版AC代码:
class Solution {
public:
bool checkIfPangram(string sentence) {
if (sentence.size() < 26) return false;
unordered_set<char> s;
for (auto c : sentence) s.insert(c);
for (char c = 'a'; c <= 'z'; ++ c )
if (s.find(c) == s.end())
return false;
return true;
}
};
题目链接:长度为三且各字符不同的子字符串
C++版AC代码:
class Solution {
public:
int countGoodSubstrings(string s) {
if (s.size() < 3) return 0;
int res = 0;
for (int i = 0; i < s.size() - 2; i ++ ) {
if (s[i] != s[i + 1] && s[i + 1] != s[i + 2] && s[i] != s[i + 2])
res ++;
}
return res;
}
};
题目链接:检查是否区域内所有整数都被覆盖
C++版AC代码:
class Solution {
public:
bool isCovered(vector<vector<int>>& ranges, int left, int right) {
unordered_set<int> s;
for (auto range : ranges)
for (int i = range[0]; i <= range[1]; i ++ )
s.insert(i);
for (int i = left; i <= right; i ++ )
if (s.find(i) == s.end())
return false;
return true;
}
};
C++版AC代码:
差分的思维解题:
class Solution {
public:
int ad[55];
bool isCovered(vector<vector<int>>& ranges, int left, int right) {
for (auto range : ranges) {
int st = range[0], ed = range[1];
ad[st] ++, ad[ed + 1] --; // 差分
}
for (int i = 1; i <= 50; ++ i ) ad[i] += ad[i - 1]; // 前缀和
for (int i = left; i <= right; ++ i )
if (!ad[i]) return false;
return true;
}
};
题目链接:重新分配字符使所有字符串都相等
C++版AC代码:
class Solution {
public:
bool makeEqual(vector<string>& words) {
unordered_map<char, int> m;
for (auto word : words)
for (auto c : word)
m[c] ++;
int n = words.size();
for (auto it : m)
if (it.second % n)
return false;
return true;
}
};
题目链接:可以输入的最大单词数
C++版AC代码:
class Solution {
public:
int canBeTypedWords(string text, string brokenLetters) {
unordered_set<char> s;
for (auto c : brokenLetters) s.insert(c);
int res = 0;
for (int i = 0; i < text.size(); i ++ ) {
int j = i;
while (j < text.size() && text[j] != ' ') j ++;
string word = text.substr(i, j - i);
i = j;
for (auto c : word)
if (s.find(c) != s.end()) {
res --;
break;
}
res ++;
}
return res;
}
};
题目链接:检查是否所有字符出现次数相同
C++版AC代码:
class Solution {
public:
bool areOccurrencesEqual(string s) {
unordered_map<char, int> m;
for (auto c : s) m[c] ++;
int num = m[s[0]];
for (auto it : m)
if (it.second != num)
return false;
return true;
}
};
题目链接:差的绝对值为 K 的数对数目
C++版AC代码:
class Solution {
public:
int countKDifference(vector<int>& nums, int k) {
unordered_map<int, int> m;
for (auto k : nums) m[k] ++;
// a - b = k, b - a = k
// b = a - k, b = a + k
int res = 0;
for (auto num : nums) {
int num1 = num - k, num2 = num + k;
if (m.count(num1)) res += m[num1];
if (m.count(num2)) res += m[num2];
m[num] -= 1; // 保证题干中的 i < j
}
return res;
}
};
题目链接:至少在两个数组中出现的值
C++版AC代码:
💩山代码,着急去玩,就这样不改了😁
class Solution {
public:
vector<int> twoOutOfThree(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3) {
// 预处理nums1, nums2, nums3,使其内部元素唯一
// 处理nums1
unordered_set<int> unique1(nums1.begin(), nums1.end());
nums1.assign(unique1.begin(), unique1.end());
// 处理nums2
unordered_set<int> unique2(nums2.begin(), nums2.end());
nums2.assign(unique2.begin(), unique2.end());
// 处理nums3
unordered_set<int> unique3(nums3.begin(), nums3.end());
nums3.assign(unique3.begin(), unique3.end());
unordered_set<int> s;
unordered_set<int> tmpres;
for (auto num : nums1) s.insert(num);
for (auto num : nums2) {
if (s.find(num) != s.end())
tmpres.insert(num);
else s.insert(num);
}
for (auto num : nums3) {
if (s.find(num) != s.end())
tmpres.insert(num);
else s.insert(num);
}
vector<int> res(tmpres.begin(), tmpres.end());
return res;
}
};