面试准备算法

devtools/2024/9/24 18:34:18/

找出数组的最大公约数

在这里插入图片描述

class Solution {
public:int findGCD(vector<int>& nums) {int min_num = *min_element(nums.begin(), nums.end());int max_num = *max_element(nums.begin(), nums.end());return gcd(min_num, max_num);}
};
//gcd()函数的用法是包含头文件#include <numeric>
int gcd(int a, int b){return b == 0 ? a : gcd(b, a%b);
}

在链表中插入最大公约数

/*** 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:int gcd(int a,int b){return b == 0 ? a : gcd(b, a%b);}ListNode* insertGreatestCommonDivisors(ListNode* head) {if(head == nullptr || head->next == nullptr){return head;}ListNode*p1 = head;ListNode*p2 = head->next;while(p2){int val = gcd(p1->val, p2->val);ListNode* newNode = new ListNode(val, p2);p1->next = newNode;p1 = p2;p2 = p2->next;}return head;}
};

使子数组最大公约数大于一的最小分割数

在这里插入图片描述
在这里插入图片描述
贪心+数学
对于数组中的每个元素,如果它与前面的元素最大公约数为1,那么它需要作为新的子数组的第一个元素。否则,它可以与前面的元素放在同一个子数组中。

因此,我们先初始化一个变量g,表示当前子数组的最大公约数。初始时,g为0,答案变量ans=1。

从前向后遍历数组,维护当前子数组的最大公约数g。如果当前元素x与g的最大公约数为1,那么我们需要将当前元素作为一个新的子数组的第一个元素。

class Solution{
public:int minimumSplits(vector<int>& nums){int ans = 1, g = 0;for(int num: nums){g = gcd(g, num);if(g == 1){++ans;g = num;}}return ans;}
};

求最大公约数通用公式

int gcd(int a, int b){return b == 0 ? a : gcd(b, a%b); 
}

查看Linux磁盘用量常用命令

df -h
du -h

最长连续序列

在这里插入图片描述

class Solution {
public:int longestConsecutive(vector<int>& nums) {int ret = 0;unordered_set<int> num_set;for(int num : nums){num_set.insert(num);}for(int num : nums){//前一个没有if(!num_set.count(num-1)){int currentNum = num;int len = 1;while(num_set.count(currentNum + 1)){currentNum++;len++;}ret = max(ret, len);}}return ret;}
};

哈希表
枚举数组中的每个数x,考虑以x为起点,不断匹配x+1,x+2,…是否存在,假设最长匹配到x+y,那么长度为y+1.

和为K的子数组

在这里插入图片描述
枚举超时

前缀和+哈希表优化
定义pre[i]为[0…i]里所有数的和,则pre[i]由pre[i-1]递推而来

pre[i] = pre[i-1] + nums[i]

那么[j…i]这个子数组和为k可以转换为

pre[i] - pre[j-1] == k

移动一下

pre[j-1] = pre[i] - k

所以考虑以i结尾的和为k的连续子数组个数只需要统计有多少个前缀和pre[i] - k的pre[j]即可。

class Solution{int subarraySum(vector<int>& nums, int k){unordered_map<int, int>mp;mp[0] = 1; //和为0的情况出现了一次int count = 0, pre = 0;for(auto&x : nums){pre += x;if(mp.find(pre-k) != mp.end()){count += mp[pre-k];}mp[pre]++;}return count;}
}

前缀和是指第一个元素到当前元素的所有元素之和,哈希表用于存储前缀和出现次数。

count记录和为k的连续子数组的个数。

旋转图像

class Solution {
public:void rotate(vector<vector<int>>& matrix) {int n = matrix.size();//转置for(int i=0; i<n; i++){for(int j=i+1; j<n; j++){swap(matrix[i][j], matrix[j][i]);}}//两列交换 0 2 ---  0 3 1 2int midJ = n/2;for(int j=0; j<midJ; j++){for(int i=0; i<n; i++){swap(matrix[i][j], matrix[i][n-j-1]);}}}
};

搜索二维矩阵

暴力

class Solution {
public:bool searchMatrix(vector<vector<int>>& matrix, int target) {int m = matrix.size(), n = matrix[0].size();for(int i=0; i<m; i++){for(int j=0; j<n; j++){if(matrix[i][j] == target){return true;}}}return false;}
};

z字形查找

class Solution {
public:bool searchMatrix(vector<vector<int>>& matrix, int target) {int m = matrix.size(), n = matrix[0].size();int x = 0, y = n-1;while(x < m && y >= 0){if(matrix[x][y] == target){return true;}else if(matrix[x][y] > target){y--;}else if(matrix[x][y] < target){x++;}}return false;}
};

反转链表

/*** 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* reverseList(ListNode* head) {if(head == nullptr || head->next == nullptr){return head;}ListNode* pre = nullptr;ListNode* cur = head;while(cur){ListNode* next = cur->next;cur->next = pre;pre = cur;cur = next;}return pre;}
};

和为K的子数组

给一个整数数组nums和一个整数k,统计并返回数组中和为k的子数组的个数。

在这里插入图片描述
前缀和+哈希表优化
定义pre[i]为[0,…,i]里所有数的和,则pre[i] = pre[i-1] + nums[i]

[j…,i]子数组和为k可以转换为pre[i] - pre[j-1] == k

转换为pre[j-1] = pre[i] - k
所以我们考虑以i结尾的和为k的连续子数组的个数时,只需要统计有多少个前缀和为pre[i] - k的pre[j]即可。

class Solution {
public:int subarraySum(vector<int>& nums, int k) {unordered_map<int, int> mp; //<pre, count>mp[0] = 1;int count = 0, pre = 0;for(int num : nums){pre += num;if(mp.count(pre - k)){count += mp[pre-k];}mp[pre]++;}return count;}
};

回文链表

在这里插入图片描述

/*** 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:bool isPalindrome(ListNode* head) {vector<int> nums;while(head){nums.push_back(head->val);head = head->next;}int left = 0, right = nums.size() - 1;while(left < right){if(nums[left] != nums[right]){return false;}left++;right--;}return true;}
};

环形链表

/*** Definition for singly-linked list.* struct ListNode {*     int val;*     ListNode *next;*     ListNode(int x) : val(x), next(NULL) {}* };*/
class Solution {
public:bool hasCycle(ListNode *head) {if(head == nullptr || head->next == nullptr){return false;}ListNode* slow = head;ListNode* fast = head->next;while(fast){if(slow == fast){return true;}`在这里插入代码片`if(fast->next != nullptr){fast = fast->next->next;}else{fast = nullptr;}slow = slow->next;}return false;}
};

