机试备考笔记11/31
2025年8月11日
小结:省流,ac 了 11 道简单题,啧,今天正好 11 号啊,那很巧了。超松弛的一天,啥也没干,就写了这点题。晚上收到梦校老师回复,周三上午一对一汇报 w(゚Д゚)w
(啧,题是11号写的,笔记是12号追加的
目录
- LeetCode
- 24. 两两交换链表中的节点
- 138. 随机链表的复制
- 20. 有效的括号
- 226. 翻转二叉树
- 169. 多数元素
- 136. 只出现一次的数字
- 461. 汉明距离
- 448. 找到所有数组中消失的数字
- 338. 比特位计数
- 121. 买卖股票的最佳时机
- 543. 二叉树的直径
- Acwing
- xxx
LeetCode
24. 两两交换链表中的节点
24. 两两交换链表中的节点
题目
给你一个链表,两两交换其中相邻的节点,并返回交换后链表的头节点。你必须在不修改节点内部的值的情况下完成本题(即,只能进行节点交换)。
秒了
class Solution {
public:void exchange(ListNode* pre, ListNode* l, ListNode* r) {l->next = r->next;r->next = l;pre->next = r;}ListNode* swapPairs(ListNode* head) {if (head == nullptr) return nullptr;else if (head->next == nullptr) return head;ListNode *tmp1 = head->next->next, *tmp2 = head->next;head->next->next = head;head->next = tmp1;head = tmp2;ListNode *pre = head->next, *l, *r;while (true) {l = pre->next;if (l == nullptr) break;r = l->next;if (r == nullptr) break;exchange(pre, l, r);pre = l;}return head;}
};
138. 随机链表的复制
138. 随机链表的复制
题干
对随机链表深拷贝
看到有人 python 直接 deepcopy 羡慕捏捏
写的好长好臭,卡了一下,都怪 random,会 new 还没 next 遇到的,别忘放 map 里
class Solution {
public:Node* copyRandomList(Node* head) {unordered_map<Node*, Node*> mp;Node *current = head, *recurrent;while (current != nullptr) {if (mp.count(current)) {recurrent = mp[current];} else {recurrent = new Node(current->val);mp[current] = recurrent;}if (current->next == nullptr){} else if (mp.count(current->next)) {recurrent->next = mp[current->next];} else {recurrent->next = new Node(current->next->val);mp[current->next] = recurrent->next;}if (current->random == nullptr){} else if (mp.count(current->random)) {recurrent->random = mp[current->random];} else {recurrent->random = new Node(current->random->val);mp[current->random] = recurrent->random;}current = current->next;}return mp[head];}
};
20. 有效的括号
20. 有效的括号
题目
给定一个只包括 ‘(’,‘)’,‘{’,‘}’,‘[’,‘]’ 的字符串 s ,判断字符串是否有效。
有效字符串需满足:1. 左括号必须用相同类型的右括号闭合。2. 左括号必须以正确的顺序闭合。3. 每个右括号都有一个对应的相同类型的左括号。
细节还是很耐人处理的,什么空啦,啥的
class Solution {
public:bool isValid(string s) {int slen = s.length();vector<char> stack;for (int i = 0; i < slen; i++) {if (s[i] == '(' || s[i] == '[' || s[i] == '{') {stack.push_back(s[i]);continue;}if (stack.empty()) return false;char tmp = stack.back();stack.pop_back();if (s[i] == ')') {if (tmp != '(') return false;} else if (s[i] == ']') {if (tmp != '[') return false;} else {if (tmp != '{') return false;}}if (stack.empty()) return true;return false;}
};
226. 翻转二叉树
226. 翻转二叉树
题目
给你一棵二叉树的根节点 root ,翻转这棵二叉树,并返回其根节点。
class Solution {
public:void solve(TreeNode *tmp) {if (tmp == nullptr) return;TreeNode *ex = tmp->right;tmp->right = tmp->left;tmp->left = ex;if (tmp->left != nullptr) solve(tmp->left);if (tmp->right != nullptr) solve(tmp->right);}TreeNode* invertTree(TreeNode* root) {solve(root);return root;}
};
官解好灵啊,不像我,(⊙o⊙) 我是把两个父结点翻了,官解是翻了子树再处理子树,差不多差不多
class Solution {
public:TreeNode* invertTree(TreeNode* root) {if (root == nullptr) {return nullptr;}TreeNode* left = invertTree(root->left);TreeNode* right = invertTree(root->right);root->left = right;root->right = left;return root;}
};作者:力扣官方题解
链接:https://leetcode.cn/problems/invert-binary-tree/solutions/415160/fan-zhuan-er-cha-shu-by-leetcode-solution/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
169. 多数元素
169. 多数元素
题目
给定一个大小为 n 的数组 nums ,返回其中的多数元素。多数元素是指在数组中出现次数 大于 ⌊ n/2 ⌋ 的元素。
哈希秒了
class Solution {
public:int majorityElement(vector<int>& nums) {unordered_map<int, int> num_cnt;int ans, max_cnt = 0;for (int num : nums) {if (++num_cnt[num] > max_cnt) {max_cnt = num_cnt[num];ans = num;}}return ans;}
};
136. 只出现一次的数字
136.只出现一次的数字
题目
给你一个 非空 整数数组 nums ,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。
异或运算太厉害了,利用了题目“其余元素均出现两次”,那两次异或就无效了
^
表示异或
class Solution {
public:int singleNumber(vector<int>& nums) {int ret = 0;for (int num : nums) ret ^= num;return ret;}
};
461. 汉明距离
461.汉明距离
题目
两个整数之间的 汉明距离 指的是这两个数字对应二进制位不同的位置的数目。
-z
是 z
取反+1(啧,那岂不是补码
z & (-z)
返回 z
二进制最后的1和0,比如 10100
=> 100
class Solution {
public:int solve(int z) {return z & (-z);}int hammingDistance(int x, int y) {int tmp = x ^ y, cnt = 0;while (tmp) {tmp -= (tmp & (-tmp));cnt += 1;}return cnt;}
};
448. 找到所有数组中消失的数字
448.找到所有数组中消失的数字
题目
给你一个含 n 个整数的数组 nums ,其中 nums[i] 在区间 [1, n] 内。请你找出所有在 [1, n] 范围内但没有出现在 nums 中的数字,并以数组的形式返回结果。
和你们这些甜菜拼了!!!利用原数组,大小正好,反正本身在 1~N,多加个 N 表示有这家伙
要把 1~N
映射到 0~N-1
class Solution {
public:vector<int> findDisappearedNumbers(vector<int>& nums) {int n = nums.size();for (int x : nums) {if (nums[(x - 1) % n] <= n) nums[(x - 1) % n] += n;}vector<int> ans;for (int i = 0; i < n; i++) {if (nums[i] <= n) ans.push_back(i + 1);}return ans;}
};
338. 比特位计数
338.比特位计数
题目
给你一个整数 n ,对于 0 <= i <= n 中的每个 i ,计算其二进制表示中 1 的个数 ,返回一个长度为 n + 1 的数组 ans 作为答案。
没技巧(通式),纯暴力啊
class Solution {
public:vector<int> countBits(int n) {vector<int> ans;for (int i = 0; i <= n; i++) {int tmp = i, cnt = 0;while (tmp) {tmp -= (tmp & (-tmp));cnt += 1;}ans.push_back(cnt);}return ans;}
};
121. 买卖股票的最佳时机
121. 买卖股票的最佳时机
题目
给定一个数组 prices ,它的第 i 个元素 prices[i] 表示一支给定股票第 i 天的价格。
你只能选择 某一天 买入这只股票,并选择在 未来的某一个不同的日子 卖出该股票。设计一个算法来计算你所能获取的最大利润。
返回你可以从这笔交易中获取的最大利润。如果你不能获取任何利润,返回 0 。
我光想着,前缀里存 min,后缀里存 max,倒是复杂了(见后后
class Solution {
public:int maxProfit(vector<int>& prices) {int N = prices.size();int up2nowmin[N], down2nowmax[N];up2nowmin[0] = prices[0], down2nowmax[N - 1] = prices[N - 1];for (int i = 1; i < N; i++) {up2nowmin[i] = min(up2nowmin[i - 1], prices[i]);// 1 - N-1int j = N - 1 - i;down2nowmax[j] = max(down2nowmax[j + 1], prices[j]);// N-2 - 0}int ans = 0;for (int i = 0; i < N; i++) {// cout << "->" << up2nowmin[i] << ", <-" << down2nowmax[i] << endl;ans = max(down2nowmax[i] - up2nowmin[i], ans);}return ans;}
};
一位老哥的,好精简啊
class Solution {
public:int maxProfit(vector<int>& prices) {int pre = prices[0], ans = 0;for (int i = 0; i < prices.size(); i++) {ans = max(ans, prices[i] - pre);pre = min(pre, prices[i]);}return ans;}
};
543. 二叉树的直径
543. 二叉树的直径
题目
给你一棵二叉树的根节点,返回该树的 直径 。
二叉树的 直径 是指树中任意两个节点之间最长路径的 长度 。这条路径可能经过也可能不经过根节点 root 。
两节点之间路径的 长度 由它们之间边数表示。
class Solution {
public:int withCurrentMaxLength(TreeNode* root, int &ans) {if (root == nullptr) {return 0;}int leftLen = withCurrentMaxLength(root->left, ans);int rightLen = withCurrentMaxLength(root->right, ans);ans = max(ans, leftLen + rightLen + 1);return max(leftLen, rightLen) + 1;}int diameterOfBinaryTree(TreeNode* root) {int ans = 0;withCurrentMaxLength(root, ans);return ans - 1;}
};