leetcode
文娘
这个作者很懒,什么都没留下…
展开
-
lc24两两交换链表中的节点
递归法。 官方题解非常好理解,head是每一组的第一个节点,newHead是每一组的第二个节点,思路是将head的next指向下一组的头节点,将newHead的next指向head,并且返回newHead,这样就完成了每一组两两相邻的交换。 var swapPairs = function(head) { if(head===null||head.next==null) return head var newHead=head.next head.next=swapPairs(new原创 2022-01-06 23:24:26 · 319 阅读 · 0 评论 -
lc206反转链表
递归与非递归写法 递归 var reverseList=function(head){ if(head===null||head.next===null) return head if(head.next==null) return head//如果到了链表尾,返回head var last=reverseList(head.next)//否则将节点反转 head.next.next=head head.next=null return last } 非递归原创 2022-01-05 20:53:38 · 233 阅读 · 0 评论 -
lc3.无重复字符的最长子串
没思路,官方题解是滑动窗口做的,改天写上。 让我记下学长写的题解,很好看懂。 var lengthOfLongestSubstring = function(s) { let m=new Map()//map存储字符对应最后一次出现的下标 let max=0//最长子串长度 let start=0//最长子串的开始字符下标 for(let i=0;i<s.length;i++){ if(m.has(s[i])){//如果遇到了重复的字符,更新最长子串的开原创 2022-01-05 17:57:25 · 123 阅读 · 0 评论 -
leetcode27&&26
27.移除元素 var removeElement = function(nums, val) { for(var i=0;i<nums.length;i++){ if(nums[i]==val){ nums.splice(i,1); i--; } } return nums.length; }; 然后从这里有了26题的灵感,虽然不是很好,但是可以通过。 var removeDuplicates原创 2021-04-20 00:33:19 · 56 阅读 · 0 评论