合集-代码训练营

摘要:704题主要注意一下区间的设置,另外要记得更新mid值,不然会陷入死循环。 class Solution { public: int search (vector<int>& nums, int target) { int left = 0; int right = nums.size() - 1; 阅读全文
posted @ 2024-06-25 15:14 清源风起时 阅读(75) 评论(0) 推荐(0) 编辑
摘要:977题没什么好说的,找到中间第一个非负值,以此为起点向左、向右分别遍历即可。 1 class Solution { 2 public: 3 vector<int> sortedSquares(vector<int>& nums) { 4 //找到第一个非负值 然后依次比较填入新数组中 5 //注意 阅读全文
posted @ 2024-06-26 09:41 清源风起时 阅读(705) 评论(0) 推荐(0) 编辑
摘要:203加一个头节点统一进行操作,很方便,最后提交的时候去掉头节点就好。 1 class Solution { 2 public: 3 ListNode* removeElements(ListNode* head, int val) { 4 //为了操作统一 还是整一个头出来比较合适 5 //依旧是 阅读全文
posted @ 2024-06-26 09:47 清源风起时 阅读(708) 评论(0) 推荐(0) 编辑
摘要:24题多个指针遍历即可,需要注意其中的衔接。 1 class Solution { 2 public: 3 ListNode* swapPairs(ListNode* head) { 4 if (head == NULL || head->next == NULL) { 5 return head; 阅读全文
posted @ 2024-06-26 10:34 清源风起时 阅读(172) 评论(0) 推荐(0) 编辑
摘要:242题思路就是分别遍历两个单词,统计字母数量看看是否一致。 1 class Solution { 2 public: 3 bool isAnagram(string s, string t) { 4 vector<int>tmp1(26); 5 int len1 = s.size(); 6 int 阅读全文
posted @ 2024-06-26 11:54 清源风起时 阅读(694) 评论(0) 推荐(0) 编辑
摘要:454题拆成两块 各自匹配 化成两个O(n^2)运算 1 class Solution { 2 public: 3 int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nu 阅读全文
posted @ 2024-07-04 18:19 清源风起时 阅读(84) 评论(0) 推荐(0) 编辑
摘要:344简单 写个循环 1 class Solution { 2 public: 3 void reverseString(vector<char>& s) { 4 char tmp; 5 int len = s.size(); 6 for (int i = 0; i < len/2; i++) { 阅读全文
posted @ 2024-07-04 18:22 清源风起时 阅读(31) 评论(0) 推荐(0) 编辑
摘要:151以前写过 很呆的写法 但能用 嘿 1 class Solution { 2 public: 3 string reverseWords(string s) { 4 // 初始化变量 5 vector<vector<int>> data; // 存储单词的起始地址和长度 6 string ans 阅读全文
posted @ 2024-07-04 18:26 清源风起时 阅读(62) 评论(0) 推荐(0) 编辑
摘要:232和225题一个思路 一起贴了 1 class MyQueue { 2 public: 3 MyQueue() { 4 stack<int> in; 5 stack<int> out; 6 } 7 8 void push(int x) { 9 in.push(x); 10 } 11 12 int 阅读全文
posted @ 2024-07-04 18:30 清源风起时 阅读(42) 评论(0) 推荐(0) 编辑
摘要:这三道题倒是有点意思的 150题还好 就是读元素 然后进行相应的操作 1 class Solution { 2 public: 3 int evalRPN(vector<string>& tokens) { 4 vector<int>Stack; 5 int tmp; 6 for(int i=0;i 阅读全文
posted @ 2024-07-04 18:33 清源风起时 阅读(44) 评论(0) 推荐(0) 编辑
摘要:今天来处理二叉树part1、2、3,顶级享受,一次到位。 完全二叉树和满二叉树概念没问题。 二叉搜索树,左子树所有结点的值小于它的根结点的值,右子树上所有结点的值大于它的根结点的值 平衡二叉搜索树,它是一棵空树或它的左右两个子树的高度差的绝对值不超过1。 二叉树的存储方式:链式存储、顺序存储。 链式 阅读全文
posted @ 2024-07-05 11:56 清源风起时 阅读(37) 评论(0) 推荐(0) 编辑
摘要:二叉树学习2 226题翻转二叉树,改一下前序递归遍历,每次遍历的时候都调换一下左右结点即可。 class Solution { public: void preorder(TreeNode *root) { if (root == nullptr) { return; } TreeNode* tmp 阅读全文
posted @ 2024-07-05 12:57 清源风起时 阅读(6) 评论(0) 推荐(0) 编辑
摘要:110平衡二叉树 1 class Solution { 2 public: 3 int GetHeight(TreeNode* root) { 4 if (!root) { 5 return 0; 6 } 7 int leftHeight = GetHeight(root->left); 8 if 阅读全文
posted @ 2024-07-05 17:43 清源风起时 阅读(6) 评论(0) 推荐(0) 编辑

点击右上角即可分享
微信分享提示