leetcode简单(数组,字符串,链表):[66, 67, 70, 83, 121, 141, 160, 169, ,206, 338]

66. 加一

var plusOne = function(digits) {
    // 1. 从最后一个值进行开始遍历
    // 2. 只要是%10===0,就直接继续遍历,否则返回值即可
    // 3. 最后,遍历完还没给出结果的,那肯定是 10,这种所以要往前加1
    for (let i = digits.length - 1; i >= 0; i--) {
        digits[i] = digits[i] + 1
        digits[i] = digits[i] % 10
        if (digits[i] !== 0) {
            return digits
        }
    }
    digits.unshift(1)
    return digits
};

67. 二进制求和

var addBinary = function(a, b) {
    var len = Math.max(a.length, b.length)
    a = a.padStart(len, 0).split('')
    b = b.padStart(len, 0).split('')

    var result = []
    var isTen = 0
    for (let i = len - 1; i >= 0; i--) {
        var tmpA = parseInt(a[i])
        var tmpB = parseInt(b[i])
        if (tmpA + tmpB + isTen == 3) { // 1+1+1
            result.unshift(1);
            isTen = 1
        } else if (tmpA + tmpB + isTen == 2) { // 1+1+0
            result.unshift(0);
            isTen = 1
        } else if (tmpA + tmpB + isTen == 1) { // 1+0+0
            result.unshift(1);
            isTen = 0
        } else { // 0+0+0
            result.unshift(0);
            isTen = 0
        }
    }
    if (isTen == 1) result.unshift(1);
    return result.join('')
};

70. 爬楼梯

var climbStairs = function(n) {
    const path = []
    //  path[n] = path[n-1] + path[n-2]   // 最终到达顶的总数 =  差一步 + 差两步的 和

    // 已知条件
    path[1] = 1
    path[2] = 2
    for (i = 3; i <= n; i++) {
        path[i] = path[i - 1] + path[i - 2]
    }
    return path[n]
};

83. 删除排序链表中的重复元素

var deleteDuplicates = function(head) {
    let curr = head
    while (curr !== null && curr.next !== null) {
        if (curr.val == curr.next.val) {
            curr.next = curr.next.next
        } else {
            curr = curr.next
        }
    }
    return head

};

121. 买卖股票的最佳时机

var maxProfit = function(prices) {
    var minPrice = prices[0]
    var maxProfit = 0
    for (let i = 0; i < prices.length; i++) {
        minPrice = prices[i] < minPrice ? prices[i] : minPrice
        maxProfit = prices[i] - minPrice < maxProfit ? maxProfit : prices[i] - minPrice

    }
    return maxProfit
};

141. 环形链表

var hasCycle = function(head) {
    // 使用快慢指针
    var fast = head;
    while (fast && fast.next) {
        head = head.next
        fast = fast.next.next
        if (head === fast) return true
    }
    return false
};

160. 相交链表

var getIntersectionNode = function(headA, headB) {
    var n1 = headA
    var n2 = headB

    while (n1 !== n2) {
        if (n1 === null) {
            n1 = headB
        } else {
            n1 = n1.next
        }
        if (n2 === null) {
            n2 = headA
        } else {
            n2 = n2.next
        }
    }
    return n1
};

169. 多数元素

var majorityElement = function(nums) {
    var keyMap = {}
    for (let i = 0; i < nums.length; i++) {
        if (!keyMap[nums[i]]) {
            keyMap[nums[i]] = 0
        }
        keyMap[nums[i]]++
    }
    var res = []
    for (const key in keyMap) {
        if (keyMap[key] > (nums.length / 2)) {
            res.push(key)
        }
    }
    return res
}

206. 反转链表

var reverseList = function(head) {
    var prev = null
    var curr = head
    var next = head
    while (curr !== null) {
        // next = curr.next // 由于是单项链表,所以需要占位
        // curr.next = prev // 实现反转
        // prev = curr
        // curr = next
        // 上述四步可简写为:
        [curr.next, prev, curr] = [prev, curr, curr.next]
    }
    return prev
};

338. 比特位计数

var countBits = function(n) {
    var res = []
    for (let i = 0; i <= n; i++) {
        var bin = i.toString(2)
        var tmp = bin.replace(/1/g, '').length
        res.push(bin.length - tmp)
    }
    return res
};
posted @ 2023-01-09 19:21  Echoyya、  阅读(36)  评论(0编辑  收藏  举报