随笔分类 -  C++ 刷题

LeetCode刷题
摘要:class MyQueue { public: stack<int>stIn; stack<int>stOut; MyQueue() { } void push(int x) { stIn.push(x); } // 从队列开头移除并返回元素 int pop() { if(stOut.empty() 阅读全文
posted @ 2022-11-01 16:48 香花草的味道 阅读(12) 评论(0) 推荐(0) 编辑
摘要:注意查看reverse的使用方法 class Solution { public: string reverseLeftWords(string s, int n) { reverse(s.begin(), s.begin() + n); reverse(s.begin() + n, s.end() 阅读全文
posted @ 2022-11-01 09:43 香花草的味道 阅读(14) 评论(0) 推荐(0) 编辑
摘要:class Solution { public: // 此处为自定义的reverse函数,其翻转为左闭右闭[]的翻转 void reverse(string &s, int start, int end) { for(int i = start, j = end; i < j; i++, j--) 阅读全文
posted @ 2022-10-31 17:05 香花草的味道 阅读(17) 评论(0) 推荐(0) 编辑
摘要:class Solution { public: vector<int> twoSum(vector<int>& nums, int target) { std::unordered_map<int, int>map; for(int i = 0; i < nums.size(); i++) { / 阅读全文
posted @ 2022-10-28 21:26 香花草的味道 阅读(23) 评论(0) 推荐(0) 编辑
摘要:#include<iostream> #include<unordered_set> using namespace std; // 取数值各个位上的单数平方之和 int getSum(int n) { int sum = 0; while (n) { sum += (n % 10) * (n % 阅读全文
posted @ 2022-10-27 17:11 香花草的味道 阅读(25) 评论(0) 推荐(0) 编辑
摘要:![](https://img2022.cnblogs.com/blog/2673551/202210/2673551-20221027120845314-464201030.png) 阅读全文
posted @ 2022-10-27 12:09 香花草的味道 阅读(8) 评论(0) 推荐(0) 编辑
摘要:暴力的解法,两层for循环,同时还要记录字符是否重复出现,很明显时间复杂度是 O(n^2) 利用哈希表解法 class Solution { public: bool isAnagram(string s, string t) { // 定义hash数组 int num = 26; int hash 阅读全文
posted @ 2022-10-27 11:00 香花草的味道 阅读(19) 评论(0) 推荐(0) 编辑
摘要:哈希表讲解参考连接: 原文链接:https://blog.csdn.net/weixin_40535588/article/details/121480672 此处源于代码随想录 哈希表的关键码就是数组的索引下标,然后通过下标直接访问数组中的元素 哈希表能解决什么问题? 一般哈希表都是用于快速判断一 阅读全文
posted @ 2022-10-27 10:58 香花草的味道 阅读(18) 评论(0) 推荐(0) 编辑
摘要:class Solution { public: ListNode *detectCycle(ListNode *head) { ListNode* fast = head; ListNode* slow = head; while(fast != NULL && fast->next != NUL 阅读全文
posted @ 2022-10-27 10:00 香花草的味道 阅读(14) 评论(0) 推荐(0) 编辑
摘要:class Solution { public: ListNode* deleteback(ListNode*head, int n) { ListNode* dummyHead = new ListNode(0); dummyHead->next = head; ListNode* fast = 阅读全文
posted @ 2022-10-27 09:47 香花草的味道 阅读(11) 评论(0) 推荐(0) 编辑
摘要:#include<iostream> using namespace std; struct ListNode { int val; ListNode* next; ListNode(int val) :val(val), next(NULL) {}; }; // 根据数组创建链表 ListNode 阅读全文
posted @ 2022-10-26 19:54 香花草的味道 阅读(21) 评论(0) 推荐(0) 编辑
摘要:#include<iostream> #include<list> using namespace std; struct ListNode { int val; // 节点上存储的元素 ListNode* next; // 指向下一个节点的指针 ListNode(int x) : val(x), 阅读全文
posted @ 2022-10-26 19:04 香花草的味道 阅读(28) 评论(0) 推荐(0) 编辑
摘要:struct ListNode { int val; ListNode* next; ListNode(int val) :val(val), next(NULL) {}; }; class Solution { public: ListNode* reverseList(ListNode* hea 阅读全文
posted @ 2022-10-26 16:55 香花草的味道 阅读(15) 评论(0) 推荐(0) 编辑
摘要:#include<iostream> using namespace std; /* 1.获取第n个节点的值 2.头部插入节点 3.尾部插入节点 4.第n个节点前插入节点 5.删第n个节点 */ class MyLinkList { public: // 定义链表节点结构体 struct Linke 阅读全文
posted @ 2022-10-26 16:55 香花草的味道 阅读(60) 评论(0) 推荐(0) 编辑

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