LeetCode与《代码随想录》双指针篇:做题笔记与总结-JavaScript版

Posted karshey

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode与《代码随想录》双指针篇:做题笔记与总结-JavaScript版相关的知识,希望对你有一定的参考价值。

文章目录

代码随想录

代码随想录
代码随想录CSDN官方

主要题目

27. 移除元素

https://leetcode.cn/problems/remove-element/

/**
 * @param number[] nums
 * @param number val
 * @return number
 */
var removeElement = function (nums, val) 
    let i = 0;
    for (let j = 0; j < nums.length; j++) 
        if (nums[j] != val) 
            nums[i++] = nums[j];
        
    
    return i;
;

344.反转字符串

https://leetcode.cn/problems/reverse-string/

/**
 * @param character[] s
 * @return void Do not return anything, modify s in-place instead.
 */
var reverseString = function (s) 
    for (let i = 0, j = s.length - 1; i < j; i++, j--) 
        [s[i], s[j]] = [s[j], s[i]]
    
;

剑指Offer 05.替换空格

https://leetcode.cn/problems/ti-huan-kong-ge-lcof/

/**
 * @param string s
 * @return string
 */
var replaceSpace = function (s) 
    let arr = Array.from(s);
    let ans = [];
    for (let i = 0; i < arr.length; i++) 
        if (arr[i] == ' ') 
            ans.push('%20')
         else 
            ans.push(arr[i])
        
    
    return ans.join('')
;

151.翻转字符串里的单词

https://leetcode.cn/problems/reverse-words-in-a-string/

/**
 * @param string s
 * @return string
 */

// 去除多余空格
var space = function (s) 
    let arr = s.split(' ');
    let ans = []
    for (let i = arr.length - 1; i >= 0; i--) 
        if (arr[i] != '') 
            ans.push(arr[i])
        
    
    return ans;


var reverseWords = function (s) 
    let arr = space(s);
    return arr.join(' ')
;

206.反转链表

https://leetcode.cn/problems/reverse-words-in-a-string/submissions/405363322/

/**
 * Definition for singly-linked list.
 * function ListNode(val, next) 
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * 
 */
/**
 * @param ListNode head
 * @return ListNode
 */
var reverseList = function (head) 
    if (head === null || head.next === null) return head;
    var a = head, b = a.next, c = b.next;
    a.next = null;
    while (b) 
        b.next = a;
        a = b; b = c;
        if (c) c = c.next;
    
    return a;
;

24. 两两交换链表中的节点

https://leetcode.cn/problems/swap-nodes-in-pairs/

要注意:不能出现null.next的情况。

/**
 * Definition for singly-linked list.
 * function ListNode(val, next) 
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * 
 */
/**
 * @param ListNode head
 * @return ListNode
 */
var swapPairs = function (head) 
    if (head === null || head.next === null) return head;
    // 虚拟头结点,指向头结点
    let pre = new ListNode(0, head);
    let a = pre, b = a.next, c = b.next;
    while (b && c) 
        a.next = c;
        b.next = c.next;
        c.next = b;
        a = a.next.next;
        b = a.next;
        if (b) c = b.next;
    
    return pre.next;
;

19. 删除链表的倒数第 N 个结点

https://leetcode.cn/problems/remove-nth-node-from-end-of-list/description/

一定要从虚拟头节点开始,而非头节点。这样可以兼容链表中只有一个节点的特殊情况。

/**
 * Definition for singly-linked list.
 * function ListNode(val, next) 
 *     this.val = (val===undefined ? 0 : val)
 *     this.next = (next===undefined ? null : next)
 * 
 */
/**
 * @param ListNode head
 * @param number n
 * @return ListNode
 */
var removeNthFromEnd = function (head, n) 
    let pre = new ListNode(0, head);
    let i = pre;
    for (let ii = 0; ii < n; ii++) 
        i = i.next;
    
    let j = pre;
    while (i.next) 
        i = i.next;
        j = j.next;
    
    j.next = j.next.next;
    return pre.next;
;

面试题 02.07. 链表相交

https://leetcode.cn/problems/intersection-of-two-linked-lists-lcci/

/**
 * Definition for singly-linked list.
 * function ListNode(val) 
 *     this.val = val;
 *     this.next = null;
 * 
 */

/**
 * @param ListNode headA
 * @param ListNode headB
 * @return ListNode
 */
var getIntersectionNode = function (headA, headB) 
    let a = headA, b = headB;
    let numa = 1, numb = 1;
    while (a) 
        a = a.next;
        numa++;
    
    while (b) 
        b = b.next;
        numb++;
    
    while (numa > numb) 
        headA = headA.next;
        numa--;
    
    while (numb > numa) 
        headB = headB.next;
        numb--;
    
    while (headA && headB && headA !== headB) 
        headA = headA.next;
        headB = headB.next;
    
    return headA;
;

142.环形链表II

https://leetcode.cn/problems/linked-list-cycle-ii/

注意:要在while里面判断i===j,若在while条件中要求i!==j,则根本不会进入循环。

/**
 * Definition for singly-linked list.
 * function ListNode(val) 
 *     this.val = val;
 *     this.next = null;
 * 
 */

