当前位置: 首页 > news >正文

24年秋季快手笔试题

266. 有效括号

链接:https://kamacoder.com/problempage.php?pid=1346

思想:这题算是签到题,采用一个栈,遍历这个字符串遇到左括号就直接入栈;遇到右括号,会有三种情况:

  1. 情况一:当前栈为空,说明没有与之匹配的左括号,说明当前括号不是有效的,则记录False

  2. 情况二:当前栈不为空,但栈顶部的左括号与当前的右括号不能匹配,说明当前括号不是有效的,则记录False

  3. 情况三:当前栈不为空,但栈顶部的左括号与当前的右括号能匹配,说明当前括号是有效的,则将栈顶的左括号弹出并扫描到下一个括号继续判断

题目里面可以先直接判断这个字符串的长度,如果不是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;
}
http://www.dtcms.com/a/263980.html

相关文章:

  • 哈尔滨服务器租用idc服务商-青蛙云
  • JVM调优实战 Day 15:云原生环境下的JVM配置
  • 【Axure视频教程】大小图轮播
  • 3D 商品展示与 AR 试戴能为珠宝行业带来一些便利?
  • 修改Spatial-MLLM项目,使其专注于无人机航拍视频的空间理解
  • C语言复习:数组和指针
  • 数据结构day5——队列和树
  • 转录组分析流程(三):功能富集分析
  • HakcMyVM-Arroutada
  • Rust 学习笔记:比较数值
  • Prompt生成指南
  • 数据结构与算法--蛇行矩阵问题
  • WPF学习笔记(17)样式Style
  • 【机器学习2】正则化regularizaiton(降低模型过拟合)
  • Http、Ftp、Dns和Dhcp服务器搭建
  • Go 服务如何“主动”通知用户?SSE广播与断线重连实战
  • 从docker-compose快速入门Docker
  • VCenter SSL过期,登录提示HTTP 500错误解决办法
  • Linux驱动学习day13(同步与互斥)
  • 记录一次生产环境ActiveMQ无法启动的问题
  • 变幻莫测:CoreData 中 Transformable 类型面面俱到(八)
  • Raspberry Pi 4边缘智能PLC:OpenPLC赋能物联网
  • 25-7-1 论文学习(1)- Fractal Generative Models 何恺明大佬的论文
  • 半导体和PN结
  • 遥感影像岩性分类:基于CNN与CNN-EL集成学习的深度学习方法
  • 胖喵安初 (azi) Android 应用初始化库 (类似 Termux)
  • Adobe AI高效设计技巧与创新思维指南
  • day41简单CNN
  • 注意力得分矩阵求解例子
  • 网站崩溃的幕后黑手:GPTBot爬虫的流量冲击