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

C++每日刷题day2025.7.13

思路:这个是迷宫的变种,我们判断条件是当往与当前方向不同的其他几个方向遍历的时候是需要将这个方向进行改变的,最主要的是先右再下再左再上这种。

class Solution {const int change[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};
public:vector<int> spiralOrder(vector<vector<int>>& matrix) {if (matrix.size() == 0 || matrix[0].size() == 0) {return {};}int rows = matrix.size();int cols = matrix[0].size();int total = rows*cols;vector<vector<bool>> visited(rows, vector<bool>(cols));vector<int> ret(total);int row = 0;int col = 0;int directindex = 0;for (int i = 0; i < total; ++i){ret[i] = matrix[row][col];visited[row][col] = true;int nextrow = row + change[directindex][0];int nextcol = col + change[directindex][1];if (nextrow < 0 || nextrow >= rows || nextcol <0||nextcol>=cols || visited[nextrow][nextcol]){directindex = (directindex + 1) %4;}row += change[directindex][0];col += change[directindex][1];}return ret;}
};

滑动窗口+哈希表,右指针遍历碰到hash值大于1的时候就将左指针往右走。

class Solution {
public:int lengthOfLongestSubstring(string s) {int hash[128] = {0};int right = 0;int left = 0;int ret = 0;while (right < s.size()){hash[s[right]]++;while(hash[s[right]] > 1){hash[s[left++]]--;}ret = max(ret, right - left + 1);right ++;}return ret;}
};

斐波那契数列的判断,我们只需要利用abc这三个数滚动构造斐波那契数列,让当前输入的n这个数位于bc之间,到b或者到c的距离哪个小就返回哪个。

#include <iostream>
using namespace std;int main() {int a = 1;int b = 1;int c = 1;int ret = 0;int n;cin >> n;while (n > c){a = b;b = c;c = a + b;}ret = min(abs(c-n), abs(b-n));cout << ret;
}

http://www.lryc.cn/news/586924.html

相关文章:

  • 查看ubuntu磁盘占用方法
  • 日记-生活随想
  • 单例模式:确保全局唯一实例
  • 芯片相关必备
  • 第三章-提示词-解锁Prompt提示词工程核销逻辑,开启高效AI交互(10/36)
  • 如何成为 PostgreSQL 中级专家
  • 图形处理算法分类、应用场景及技术解析
  • Web应用性能优化之数据库查询实战指南
  • C/C++数据结构之多维数组
  • MySQL实操:将Word表格数据导入MySQL表
  • 导入 SciPy 的 io 模块
  • 基于Springboot+UniApp+Ai实现模拟面试小工具三:后端项目基础框架搭建上
  • 在人工智能自动化编程时代:AI驱动开发和传统软件开发的分析对比
  • ECU(电子控制单元)是什么?
  • Hashtable 与 HashMap 的区别笔记
  • LeetCode|Day9|976. 三角形的最大周长|Python刷题笔记
  • 代码部落 20250629 CSP-S复赛 模拟赛
  • 代码随想录算法训练营第十八天
  • 攻防世界——Web题 very_easy_sql
  • 解析磁盘文件系统
  • 面试150 从中序与后序遍历构造二叉树
  • 手写std::optional:告别空指针的痛苦
  • HTTP与HTTPS详解
  • 20250713 保存 PGM / PPM 图片 C++
  • COZE token刷新
  • 一文读懂现代卷积神经网络—使用块的网络(VGG)
  • 2025江苏省信息安全管理与评估赛项二三阶段任务书
  • 改进后的 OpenCV 5.x + GStreamer + Python 3.12 编译流程(适用于 Orange Pi / ARM64)
  • 3.7 ASPICE的问题解决与改进过程
  • Linux-网络管理