测试常见的八股文
这篇文章总结了一下我之前学习软件测试面试的一些算题。 文章主要包含Leecode一些简单和中等难度的题。
1. 每日温度
Leecode链接: 739.每日温度
class Solution {
public:
vector<int> dailyTemperatures(vector<int>& temperatures) {
int n = temperatures.size();
vector<int> res(n, 0);
stack<int> stk;
for (int i = n - 1; i >= 0; i -- )
{
while(stk.size() && temperatures[stk.top()] <= temperatures[i]) stk.pop();
if (stk.empty()) res[i] = 0;
else res[i] = stk.top() - i;
stk.push(i);
}
return res;
}
};
2. 二叉树的最小深度
Leecode链接: 111.二叉树的最小深度
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int minDepth(TreeNode* root) {
// 最小深度考虑用dfs
if (root == nullptr) return 0;
queue<TreeNode*> q;
q.push(root);
int depth = 1;
while (!q.empty())
{
int sz = q.size();
for (int i = 0; i < sz; i ++ )
{
auto t = q.front();
q.pop();
// 判断一下bfs的终止条件
if (t -> left == nullptr && t -> right == nullptr)
return depth;
if (t -> left != nullptr) q.push(t -> left);
if (t -> right != nullptr) q.push(t -> right);
}
depth ++;
}
return depth;
}
};
3. 有效的括号
Leecode链接: 20.有效的括号
class Solution {
public:
bool isValid(string s) {
stack<char> stk;
for (auto a : s)
{
if (a == '(') stk.push(')');
else if (a == '[') stk.push(']');
else if (a == '{') stk.push('}');
// stk 匹配完拿走
else if (stk.empty() || stk.top() != a) return false;
// 说明此时合法,要将此时的stack顶元素拿走
else stk.pop();
}
// 最后判断一下stack是否为空
return stk.empty();
}
};
4. 删除排序链表中的重复元素
Leecode链接: 83.删除排序链表中的重复元素
/**
* Definition for singly-linked list.
* struct ListNode {
* int val;
* ListNode *next;
* ListNode() : val(0), next(nullptr) {}
* ListNode(int x) : val(x), next(nullptr) {}
* ListNode(int x, ListNode *next) : val(x), next(next) {}
* };
*/
class Solution {
public:
ListNode* deleteDuplicates(ListNode* head) {
auto p = head;
while (p != nullptr && p -> next != nullptr)
{
if (p -> val == p -> next -> val)
{
// 直接用一个新的指针指向需要被删除的结点
ListNode* temp = p -> next;
p -> next = p -> next -> next;
delete temp;
}
else
{
p = p -> next;
}
}
return head;
return nullptr;
}
};
5. 最长回文子串
Leecode链接: 5. 最长回文子串
class Solution {
public:
string longestPalindrome(string s) {
// 存储一下最后要输出的串
int n = s.size();
string res;
// 枚举一下中心的位置
for (int i = 0; i < n; i ++ )
{
string s1 = Palindrome(s, i, i);
string s2 = Palindrome(s, i, i + 1);
res = res.size() > s1.size() ? res:s1;
res = res.size() > s2.size() ? res:s2;
}
return res;
}
// 优美的地方在于这个算法他会回退
string Palindrome(string& s, int l, int r)
{
while(l >= 0 && r < s.size() && s[l] == s[r])
{
l --;
r ++;
}
return s.substr(l + 1, r - 1 - l);
}
};
6. 回文数
Leecode链接: 9. 回文数
class Solution {
public:
bool isPalindrome(int x) {
if (x < 0) return false;
vector<int> x_vec;
while(x > 0)
{
x_vec.push_back(x % 10);
x /= 10;
}
// for (auto a : x_vec)
// cout << a << endl;
int i = 0, j = x_vec.size() - 1;
while(i < j)
{
if (x_vec[i ++] != x_vec[j --]) return false;
}
return true;
}
};
7. 跳跃游戏
Leecode链接: 55. 跳跃游戏
class Solution {
public:
bool canJump(vector<int>& nums) {
int n = nums.size();
if (n == 1) return true;
int max_dist = nums[0];
for (int i = 1; i < n; i ++ )
{
if (max_dist < i) return false;
else max_dist = max(max_dist, i + nums[i]);
}
return true;
}
};
8.颜色分类
Leecode链接: 75. 颜色分类
class Solution {
public:
void sortColors(vector<int>& nums) {
for (int i = 0, j = 0, k = nums.size() - 1; i <= k; )
{
if (nums[i] == 0) swap(nums[i ++], nums[j ++ ]);
else if (nums[i] == 2) swap(nums[i], nums[k --]);
else i ++;
}
}
};
9. 螺旋矩阵
Leecode链接: 54. 螺旋矩阵
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
vector<int> ans;
// 确立一下四个边界
int n = matrix.size();
int m = matrix[0].size();
if (n == 0 || m == 0) return ans;
int left = 0, right = m - 1, top = 0, down = n - 1;
while (true)
{
for (int i = left; i <= right; i ++ ) ans.push_back(matrix[top][i]);
// 判断一下是否终止
if (++ top > down) break;
for (int i = top; i <= down ; i ++ ) ans.push_back(matrix[i][right]);
if (--right < left) break;
for(int i = right; i >= left; i -- ) ans.push_back(matrix[down][i]);
if (-- down < top ) break;
for (int i = down; i >= top; i -- ) ans.push_back(matrix[i][left]);
if (++ left > right) break;
}
return ans;
}
};
10. 省份数量
Leecode链接: 547. 省份数量
class Solution {
public:
vector<int> p;
int findCircleNum(vector<vector<int>>& isConnected) {
int n = isConnected.size();
p = vector<int>(n, 0);
// 初始化一下我们并查集, 让所有的city
// 都指向自身
for (int i = 0; i < n; i ++ )
{
p[i] = i;
}
int cnt = n;
for (int i = 0; i < n; i ++ )
for (int j = 0; j < n; j ++ )
{
// 当i, j相连,且不在一个集合中
if (isConnected[i][j] && find(i) != find(j))
{
p[find(i)] = find(j);
cnt --;
}
}
return cnt;
}
int find(int x)
{
if (p[x] != x) p[x] = find(p[x]);
return p[x];
}
};