💫你好,我是辰chen,本文旨在准备考研复试或就业
💫本文内容来自某机构网课,是我为复试准备的第一个项目
💫欢迎大家的关注,我的博客主要关注于考研408以及AIoT的内容
🌟 预置知识详见我的AIoT板块,需掌握 基本Python语法, Numpy, Pandas, Matplotlib
以下的几个专栏是本人比较满意的专栏(大部分专栏仍在持续更新),欢迎大家的关注:
💥ACM-ICPC算法汇总【基础篇】
💥ACM-ICPC算法汇总【提高篇】
💥AIoT(人工智能+物联网)
💥考研
💥CSP认证考试历年题解
题目链接:查找包含给定字符的单词
C++版AC代码:
class Solution {
public:
vector<int> findWordsContaining(vector<string>& words, char x) {
vector<int> res;
for (int i = 0; i < words.size(); ++ i)
for (int j = 0; j < words[i].size(); ++ j)
if (words[i][j] == x) {
res.push_back(i);
break;
}
return res;
}
};
Python版AC代码:
class Solution:
def findWordsContaining(self, words: List[str], x: str) -> List[int]:
ans = []
for i, word in enumerate(words):
if x in word:
ans.append(i)
return ans
题目链接:计算列车到站时间
C++版AC代码:
class Solution {
public:
int findDelayedArrivalTime(int arrivalTime, int delayedTime) {
return (arrivalTime + delayedTime) % 24;
}
};
Python版AC代码:
class Solution:
def findDelayedArrivalTime(self, arrivalTime: int, delayedTime: int) -> int:
return (arrivalTime + delayedTime) % 24
题目链接:数组中字符串的最大值
C++版AC代码:
class Solution {
public:
int max(int a, int b) {
if (a >= b) return a;
return b;
}
int maximumValue(vector<string>& strs) {
int res = 0;
for (auto str : strs) {
bool flag = true;
for (auto c : str) {
if (!isdigit(c)) {
flag = false;
break;
}
}
// stoi : string to int
if (flag) res = max(res, stoi(str)); // stoi(str) 可以把 string 类型转为 int 类型
else res = max(res, str.size());
}
return res;
}
};
Python版AC代码:
class Solution:
def maximumValue(self, strs: List[str]) -> int:
res = 0
for str in strs:
if str.isdigit():
res = max(res, int(str))
else:
res = max(res, len(str))
return res
题目链接:统计和小于目标的下标对数目
C++版AC代码:
class Solution {
public:
int countPairs(vector<int>& nums, int target) {
int res = 0;
for (int i = 0; i < nums.size(); i ++ )
for (int j = i + 1; j < nums.size(); j ++ )
if (nums[i] + nums[j] < target)
res ++;
return res;
}
};
C++版AC代码:
双指针,题中要求的 i < j
其实是指选择两个不同位置的数,因此可以进行排序。
class Solution {
public:
int countPairs(vector<int>& nums, int target) {
sort(nums.begin(), nums.end());
int res = 0;
for (int i = 0, j = nums.size() - 1; i < j; i ++ ) {
while (i < j && nums[i] + nums[j] >= target) j --;
res += j - i;
}
return res;
}
};
Python版AC代码:
class Solution:
def countPairs(self, nums: List[int], target: int) -> int:
nums.sort()
res = 0
i, j = 0, len(nums) - 1
while i < j :
while i < j and nums[i] + nums[j] >= target:
j -= 1
res += j - i
i += 1
return res
题目链接:分类求和并作差
C++版AC代码:
class Solution {
public:
int differenceOfSums(int n, int m) {
int canm = 0, uncanm = 0;
for (int i = 1; i <= n; i ++ ) {
if (i % m) uncanm += i;
else canm += i;
}
return uncanm - canm;
}
};
Python版AC代码:
class Solution:
def differenceOfSums(self, n: int, m: int) -> int:
canm, uncanm = 0, 0
for i in range(n + 1):
if i % m:
uncanm += i
else:
canm += i
return uncanm - canm
题目链接:统计相似字符串对的数目
C++版AC代码:
哈希表+位运算(压缩映射),用 int
的低 26位表示26个字母,对于 |
,只有 0|0=0
class Solution {
public:
int similarPairs(vector<string>& words) {
unordered_map<int, int> m;
for (auto word : words) {
int num = 0;
for (auto c : word) num |= 1 << (c - 'a');
m[num] ++;
}
int res = 0;
for (auto it : m) {
int n = it.second;
res += n * (n - 1) / 2;
}
return res;
}
};
Python版AC代码:
class Solution:
def similarPairs(self, words: List[str]) -> int:
res, m = 0, Counter()
for word in words:
num = 0
for c in word:
num |= 1 << (ord(c) - ord('a')) # ord返回ASCII
res += m[num]
m[num] += 1
return res
题目链接:删除每行中的最大值
C++版AC代码:
class Solution {
public:
static bool cmp(int a, int b) {
return a > b;
}
int getmax(vector<vector<int>>& grid, int k) {
int maxnum = grid[0][k];
for (int i = 1; i < grid.size(); ++ i )
maxnum = max(maxnum, grid[i][k]);
return maxnum;
}
int deleteGreatestValue(vector<vector<int>>& grid) {
for (int i = 0; i < grid.size(); ++ i )
sort(grid[i].begin(), grid[i].end(), cmp);
int n = grid[0].size(), res = 0;
for (int i = 0; i < n; ++ i ) {
res += getmax(grid, i);
}
return res;
}
};
Python版AC代码:
class Solution:
def deleteGreatestValue(self, grid: List[List[int]]) -> int:
# 按照升序对每一行排序
sort_grid = [sorted(row) for row in grid]
# 在每一列中找到最大值
maxnum = [max(col) for col in zip(*sort_grid)]
# 计算最大值的和
return sum(maxnum)