随笔分类 - 刷题记录
摘要:对称二叉树 迭代法 class Solution { public: bool isSymmetric(TreeNode* root) { if(root == NULL) return true; queue<TreeNode*> que; que.push(root->left); // 将左子
阅读全文
摘要:翻转二叉树 递归法 class Solution { public: TreeNode* invertTree(TreeNode* root) { if(root == nullptr) return root; swap(root->left, root->right); //中 invertTr
阅读全文
摘要:二叉树的层序遍历 递归法 class Solution { public: void order(TreeNode* cur, vector<vector<int>>& result, int depth) { if(cur == nullptr) return; //递归结束条件,指针为空 if(
阅读全文
摘要:二叉树的前序遍历 递归遍历法 class Solution { public: void traversal(TreeNode* cur, vector<int>& result){ if(cur == NULL) return; //当前节点为空,终止递归过程 result.push_back(c
阅读全文
摘要:前 K 个高频元素 class Solution { public: // 小顶堆 class mycomparison { public: bool operator()(const pair<int, int>& lhs, const pair<int, int>& rhs) { return
阅读全文
摘要:滑动窗口最大值 class Solution { private: class MyQueue{ public: deque<int> que; // 使用双端队列deque来实现单调队列 void pop(int value){ // 每次弹出的时候,比较当前要弹出的数值是否等于队列出口元素的数值
阅读全文
摘要:逆波兰表达式求值 class Solution { public: int evalRPN(vector<string>& tokens) { stack<long long> st; for(int i = 0; i < tokens.size(); i++){ //检索到运算符号 if(toke
阅读全文
摘要:删除字符串中所有相邻重复项 1047. 删除字符串中的所有相邻重复项 - 力扣(LeetCode) class Solution { public: string removeDuplicates(string s) { stack<char> st; for(char value : s){ if
阅读全文
摘要:有效的括号 20. 有效的括号 - 力扣(LeetCode) class Solution { public: bool isValid(string s) { if (s.size() % 2 != 0) return false; //奇数必不符合 stack<char> st; for(int
阅读全文
摘要:用栈实现队列 232. 用栈实现队列 - 力扣(LeetCode) class MyQueue { public: stack<int> stIn; stack<int> stOut; MyQueue() { } void push(int x) { stIn.push(x); } int pop(
阅读全文
摘要:重复的子字符串 459. 重复的子字符串 - 力扣(LeetCode) class Solution { public: void getNext (int* next, const string& s){ next[0] = -1; int j = -1; for(int i = 1;i < s.
阅读全文
摘要:实现strStr 28. 找出字符串中第一个匹配项的下标 - 力扣(LeetCode) class Solution { public: //構造next前綴表 void getNext(int* next, const string& s){ //从左往右为前缀,从右往左为后缀。 int j =
阅读全文
摘要:左旋字符串 剑指 Offer 58 - II. 左旋转字符串 - 力扣(LeetCode) class Solution { public: string reverseLeftWords(string s, int n) { reverse(s.begin(), s.begin() + n); r
阅读全文
摘要:反转字符串中的单词 151. 反转字符串中的单词 - 力扣(LeetCode) class Solution { public: void reverse(string& s, int start, int end){ //反转单词字符串,写法为左闭右闭,包括start和end for (int i
阅读全文
摘要:反转字符串 344. 反转字符串 - 力扣(LeetCode) class Solution { public: void reverseString(vector<char>& s) { int right = s.size() - 1; for(int left = 0; left<right;
阅读全文
摘要:四数之和 18. 四数之和 - 力扣(LeetCode) class Solution { public: vector<vector<int>> fourSum(vector<int>& nums, int target) { vector<vector<int>> result; sort(nu
阅读全文
摘要:三数之和 题目链接15. 三数之和 - 力扣(LeetCode) class Solution { public: vector<vector<int>> threeSum(vector<int>& nums) { vector<vector<int>> result; sort(nums.begi
阅读全文
摘要:赎金信 题目链接代码随想录 (programmercarl.com) class Solution { public: bool canConstruct(string ransomNote, string magazine) { int record[26] = {0}; if (ransomNo
阅读全文
摘要:四数相加II 题目链接454. 四数相加 II - 力扣(LeetCode) class Solution { public: int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<in
阅读全文
摘要:快乐数 题目链接202. 快乐数 - 力扣(LeetCode) class Solution { public: int getSum(int n){ int sum = 0; while(n){ sum += (n % 10) * (n % 10); n /= 10; } return sum;
阅读全文

浙公网安备 33010602011771号