leetcode简单(双指针):[88, 202, 345, 392, 455, 905, 922, 917, 925, 942]

[toc

88. 合并两个有序数组

var merge = function(nums1, m, nums2, n) {
    let A1 = nums1.slice(0, m)
    let A2 = nums2
    // 追加哨兵
    A1.push(Number.MAX_SAFE_INTEGER)
    A2.push(Number.MAX_SAFE_INTEGER)
    for (let k = 0, i = 0, j = 0; k < m + n; k++) {
        // 循环不变式
        //  k: 下一个写入位置
        //  i: A1中获回写位置
        //  j: A2中回写位置
        nums1[k] = A1[i] < A2[j] ? A1[i++] : A2[j++]
    }
};

202. 快乐数

var isHappy = function(n) {
    const squareSum = (n) => {
        let sum = 0
        while (n > 0) {
            let digit = n % 10
            sum += digit * digit
            n = Math.floor(n / 10)
        }
        return sum
    }
    // 判断是否有循环可以使用快慢指针,表示链表中有环
    let slow = n
    let fast = squareSum(n)

    while (slow != fast) {
        slow = squareSum(slow)
        fast = squareSum(squareSum(fast))
    }
    return slow === 1
};

345. 反转字符串中的元音字母

var reverseVowels = function(s) {
    let set = new Set(['a', 'e', 'i', 'o', 'u', 'A', 'E', 'I', 'O', 'U']);
    let arr = s.split('')
    let i = 0,
        j = s.length - 1
    while (i < j) {
        if (set.has(arr[i])) {
            if (set.has(arr[j])) {
                [arr[i], arr[j]] = [arr[j], arr[i]]
                i++
            }
            j--
        } else {
            i++
        }
    }
    return arr.join('')
};

392. 判断子序列

var isSubsequence = function(s, t) {
    if (s == '') return true

    let i = 0;
    let j = 0
    while (j < t.length) {
        if (s[i] === t[j]) {
            i++
        }
        if (i == s.length) {
            return true
        }
        j++
    }
    return false
};

455. 分发饼干

var findContentChildren = function(g, s) {
    let ng = g.sort((a, b) => a - b)
    let ns = s.sort((a, b) => a - b)
    let [i, j] = [0, 0]
    while (i < ng.length && j < ns.length) {
        if (ng[i] <= ns[j]) {
            i++
        }
        j++ // 饼干被消费或不满足,需递增
    }
    return i
};

905. 按奇偶排序数组

var sortArrayByParity = function(nums) {
    let i = 0;
    let j = nums.length - 1
    while (i <= j) {
        if (nums[j] % 2 == 0 && nums[i] % 2 != 0) { // 偶前奇后
            [nums[i], nums[j]] = [nums[j], nums[i]]
            i++
            j--
        } else if (nums[j] % 2 == 0) {
            i++
        } else if (nums[i] % 2 != 0) {
            j--
        } else {
            i++
            j--
        }
    }
    return nums
};

922. 按奇偶排序数组 II

var sortArrayByParityII = function(nums) {
    let j = 1;
    let o = 0
    let res = []
    nums.forEach(n => {
        if (n % 2 == 0) {
            res[o] = n
            o += 2
        } else {
            res[j] = n
            j += 2
        }
    })
    return res
};

917. 仅仅反转字母

var reverseOnlyLetters = function(s) {
    let reg = /[a-zA-Z]/
    let strArr = s.split('')
    let i = 0
    let j = strArr.length - 1
    while (i < j) {
        if (reg.test(strArr[i]) && reg.test(strArr[j])) {
            [strArr[i], strArr[j]] = [strArr[j], strArr[i]]
            i++
            j--
        } else if (reg.test(strArr[i])) {
            j--
        } else if (reg.test(strArr[j])) {
            i++
        } else {
            i++
            j--
        }
        console.log(strArr);
    }
    return strArr.join('')
};

925. 长按键入

var isLongPressedName = function(name, typed) {
    let i = 0
    let j = 0
    while (j < typed.length) {
        if (name[i] == typed[j]) {
            i++
        }
        if (name[i] != typed[j] && name[i - 1] != typed[j]) {
            return false
        }
        if (i == name.length) {
            let flag = typed.slice(j).split(name[name.length - 1]).join('')
            if (flag != '') {
                return false
            }
            return true
        }
        j++
    }
    return false
};

942. 增减字符串匹配

var diStringMatch = function(s) {
    let m = 0
    let n = s.length
    let res = []
    for (let i = 0; i <= s.length; i++) {
        if (s[i] == 'I') {
            res.push(m)
            m++
        } else {
            res.push(n)
            n--
        }
    }
    return res
};
posted @ 2023-01-17 21:05  Echoyya、  阅读(45)  评论(0编辑  收藏  举报