24年秋季快手笔试题
266. 有效括号
链接:https://kamacoder.com/problempage.php?pid=1346
思想:这题算是签到题,采用一个栈,遍历这个字符串遇到左括号就直接入栈;遇到右括号,会有三种情况:
-
情况一:当前栈为空,说明没有与之匹配的左括号,说明当前括号不是有效的,则记录False
-
情况二:当前栈不为空,但栈顶部的左括号与当前的右括号不能匹配,说明当前括号不是有效的,则记录False
-
情况三:当前栈不为空,但栈顶部的左括号与当前的右括号能匹配,说明当前括号是有效的,则将栈顶的左括号弹出并扫描到下一个括号继续判断
题目里面可以先直接判断这个字符串的长度,如果不是2的整数倍的话,那么一定不是有效的括号,直接记录为False,并跳到下一行。
#include <iostream>
#include <stack>
#include <string>
#include <unordered_map>using namespace std;int main()
{unordered_map<char, char> mapping = {{')', '('}, {']', '['}, {'}', '{'}};int N;cin >> N;cin.ignore(); // 忽略换行符while(N--) {string str;stack<char> st;getline(cin, str);bool ans = true;// 可加判断,如果str的长度不为整数倍的话,那么直接记录为falsefor (auto c : str){if (mapping.count(c)){if (st.empty()){ans = false;break;}char top_element = st.top();st.pop();if (mapping[c] != top_element){ans = false;break;}}else {st.push(c);}}if (ans){if (st.empty()){cout << str.length() / 2 << endl;}else{cout << "False" << endl;}}else {cout << "False" << endl;}}return 0;
}
267. 最长的滑道
链接:https://kamacoder.com/problempage.php?pid=1347
思想:这题很明显是一个DFS的问题,在一个点开始寻找旁边四个比其高度小的点,并将其与当前的result进行对比res = max(res, dfs());
暴力搜索代码
以下是暴力搜索的代码,时间复杂度为O(n2),空间复杂度为O(n2)。
但这样会超出代码的时间限制,因为在进行搜索的时候并没有对重复搜索的路径进行排除。
例如:如果从第一个点开始到这个矩阵的最后一个点搜索到了矩阵的尺寸大小长度的滑道,那么后面的点都没有必要进行搜索了,因为已经是最大了。同理,如果这个点已经被包围在已经遍历过点的滑道里面,那么这个点能拥有的最大滑道是已经遍历过的滑道,所以没有必要再进行搜索了。
#include <iostream>
#include <vector>using namespace std;int dfs(int i, int j, vector<vector<int>> map, int res)
{res++;int temp = res;if (i - 1 >= 0 && map[i][j] > map[i-1][j]){res = max(res, dfs(i - 1, j, map, temp));}if (i + 1 < map.size() && map[i][j] > map[i+1][j]){res = max(res, dfs(i + 1, j, map, temp));}if (j - 1 >= 0 && map[i][j] > map[i][j-1]){res = max(res, dfs(i, j - 1, map, temp));}if (j + 1 < map[0].size() && map[i][j] > map[i][j+1]){res = max(res, dfs(i, j + 1, map, temp));}return res;
}int main()
{int R, C;cin >> R >> C;vector<vector<int>> hMap(R, vector<int> (C, 0));for (int i = 0; i < R; ++i) {for (int j = 0; j < C; ++j){int a;cin >> a;hMap[i][j] = a;}}int result = 0;for (int i = 0; i < R; ++i){for (int j = 0; j < C; ++j){int a = dfs(i, j, hMap, 0);result = result > a ? result : a;}}cout << result << endl;return 0;
}
优化
思路:将已经有了最大滑道的点进行标记,如果在下次遍历的时候遇见了,那么就直接返回以当前点为起点的最大滑道的长度,如果没有,就进行下一次的dfs。这是修改过后的代码。
#include <iostream>
#include <vector>
#include <algorithm>using namespace std;int R, C;
vector<vector<int>> hMap;
vector<vector<int>> maxPath;
int directions[4][2] = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};int dfs(int i, int j)
{if (maxPath[i][j] != -1) return maxPath[i][j];int res = 1;for (int k = 0; k < 4; ++k){int di = i + directions[k][0];int dj = j + directions[k][1];if (di >= 0 && di < R && dj >= 0 && dj < C && hMap[i][j] > hMap[di][dj]){res = max(res, 1 + dfs(di, dj));}}maxPath[i][j] = res;return res;
}int main()
{cin >> R >> C;hMap.resize(R, vector<int>(C));for (int i = 0; i < R; ++i) {for (int j = 0; j < C; ++j){cin >> hMap[i][j];}}maxPath.assign(R, vector<int>(C, -1));int result = 0;for (int i = 0; i < R; ++i){for (int j = 0; j < C; ++j){result = max(result, dfs(i, j));}}cout << result << endl;return 0;
}