Algorithm selection of double pointer

(1) Define
double pointers, which means that in the process of traversing objects, instead of using a single pointer for ordinary access, two pointers in the same direction (fast and slow pointers) or opposite directions (collision pointers) are used for scanning. so as to achieve the corresponding purpose.

(2) Usage
(1) Colliding pointers are applicable to ordered arrays, that is to say, when you encounter a problem and are given an ordered array, you should immediately think of using colliding pointers to solve the problem.

Example: Given two sorted arrays, merge the two arrays into one. Input Output Sample Inputs are two arrays and their respective lengths m and n. The length of the first array is extended to m + n, and the extra n bits are filled with 0. The title requires merging the second array into the first array without opening up additional space. Input: nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3 Output: nums1 = [1,2,2,3,5, 6]

    <script>
        var nums1 = [1, 2, 3, 0, 0, 0],
            m = 3,
            nums2 = [2, 5, 6],
            n = 3;
        // 方法一:
        // function getArray(nums1, m, nums2, n) {
        //     nums1.splice(m, nums1.length - m, ...nums2);
        //     nums1.sort(function(a, b) {
        //         return a - b;
        //     });
        //     return nums1;
        // }
        // var newArray = getArray(nums1, m, nums2, n);
        // console.log(newArray);
        //方法二:双指针
        function getArray(nums1, m, nums2, n) {
            while (n > 0) {
                let k = m + n - 1;
                if (nums1[m - 1] > nums2[n - 1]) {
                    nums1[k] = nums1[m - 1];
                    m--;
                } else {
                    nums1[k] = nums2[n - 1];
                    n--;
                }
            }
            return nums1;
        }
        var arr = getArray(nums1, m, nums2, n);
        console.log(arr);
    </script>

(2) The fast and slow pointers  are also double pointers, but the two pointers start to traverse the array from the same side, define the two pointers as and respectively 快指针(fast), 慢指针(slow)and the two pointers move with different strategies until the values ​​of the two pointers are equal (or other special conditions).

 

Example: Find two numbers in an increasing array of integers such that their sum is a given value. There is one and only one pair of solutions. Input and output example: The input is an array (numbers) and a given value (target). The output is the position of two numbers, counting from 1. Input: numbers = [2,7,11,15], target = 9 Output: [1,2]

    <script>
        function countNumber(arr, target) {
            // 方法:用一个空数组来储存两个相加值为target的,因为可能存在多种情况
            // 思路:遍历数组,判断是否有值arr[j]和target-arr[i]相等,如果有,就说明这两个数是我们要找的
            let list = [];
            for (let i = 0; i < arr.length; i++) {
                for (let j = i + 1; j < arr.length; j++) {
                    if (arr[j] == target - arr[i]) {
                        list.push(i + 1, j + 1);
                    }
                }
            }
            return list;
        }
        let arr = [2, 7, 11, 15],
            num = 9;
        let list = countNumber(arr, num);
        console.log(list);
    </script>

Guess you like

Origin blog.csdn.net/DIUDIUjiang/article/details/126998625