diff --git "a/0148. \346\216\222\345\272\217\351\223\276\350\241\250.md" "b/0148. \346\216\222\345\272\217\351\223\276\350\241\250.md" new file mode 100644 index 0000000..cd75084 --- /dev/null +++ "b/0148. \346\216\222\345\272\217\351\223\276\350\241\250.md" @@ -0,0 +1,55 @@ +# 148. 排序链表 + +给你链表的头结点 head ,请将其按 升序 排列并返回 排序后的链表 。 + + + +```js +/** + * 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 sortList = function(head) { + if (!head || !head.next) return head + + var slow = fast = head + while (fast.next && fast.next.next) { + slow = slow.next + fast = fast.next.next + } + var temp = slow.next + slow.next = null + slow = temp + + return merge(sortList(head), sortList(slow)) +}; + +// LC21 +var merge = function(l1, l2) { + const pre = new ListNode(-1) + let current = pre + while(l1 !== null && l2 !== null) { + if (l1.val > l2.val) { + current.next = l2 + l2 = l2.next + } else { + current.next = l1 + l1 = l1.next + } + current = current.next + } + if (l1 === null) { + current.next = l2 + } else { + current.next = l1 + } + return pre.next +}; +``` diff --git "a/0152. \344\271\230\347\247\257\346\234\200\345\244\247\345\255\220\346\225\260\347\273\204.md" "b/0152. \344\271\230\347\247\257\346\234\200\345\244\247\345\255\220\346\225\260\347\273\204.md" new file mode 100644 index 0000000..8c23876 --- /dev/null +++ "b/0152. \344\271\230\347\247\257\346\234\200\345\244\247\345\255\220\346\225\260\347\273\204.md" @@ -0,0 +1,26 @@ +# 152. 乘积最大子数组 + +给你一个整数数组 nums ,请你找出数组中乘积最大的连续子数组(该子数组中至少包含一个数字),并返回该子数组所对应的乘积。 + + + +--- + +```js +/** + * @param {number[]} nums + * @return {number} + */ +var maxProduct = function(nums) { + var min = max = result = nums[0] + for (var i = 1; i < nums.length; i ++) { + + var minTemp = min, maxTemp = max + + min = Math.min(nums[i] * maxTemp, nums[i], nums[i] * minTemp) + max = Math.max(nums[i] * minTemp, nums[i], nums[i] * maxTemp) + result = Math.max(result, max) + } + return result +}; +```