Leetcode
聚精会神搞学习
这个作者很懒,什么都没留下…
展开
-
Leetcode:49.字母异位词分组
用哈希表解决字母异位词分组问题 map的key为排序好的字符串,value为答案 class Solution { public: vector<vector<string>> groupAnagrams(vector<string>& strs) { unordered_map<string,vector<string>> mp; for(string& str : strs){原创 2022-03-16 09:23:51 · 348 阅读 · 0 评论 -
Leetcode15. 三数之和(面试经典,要反复看)
思想 first向右走 second = first + 1向右走 third = n - 1 向左走 假设此时 first + second + third = target 那么下一次不同的结果必须second向右走,third向左走才能保证答案是不同的 class Solution { public: vector<vector<int>> threeSum(vector<int>& nums) { int n = nums.si原创 2022-03-16 09:23:22 · 357 阅读 · 0 评论 -
Leetcode344. 反转字符串
😊这道题如果知道了swap函数就非常的好解了 class Solution { public: void reverseString(vector<char>& s) { int len = s.size(); for(int left = 0, right = len -1; left < right ; left++,right--){ swap(s[left],s[right]);原创 2022-03-07 11:09:57 · 201 阅读 · 0 评论 -
Leetcode剑指 Offer 58 - II. 左旋转字符串
暴力解 class Solution { public: string reverseLeftWords(string s, int n) { string str; for(int i =n; i < s.length(); i++){ str.push_back(s[i]); } for(int i = 0; i < n ; i ++){ str.push_back(s[原创 2022-03-07 10:08:27 · 136 阅读 · 0 评论 -
Leetcode剑指 Offer 05. 替换空格
暴力解: class Solution { public: string replaceSpace(string s) { for(int i = 0; i < s.length(); i ++){ char ch = s[i]; if(ch == ' '){ s.append(" "); for(原创 2022-03-07 09:26:26 · 225 阅读 · 0 评论 -
Leetcode1047. 删除字符串中的所有相邻重复项
class Solution { public: string removeDuplicates(string s) { string stk; for(char ch : s){ if(!stk.empty() && stk.back() == ch){ //pop_back 在字符串末尾删除一个字符 stk.pop_back(); }原创 2022-03-06 16:16:28 · 143 阅读 · 0 评论 -
Leetcode150. 逆波兰表达式求值
本体需要注意的问题: 1.数字有可能是负数 2.容器的类型是字符串,不是字符 示例: 输入:tokens = [“10”,“6”,“9”,“3”,"+","-11","","/","",“17”,"+",“5”,"+"] 输出:22 解释:该算式转化为常见的中缀算术表达式为: ((10 * (6 / ((9 + 3) * -11))) + 17) + 5 = ((10 * (6 / (12 * -11))) + 17) + 5 = ((10 * (6 / -132)) + 17) + 5 = ((10 *原创 2022-03-05 10:05:00 · 162 阅读 · 0 评论 -
Leetcode:剑指 Offer 22. 链表中倒数第k个节点
代码实现的思想: 设置两个指针(node1,node2)指向头节点 第一个指针走k-1步(因为倒数第k个和最后一个节点距离为k-1) 然后让node1,node2同时向前走,直到node1走到最后一个节点 此时node2恰好在倒数第k个节点上 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x),原创 2022-03-04 10:49:33 · 208 阅读 · 0 评论 -
Leetcode160. 相交链表
/** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * ListNode(int x) : val(x), next(NULL) {} * }; */ class Solution { public: ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) {原创 2022-03-03 15:24:24 · 434 阅读 · 0 评论 -
Leetcode142. 环形链表 II
class Solution { public: ListNode *detectCycle(ListNode *head) { ListNode *slow = head, *fast = head; while (fast != nullptr) { slow = slow->next; if (fast->next == nullptr) { return nullptr原创 2022-03-03 15:21:31 · 196 阅读 · 0 评论 -
Leetcode141. 环形链表
/** * 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-原创 2022-03-03 15:20:53 · 178 阅读 · 0 评论 -
Leetcode24. 两两交换链表中的节点
/** * 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)原创 2022-03-03 15:17:43 · 436 阅读 · 0 评论 -
Leetcode977. 有序数组的平方
class Solution { public: vector<int> sortedSquares(vector<int>& nums) { //指向第一个链表左部的指针 int left = 0; //指向第一个链表右部的指针 int right = nums.size() - 1; //开辟一个等长的容器 vector<int> res(nums.size原创 2022-03-03 15:07:41 · 88 阅读 · 0 评论 -
Leetcode209. 长度最小的子数组
class Solution { public: int minSubArrayLen(int target, vector<int>& nums) { int sum = 0; int start = 0; int subLength = 0; int minSize = INT_MAX; for(int end = 0; end < nums.size(); end++){原创 2022-03-03 14:48:28 · 76 阅读 · 0 评论 -
Leetcode20:有效的括号
class Solution { public: bool isValid(string s) { //无序的map unordered_map<char,int> m{{'(',1},{'[',2},{'{',3}, {')',4},{']',5},{'}',6}}; stack<char> st; bool istrue=true;原创 2022-03-03 14:27:49 · 82 阅读 · 0 评论