![](https://img-blog.csdnimg.cn/20201014180756925.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
leetcode
文章平均质量分 55
力扣
Aurora2005
这个作者很懒,什么都没留下…
展开
-
541. Reverse String II
public:i<j;i<length;原创 2024-07-07 11:23:50 · 176 阅读 · 0 评论 -
344. Reverse String
swithO(1)原创 2024-07-07 10:22:45 · 115 阅读 · 0 评论 -
18. 4Sum
numsofnbcd。原创 2024-07-06 15:52:22 · 123 阅读 · 0 评论 -
15. 3Sum
iii)else 将这三个数加入到result数组中,同时,我们需要在这里考虑去重操作,为什么不在前面判断呢?(3)内部再一次循环,循环条件是left原创 2024-06-30 11:06:48 · 296 阅读 · 0 评论 -
383. Ransom Note
Given two strings and , return if can be constructed by using the letters from and otherwise.Each letter in can only be used once in .Constraints:注意: 1.unordered_map<char,int>map;不是unordered_map<char,char>map 2.应该先遍历magazine,再遍历ran原创 2024-06-29 11:12:22 · 307 阅读 · 0 评论 -
454. 4Sum II
Given four integer arrays , , , and all of length , return the number of tuples such that:Constraints: 1.为什么想到用哈希表map? 因为在这道题中,需要统计四个数相加为0的四元组,同时还需要记录出现的次数 2.count+=map[target]? 假设a[1]+b[2]=5,a[2]+b[3]=5,c[1]+d[2]=-5,此时出现了两原创 2024-06-29 10:58:36 · 291 阅读 · 0 评论 -
1. Two Sum
key表示数值,value表示下标(代码中是second),因为map查找key的速度比较快。这里用到的有两个数,一个是数值,一个是下标,而且在这里,并不需要map是有序的。1.为什么想到用map,为什么是unordered_map?3.key和value分别表示什么?用来存储符合条件的数组元素和下标。4.熟悉map的常见操作。此图来源于代码随想录网站。原创 2024-06-28 12:03:06 · 359 阅读 · 0 评论 -
202. Happy Number
2.注意别把bool中建立unordered_set放到循环中了,这样会重复建立哈希表,导致死循环。1.如何判断是否出现死循环?——哈希表,这个sum曾出现过。原创 2024-06-27 11:16:08 · 369 阅读 · 0 评论 -
349. Intersection of Two Arrays
1.总体思路就是:先用一个unordered_set建立result,用来存储最后的结果,然后再建立一个unordered_set<int>nums用来存储nums1的元素,方便遍历一次寻找交集,遍历nums2时,当nums中出现了nums2中的元素,就插入到result中。最后返回vector(强制把set转为vector)2.unordered_set是去重的。1.是hash[nums1[i]]=1。原创 2024-06-26 11:45:33 · 253 阅读 · 0 评论 -
242. Valid Anagram
Given two strings and , return if is an anagram of , and otherwise.An Anagram is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once.Constraints:Follow up: What if the原创 2024-06-25 14:55:11 · 325 阅读 · 0 评论 -
142. Linked List Cycle II
Given the of a linked list, return the node where the cycle begins. If there is no cycle, return .There is a cycle in a linked list if there is some node in the list that can be reached again by continuously following the pointer. Internally, is used to原创 2024-06-24 12:09:47 · 766 阅读 · 0 评论 -
160. Intersection of Two Linked Lists
这道题和02.07的面试题简直是一摸一样的。原创 2024-06-24 11:32:54 · 804 阅读 · 0 评论 -
面试题 02.07. Intersection of Two Linked Lists LCCI
nullnull。原创 2024-06-24 11:25:34 · 1001 阅读 · 0 评论 -
24. Swap Nodes in Pairs
Given a linked list, swap every two adjacent nodes and return its head. You must solve the problem without modifying the values in the list's nodes (i.e., only nodes themselves may be changed.)Constraints:注意: 1.curr->next->next=temp1;这行代码小心别少了一原创 2024-06-23 11:45:09 · 341 阅读 · 0 评论 -
206. Reverse Linked List
Given the of a singly linked list, reverse the list, and return the reversed list.Constraints:Follow up: A linked list can be reversed either iteratively or recursively. Could you implement both?2.递归:在双指针思路上进行注意: 1.在用递归时,别忘了返回值 2.反原创 2024-06-23 11:17:55 · 297 阅读 · 0 评论 -
707. Design Linked List
Design your implementation of the linked list. You can choose to use a singly or doubly linked list.A node in a singly linked list should have two attributes: and . is the value of the current node, and is a pointer/reference to the next node.If you w原创 2024-06-22 12:59:44 · 943 阅读 · 0 评论 -
移除链表元素总结
其实这个链表的移除操作只是链表的一个基本操作,这几道题也没有什么很难的点,只需要注意dummyHead什么时候使用就可以。2.移除链表元素的倒数第n个元素。3.删除排序链表中的重复元素。1.直接移除链表元素。原创 2024-06-21 09:26:48 · 76 阅读 · 0 评论 -
83. Remove Duplicates from Sorted L
Given the of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well.Constraints:注意: 1.需要先判断链表是否为空 2.因为需要直接在链表上进行删除操作,所以用dummyHead直接=head,之所以没有用Head直接操作,是为了return的时候方便找原创 2024-06-21 09:23:45 · 405 阅读 · 0 评论 -
19. Remove Nth Node From End of List
Given the of a linked list, remove the node from the end of the list and return its head.Constraints:Follow up: Could you do this in one pass?注意:我的这种方法是最容易想到的,先遍历一遍链表得到链表长度,需要注意的一点只有count++放的位置了。原创 2024-06-20 12:29:29 · 761 阅读 · 0 评论 -
203. Remove Linked List Elements
Given the of a linked list and an integer , remove all the nodes of the linked list that has , and return the new head.Constraints:注意: 1.其实这道题可以有两种方法去做,第一种是不适用虚拟头节点的,但是这种方法需要分类,一种是删除的节点是头节点的时候,第二种是其他元素,但是这样的话,代码不够简洁。所以采用了虚拟头节点的方式来做 2.C+原创 2024-06-20 12:26:41 · 492 阅读 · 0 评论 -
螺旋矩阵总结
其实螺旋矩阵的话,感觉它并没有什么很复杂的知识点,主要需要考虑的就是,选择左闭右开还是左闭右闭,这个主要是用来判断边界条件,然后按照矩形的每一条边依次考虑。原创 2024-06-20 11:28:55 · 122 阅读 · 0 评论 -
885. Spiral Matrix III
You start at the cell of an grid facing east. The northwest corner is at the first row and column in the grid, and the southeast corner is at the last row and column.You will walk in a clockwise spiral shape to visit every position in this grid. Whenever原创 2024-06-19 12:33:55 · 572 阅读 · 0 评论 -
54. Spiral Matrix
Given an , return all elements of the in spiral order.Constraints:注意: 1.和59题不一样的点在于,59题保证了一定是n×n型的矩阵,这个情况下我们需要考虑的边界条件比较少,故用左闭右开的原则其实写起来也不会很麻烦 2.这里采用了左闭右闭的原则,其实也完全可以用左闭右开的原则。我之前一直在纠结用左闭右闭的话,不就会重复计入元素了吗?但其实不会,因为每一个for循环后面的if判断句中StartX和St原创 2024-06-18 11:50:36 · 199 阅读 · 0 评论 -
59. Spiral Matrix II
4.总体思路是用到了左闭右开的原则,这点参考二分查找,每行每列的最后一个元素都是交给下一个循环的时候做。这道题的算法很简单,就是需要处理的边界条件有点绕,很容易没注意到就错了,小心数组的越界。1.offset的初始值,其实就是因为数组的行和列都是从0开始的。3.每旋转一圈StartX、StartY和offset都要+1。2.注意边界条件,数组别越界了。原创 2024-06-17 12:12:30 · 429 阅读 · 0 评论 -
滑动窗口总结
我们可以对比一下这两道滑动窗口的题目,第一道是力扣209题,求长度最小的子数组,第二道是力扣3题,无重复字符的最长子串。都是滑动窗口中求不定长的最长、最短型。(还有别的类型,主要是还没刷到,下次二刷的时候刷灵神的题单时补充)原创 2024-06-17 11:26:10 · 204 阅读 · 0 评论 -
3. Longest Substring Without Repeating Characters
s313。原创 2024-06-16 12:26:59 · 412 阅读 · 0 评论 -
209. Minimum Size Subarray Sum
Given an array of positive integers and a positive integer , return the minimal length of a subarraywhose sum is greater than or equal to . If there is no such subarray, return instead.Constraints:Follow up: If you have figured out the solution, try cod原创 2024-06-16 11:54:10 · 485 阅读 · 0 评论 -
349. Intersection of Two Arrays
方法:双指针+哈希表nums1andnums2, return[2][9,4]原创 2024-06-14 09:35:02 · 495 阅读 · 0 评论 -
977. Squares of a Sorted Array
Given an integer array sorted in non-decreasing order, return an array of the squares of each number sorted in non-decreasing order.Example 1:Input: nums = [-4,-1,0,3,10]Output: [0,1,9,16,100]Explanation: After squaring, the array becomes [16,1,0,9,100原创 2024-06-14 09:01:20 · 507 阅读 · 0 评论 -
283. Move Zeroes
nums, move all0nums = [0][0]但是这个代码的空间复杂度要O(N),下列代码空间复杂度为O(1)原创 2024-06-13 12:47:47 · 433 阅读 · 0 评论 -
26. Remove Duplicates from Sorted Array
第一次的代码:执行出错——原因,当fast=nums.size()-1的时候,fast+1越界第二次:修改后,把slow和fast的初始值都更改为1。原创 2024-06-12 15:25:58 · 126 阅读 · 0 评论 -
27. Remove Element
Given an integer array and an integer , remove all occurrences of in in-place. The order of the elements may be changed. Then return the number of elements in which are not equal to .Consider the number of elements in which are not equal to be , to g原创 2024-06-12 15:07:56 · 527 阅读 · 0 评论 -
二分法的总结
相信大家在刷二分法的时候也会遇到一些相似的问题,接下来就一一解答吧。原创 2024-06-12 14:26:44 · 1124 阅读 · 0 评论 -
441. Arranging Coins
You havenkithin, returnn = 52n = 83第一次代码:超出时间限制第二次代码:修正后。原创 2024-06-11 12:18:26 · 465 阅读 · 0 评论 -
744. Find Smallest Letter Greater Than Target
第一次的代码:超出时间限制,可能有死循环第二次:修正后。原创 2024-06-11 11:48:02 · 224 阅读 · 0 评论 -
33. Search in Rotated Sorted Array
numsvalues).numsisk3and becomenumstarget, returntargetnums, or-1numsO(log n)4-1-1numsnums第一次写的代码:只是简单地用middle把左右两边分开,但是事实上应该先分清楚是顺序(刚好旋转回去了)还是乱序(旋转后不是单一的递增)原创 2024-06-11 11:16:01 · 325 阅读 · 0 评论 -
162. Find Peak Element
A peak element is an element that is strictly greater than its neighbors.Given a 0-indexed integer array , find a peak element, and return its index. If the array contains multiple peaks, return the index to any of the peaks.You may imagine that . In other原创 2024-06-10 15:57:12 · 365 阅读 · 0 评论 -
153. Find Minimum in Rotated Sorted
Suppose an array of length sorted in ascending order is rotated between and times. For example, the array might become:Notice that rotating an array 1 time results in the array .Given the sorted rotated array of unique elements, return the minimum el原创 2024-06-10 15:17:29 · 490 阅读 · 0 评论 -
69. Sqrt(x)
5.我第一次的提交用的是返回middle,在第一个if里面就返回了,但事实上这是错的。说到这个返回ret,注意else后面是middle*middle>x的情况了,这个肯定不行,所以不需要更新ret的值。4.之前我使用三个if类型句子,超出了时间限制,而且当时的第三个else{ret=middle},完全可以并到第一个if里面。2.left初始值设为0,因为题目是说非负整数,并不排除0,如果排除0的话,left的初始值设为1是可以的。3.middle*middle的数据类型要设为long long。原创 2024-06-10 14:34:22 · 243 阅读 · 0 评论 -
35. Search Insert Position
Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.You must write an algorithm with runtime complexity.Constraints: 1.其实这题主要原创 2024-06-09 21:18:24 · 360 阅读 · 0 评论