llllmz

导航

合集-科软力扣学习记录

34. 在排序数组中查找元素的第一个和最后一个位置
摘要:这题还蛮有意思的,看了下解析,分成两部分分开来求解。 左右边界都是普通的二分查找算法,重点就是当等于的时候的处理,左边界函数等于目标值的时候,要记录当前mid的值作为边界,同时区间要向左移。 反过来,右边界的话,区间要向右移动。记得记录相等时候的mid值,最后一次相等记录的值也就是这个边界值。 cl 阅读全文

posted @ 2024-09-04 17:43 神奇的萝卜丝 阅读(36) 评论(0) 推荐(0)

69. x 的平方根
摘要:这题也是利用二分查找来计算。 首先区间是[0,x]。通过x/2的平方判断应该区间左移还是区间右移,同时值得注意的是要记录mid的平方小于x的mid值,因为比如8的平方根是2,所以平方小于x的mid要记录。 class Solution { public: int mySqrt(int x) { in 阅读全文

posted @ 2024-09-05 21:47 神奇的萝卜丝 阅读(39) 评论(0) 推荐(0)

367. 有效的完全平方数
摘要:class Solution { public: bool isPerfectSquare(int num) { int left = 0, right = num; while(left <= right){ int mid = left + (right - left) / 2; if(pow( 阅读全文

posted @ 2024-09-05 21:51 神奇的萝卜丝 阅读(34) 评论(0) 推荐(0)

27. 移除元素
摘要:像个漏斗一样把元素筛出来就好了。 class Solution { public: int removeElement(vector<int>& nums, int val) { int head = 0, curIndex = 0; for(; curIndex < nums.size(); ++ 阅读全文

posted @ 2024-09-06 21:15 神奇的萝卜丝 阅读(29) 评论(0) 推荐(0)

26. 删除有序数组中的重复项
摘要:class Solution { public: int removeDuplicates(vector<int>& nums) { int head = 0, curIndex = 0, target = INT_MIN; for(; curIndex < nums.size(); ++curIn 阅读全文

posted @ 2024-09-06 21:22 神奇的萝卜丝 阅读(26) 评论(0) 推荐(0)

283. 移动零
摘要:class Solution { public: void moveZeroes(vector<int>& nums) { int head = 0, curIndex = 0; for(; curIndex < nums.size(); ++curIndex){ if(nums[curIndex] 阅读全文

posted @ 2024-09-08 16:28 神奇的萝卜丝 阅读(35) 评论(0) 推荐(0)

844. 比较含退格的字符串
摘要:c++字符串还是不太熟练 class Solution { public: bool backspaceCompare(string s, string t) { return dealString(s) == dealString(t); } private: string dealString( 阅读全文

posted @ 2024-09-08 16:37 神奇的萝卜丝 阅读(51) 评论(0) 推荐(0)

977. 有序数组的平方
摘要:sort 一下 class Solution { public: vector<int> sortedSquares(vector<int>& nums) { vector<int> ret; for(int num : nums){ ret.push_back(pow(num, 2)); } so 阅读全文

posted @ 2024-09-08 16:47 神奇的萝卜丝 阅读(25) 评论(0) 推荐(0)

209. 长度最小的子数组
摘要:滑动窗口!! class Solution { public: int minSubArrayLen(int target, vector<int>& nums) { int left = 0, right = 0, sum = nums[0]; int minLength = INT_MAX; w 阅读全文

posted @ 2024-09-10 22:46 神奇的萝卜丝 阅读(18) 评论(0) 推荐(0)

904. 水果成篮
摘要:改不了C的思维 class Solution { public: int totalFruit(vector<int>& fruits) { if(fruits.size() <= 2) return fruits.size(); int left = 0, right = 0, curNum = 阅读全文

posted @ 2024-09-12 21:35 神奇的萝卜丝 阅读(41) 评论(0) 推荐(0)

76. 最小覆盖子串
摘要:跟着别人的代码履了一遍,明天自己再重写遍。 class Solution { public: map<char, int> tstr, sstr; bool isContained(){ for(auto tchar : tstr){ if(tchar.second > sstr[tchar.fir 阅读全文

posted @ 2024-09-14 22:56 神奇的萝卜丝 阅读(33) 评论(0) 推荐(0)

76. 最小覆盖子串
摘要:class Solution { public: map<char, int> maps, mapt; bool isContained(){ for(pair<char, int> elem : mapt){ if(elem.second > maps[elem.first]) return fa 阅读全文

posted @ 2024-09-15 20:36 神奇的萝卜丝 阅读(26) 评论(0) 推荐(0)

59. 螺旋矩阵 II
摘要:不知道一年后会成长成什么样,只感觉好难好难。有好多东西要学,源码也看不懂,项目也不会做。 class Solution { public: vector<vector<int>> generateMatrix(int n) { vector<vector<int>> vec(n, vector<in 阅读全文

posted @ 2024-09-16 22:29 神奇的萝卜丝 阅读(35) 评论(0) 推荐(0)

58. 区间和(第九期模拟笔试)
摘要:中秋节摆了一天,感觉畏难情绪一直困扰着我,要好好调制状态才行。 #include<iostream> #include<vector> using namespace std; int main(){ int n = 0; cin >> n; vector<int> sum(n, 0); for(i 阅读全文

posted @ 2024-09-17 20:49 神奇的萝卜丝 阅读(39) 评论(0) 推荐(0)

44. 开发商购买土地
摘要:\ 路长且艰,任重而道远,什么时候才能成长成真正的程序员呢 #include<iostream> #include<vector> #include<climits> using namespace std; int main(){ int n, m; cin >> n >> m; vector<v 阅读全文

posted @ 2024-09-18 23:04 神奇的萝卜丝 阅读(94) 评论(0) 推荐(0)

20. 有效的括号
摘要:学习了C++ stack的使用 class Solution { public: #include<stack> bool isValid(string s) { stack<char> stk; for(int i = 0; i < s.size(); ++i){ if(s[i] == '(' | 阅读全文

posted @ 2024-09-19 11:16 神奇的萝卜丝 阅读(40) 评论(0) 推荐(0)

146. LRU 缓存
摘要:\ class LRUCache { public: LRUCache(int capacity) :_capacity(capacity) {} int get(int key) { auto it = _unorderedMap.find(key); if(it != _unorderedMap 阅读全文

posted @ 2024-09-19 17:31 神奇的萝卜丝 阅读(37) 评论(0) 推荐(0)

203. 移除链表元素
摘要:老题新做 class Solution { public: ListNode* removeElements(ListNode* head, int val) { if(!head) return nullptr; if(head->val != val){ head->next = removeE 阅读全文

posted @ 2024-09-20 22:18 神奇的萝卜丝 阅读(32) 评论(0) 推荐(0)

707. 设计链表
摘要:心要细,这题才能作对。 class MyLinkedList { public: MyLinkedList() :_size(0), _head(new listedNode(0)) {} int get(int index) { if(index < 0 || index >= _size) re 阅读全文

posted @ 2024-09-24 18:42 神奇的萝卜丝 阅读(24) 评论(0) 推荐(0)

206. 反转链表
摘要:class Solution { public: ListNode* reverseList(ListNode* head) { if(!head) return nullptr; ListNode* cur = head->next; if(!cur) return head; head->nex 阅读全文

posted @ 2024-09-24 18:48 神奇的萝卜丝 阅读(24) 评论(0) 推荐(0)

24. 两两交换链表中的节点
摘要:感觉自己畏难情绪好严重呀,一点都不想学新东西。 class Solution { public: ListNode* swapPairs(ListNode* head) { if(!head) return nullptr; if(!head->next) return head; ListNode 阅读全文

posted @ 2024-09-26 19:27 神奇的萝卜丝 阅读(34) 评论(0) 推荐(0)

19. 删除链表的倒数第 N 个结点
摘要:相当于删除正数第n个节点 class Solution { public: ListNode* removeNthFromEnd(ListNode* head, int n) { if(!head) return head; int listLength = 0; ListNode* temp = 阅读全文

posted @ 2024-09-27 20:45 神奇的萝卜丝 阅读(36) 评论(0) 推荐(0)

19. 删除链表的倒数第 N 个结点 Plus
摘要:class Solution { public: ListNode* removeNthFromEnd(ListNode* head, int n) { if(!head) return head; ListNode* dummyHead = new ListNode(0, head); ListN 阅读全文

posted @ 2024-09-27 21:03 神奇的萝卜丝 阅读(26) 评论(0) 推荐(0)

面试题 02.07. 链表相交
摘要:明天回家喽,最近在学习的瓶颈期,感觉学的东西好难,有厌学的心理,但是我相信过了这段煎熬的时期,就好了。 class Solution { public: ListNode *getIntersectionNode(ListNode *headA, ListNode *headB) { int na 阅读全文

posted @ 2024-09-28 21:37 神奇的萝卜丝 阅读(31) 评论(0) 推荐(0)

142. 环形链表 II
摘要:class Solution { public: ListNode *detectCycle(ListNode *head) { ListNode* fast = head; ListNode* slow = head; while(fast && fast->next){ fast = fast- 阅读全文

posted @ 2024-09-30 19:46 神奇的萝卜丝 阅读(24) 评论(0) 推荐(0)

242. 有效的字母异位词
摘要:class Solution { public: bool isAnagram(string s, string t) { if(s.size() != t.size()) return false; for(int i = 0; i < s.size(); ++i) ++maps[s[i]]; f 阅读全文

posted @ 2024-10-04 20:04 神奇的萝卜丝 阅读(22) 评论(0) 推荐(0)

349. 两个数组的交集
摘要:class Solution { public: vector<int> intersection(vector<int>& nums1, vector<int>& nums2) { unordered_set<int> ans_set; unordered_set<int> num1_set(nu 阅读全文

posted @ 2024-10-05 21:03 神奇的萝卜丝 阅读(30) 评论(0) 推荐(0)

202. 快乐数
摘要:预计再过半个月就可以写项目了,不知道像我这样的彩笔,能不能驾驭得住项目难度,希望可以随便拿捏。 class Solution { public: bool isHappy(int n) { unordered_set<int> unset; while(1){ int sum = getNextNu 阅读全文

posted @ 2024-10-06 20:49 神奇的萝卜丝 阅读(31) 评论(0) 推荐(0)

1. 两数之和
摘要:class Solution { public: vector<int> twoSum(vector<int>& nums, int target) { unordered_map<int, int> unmap; for(int i = 0; i < nums.size(); ++i){ unma 阅读全文

posted @ 2024-10-07 21:56 神奇的萝卜丝 阅读(42) 评论(0) 推荐(0)

454. 四数相加 II
摘要:class Solution { public: int fourSumCount(vector<int>& nums1, vector<int>& nums2, vector<int>& nums3, vector<int>& nums4) { int ans = 0; unordered_map 阅读全文

posted @ 2024-10-08 18:36 神奇的萝卜丝 阅读(33) 评论(0) 推荐(0)

383. 赎金信
摘要:class Solution { public: bool canConstruct(string ransomNote, string magazine) { unordered_map<char, int> unmapran; unordered_map<char, int> unmapmag; 阅读全文

posted @ 2024-10-10 22:38 神奇的萝卜丝 阅读(23) 评论(0) 推荐(0)

15. 三数之和
摘要:practise makes perfect! 相信自己的努力不会白费,继续学习吧 class Solution { public: vector<vector<int>> threeSum(vector<int>& nums) { vector<vector<int>> ans; sort(num 阅读全文

posted @ 2024-10-12 19:45 神奇的萝卜丝 阅读(28) 评论(0) 推荐(0)

18. 四数之和
摘要:class Solution { public: vector<vector<int>> fourSum(vector<int>& nums, int target) { vector<vector<int>> ans; sort(nums.begin(), nums.end()); long su 阅读全文

posted @ 2024-10-13 23:00 神奇的萝卜丝 阅读(34) 评论(0) 推荐(0)

344. 反转字符串
摘要:class Solution { public: void reverseString(vector<char>& s) { int head = 0, tail = s.size() -1; while(head < tail){ char temp = s[head]; s[head] = s[ 阅读全文

posted @ 2024-10-14 22:24 神奇的萝卜丝 阅读(27) 评论(0) 推荐(0)

541. 反转字符串 II
摘要:class Solution { public: string reverseStr(string s, int k) { for(int i = 0; i < s.size(); i += 2*k){ if(s.size() - i >= k){ reverse(s, i, i + k -1); 阅读全文

posted @ 2024-10-14 23:05 神奇的萝卜丝 阅读(28) 评论(0) 推荐(0)

55. 右旋字符串(第八期模拟笔试)
摘要:#include <iostream> #include <string> using std::cin; using std::cout; using std::endl; using std::string; int main(){ int k; string s; cin >> k; cin 阅读全文

posted @ 2024-10-17 18:44 神奇的萝卜丝 阅读(40) 评论(0) 推荐(0)

28. 找出字符串中第一个匹配项的下标
摘要:决定转前端了。先用c++刷题刷着先 class Solution { public: int strStr(string haystack, string needle) { if(haystack.size() < needle.size()) return -1; for(int i = 0; 阅读全文

posted @ 2024-10-19 18:20 神奇的萝卜丝 阅读(35) 评论(0) 推荐(0)

459. 重复的子字符串
摘要:KMP算法一点都不想回忆了,等找实习的时候再仔细看吧 class Solution { public: bool repeatedSubstringPattern(string s) { if(s.size() <= 1) return false; string temp = "", str = 阅读全文

posted @ 2024-10-21 23:33 神奇的萝卜丝 阅读(17) 评论(0) 推荐(0)

232. 用栈实现队列
摘要:class MyQueue { public: MyQueue() { } void push(int x) { s1.push(x); } int pop() { int ret; if(!empty()){ if(!s2.empty()){ ret = s2.top(); s2.pop(); r 阅读全文

posted @ 2024-10-22 19:08 神奇的萝卜丝 阅读(38) 评论(0) 推荐(0)

225. 用队列实现栈
摘要:class MyStack { public: MyStack() : q1(queue<int>()), q2(queue<int>()) { } void push(int x) { q1.push(x); } int pop() { int ret; if(q1.size() == 1){ r 阅读全文

posted @ 2024-10-23 23:01 神奇的萝卜丝 阅读(33) 评论(0) 推荐(0)

20. 有效的括号
摘要:class Solution { public: bool isValid(string s) { int r1 = 0, r2 = 0, r3 = 0; char temp; stack<char> stk; for(int i = 0; i < s.size(); ++i){ if(s[i] = 阅读全文

posted @ 2024-10-24 22:16 神奇的萝卜丝 阅读(29) 评论(0) 推荐(0)