/**
 * @param ListNode head
 * @return ListNode
 */
var detectCycle = function (head) 
    let i = head, j = head;
    while (i && j) 
        i = i.next;
        j = j.next;
        if (i) i = i.next;
        if (i === j) break;
    
    if (!i) 
        return null;
    

    // ij在这相遇 i是快指针
    let k = head;
    while (i != k) 
        i = i.next;
        k = k.next;
    
    return i;
;

15. 三数之和

https://leetcode.cn/problems/3sum/

/**
 * @param number[] nums
 * @return number[][]
 */
var threeSum = function (nums) 
    let ans = [];
    // 排序:nums从小到大
    nums.sort((a, b) => (a - b));

    for (let i = 0; i < nums.length; i++) 
        let l = i + 1, r = nums.length - 1;
        if (nums[i] > 0) continue;
        if (i && nums[i] === nums[i - 1]) continue;

        while (l < r) 
            let temp = nums[i] + nums[l] + nums[r];
            if (temp > 0) 
                r--;
            
            else if (temp < 0) 
                l++;
            
            else 
                ans.push([nums[i], nums[l], nums[r]]);
                let tempp = nums[l];
                while (nums[l] === tempp) 
                    l++;
                
                tempp = nums[r];
                while (nums[r] === tempp) 
                    r--;
                
            
        
    
    return ans;
;

18. 四数之和

https://leetcode.cn/problems/4sum/

注意i、j的去重。

/**
 * @param number[] nums
 * @param number target
 * @return number[][]
 */
var fourSum = function (nums, target) 
    nums.sort((a, b) => (a - b));
    let ans = [];

    for (let i = 0; i < nums.length; i++) 
        // 去重:判断左边界
        if (i && nums[i] === nums[i - 1]) continue;
        for (let j = i + 1; j < nums.length; j++) 
            if (j > i + 1 && nums[j] === nums[j - 1]) continue;
            let t1 = nums[i] + nums[j];

            let l = j + 1, r = nums.length - 1;
            while (l < r) 
                let t2 = nums[l] + nums[r];
                if (t1 + t2 > target) 
                    r--;
                
                else if (t1 + t2 < target) 
                    l++;
                
                else 
                    ans.push([nums[i], nums[j], nums[l], nums[r]]);
                    let temp = nums[l];
                    while (nums[l] === temp) 
                        l++;
                    
                    temp = nums[r];
                    while (nums[r] === temp) 
                        r--;
                    
                
            
        
    
    return ans;
;

相关题目

26.删除排序数组中的重复项

https://leetcode.cn/problems/remove-duplicates-from-sorted-array/

/**
 * @param number[] nums
 * @return number
 */
var removeDuplicates = function (nums) 
    let i = 0;
    for (let j = 0; j < nums.length; j++) 
        if (!i || nums[j] != nums[i - 1]) 
            nums[i++] = nums[j];
        
    
    return i;
;

283.移动零

https://leetcode.cn/problems/move-zeroes/

/**
 * @param number[] nums
 * @return void Do not return anything, modify nums in-place instead.
 */
var moveZeroes = function (nums) 
    let i = 0;
    for (let j = 0; j < nums.length; j++) 
        if (nums[j] != 0) 
            nums[i++] = nums[j];
        
    
    for (; i < nums.length; i++) 
        nums[i] = 0;
    
;

844.比较含退格的字符串

https://leetcode.cn/problems/backspace-string-compare/

/**
 * @param string s
 * @param string t
 * @return boolean
 */

var fun = function (s) 
    let ans = '';
    let cnt = 0;
    for (let i = s.length - 1; i >= 0; i--) 
        if (s[i] == '#') 
            cnt++;
        
        else 
            if (cnt) 
                cnt--;
             else 
                ans += s[i];
            
        
    
    return ans;


var backspaceCompare = function (s, t) 
    let ss = fun(s);
    let tt = fun(t);
    // console.log(ss,tt)
    if (ss == tt) return true;
    else return false;
;

977.有序数组的平方

https://leetcode.cn/problems/squares-of-a-sorted-array/

/**
 * @param number[] nums
 * @return number[]
 */
var sortedSquares = function (nums) 
    // 平方:两边大,中间小
    let ans = [];
    for (let i = 0, j = nums.length - 1; i <= j;) 
        let ii = nums[i] * nums[i], jj = nums[j] * nums[j];
        if (ii >= jj) 
            ans.unshift(ii); i++;
         else 
            ans.unshift(jj); j--;
        
    
    return ans;
;

以上是关于LeetCode与《代码随想录》双指针篇:做题笔记与总结-JavaScript版的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode与《代码随想录》链表篇:做题笔记与总结-JavaScript版

LeetCode与《代码随想录》哈希表篇:做题笔记与总结-JavaScript版

LeetCode与《代码随想录》栈与队列篇:做题笔记与总结-JavaScript版

LeetCode与《代码随想录》数组篇:做题笔记与总结-Java版

LeetCode与《代码随想录》字符串篇:做题笔记与总结-JavaScript版

LeetCode与《代码随想录》二叉树篇:做题笔记与总结-JavaScript版