二叉树的最大深度

二叉树的最大深度是指从根节点到最远叶子节点的最长路径上的节点数。

/*** 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 maxDepth(TreeNode* root) {if(root == nullptr){return 0;}int lDepth = maxDepth(root->left);int rDepth = maxDepth(root->right);return max(lDepth, rDepth)+1;}
};

http://www.ppmy.cn/devtools/98722.html

相关文章

LLM基础知识备忘录

预训练相关 LearningRate和BatchSize之间的关系 假设batch_size足够小&#xff0c;等于1&#xff0c;如果lr设的比较大则容易导致loss发散&#xff1b;假设batch_size足够大&#xff0c;等于全量的数据&#xff0c;如果这个时候lr设的小则收敛会很慢。所以理论上来说&#xff…

redis支持的数据结构

redis支持多种数据结构&#xff0c;这些数据结构可以满足各种用途&#xff0c;包括缓存&#xff0c;计数&#xff0c;排序&#xff0c;消息队列等等 Redis支持以下数据结构&#xff1a; 字符串&#xff08;String&#xff09;&#xff1a;字符串是最简单的数据结构&#xff0c…

Python爬虫APP程序思维逻辑(附带源码)

请注意&#xff0c;这个示例是假设性的&#xff0c;并不代表任何真实网站或API。在实际使用中&#xff0c;你需要根据目标网站的具体结构来调整代码。 环境准备 首先&#xff0c;确保你已经安装了requests和BeautifulSoup。如果没有安装&#xff0c;可以通过以下命令安装&…

QPushbutton checked状态下文字显示不全

1&#xff0c;同时设置粗体、单边框样式会导致QPushbutton 在checked状态下&#xff0c;文字显示不全 类似下面的QSS QPushButton:checked {font-weight: bold;border: none;border-bottom: 2px solid #00A38B; }应该是Qt本身的问题&#xff0c;checked状态下button的文字会偏…

融合通信平台的视频可以在哪些设备上看?

伴随着互联网和移动通信技术的发展&#xff0c;融合通信从上个世纪九十年代兴起并发展至今&#xff0c;融合通信已经从音频融合发展到视频融合阶段&#xff0c;通过融合通信平台对各种视频进行高效的融合&#xff0c;可以发挥出融合通信在视频调度方面的通信能力。 近年来&…

HashMap-leetcode总结

为什么用Hashmap? 将两种属性&#xff08;key,value&#xff09;具有某种联系&#xff0c;需要保存下来 随时读取是否存在且通过一方获取它对应值 数据结构 一数值value经过hashcode()计算出key&#xff0c;key对应数组位置建立链表 HashMap常用方法 1、HashMap的初始化 Hash…

几种防止Spring Boot 程序崩溃的方法

在 Spring Boot 应用程序中&#xff0c;预防程序崩溃并确保应用的稳定性可以通过以下几种方式来实现&#xff1a; 1. 全局异常处理 使用 Spring 的 ControllerAdvice 和 ExceptionHandler 注解&#xff0c;处理所有未捕获的异常&#xff0c;防止异常直接导致程序崩溃。 Cont…

新手如何找到正确入行 Web3 路径?揭开职业启航新篇章

&#x1f3c4; Web3 新晋开发者如何找到心仪的工作&#xff1f;除了加强自身技术本领&#xff0c;开发创新优质项目以外&#xff0c;拓展社会人脉、接触行业资源同样重要。与此同时&#xff0c;风云变幻的 Web3 行业环境中&#xff0c;我们又该如何寻找优质潜力的项目生态实现深…