### Word splitting (medium)

- dynamic programming
- ”Whether leetcode "can be composed and decomposed into - whether" l "is stored in the dictionary and subsequent substrings can be composed when the step size is 1, etc; When the step size is 2, "le" is stored in the dictionary and subsequent substrings can be composed, etc; When the step size is 3, whether "lee" is stored in the dictionary and subsequent substrings can be composed, etc
- By analogy, let dp[i] indicate whether the string of s[0, i] can be composed, and let j be the current step size. If dp[i] is true, dp[j] must be true and s[j, i - j] must exist in the dictionary, so the recursive formula is derived: dp[i] = dp[j] & & istrue (s[j, i - j])

var wordBreak = function(s, wordDict) { // It is convenient to use the has method to determine whether the Set object is true or not let set = new Set(wordDict) // If the filled array is false, why should there be one more bit? let dp = new Array(s.length + 1).fill(false) dp[0] = true // Boundary: define empty string as true // i can be equal to s.length. What's the connection with one more bit above? Think about substr usage for (let i = 1; i <= s.length; i++) { // The word length cannot exceed the length of the substring currently judged for (let j = 0; j < i; j++) { if (dp[j] && set.has(s.substr(j, i - j))) { dp[i] = true break } } } return dp[s.length] };

### Consolidation interval (medium)

- Sorting: as long as the interval set is arranged in ascending order according to the left end point, the merging conditions can be simplified - as long as the left end point of the second interval falls in the first interval and the right end point is not, the two intervals can be merged. If not sorted? The merging area is separated by a certain distance, and the judgment conditions will be much more complex

var merge = function(intervals) { intervals.sort((a, b) => a[0] - b[0]) let res = [] let cur = intervals[0] for (let i = 1; i < intervals.length; i++) { // Judge whether the left end point of the current interval falls in the interval of cur if (cur[1] >= intervals[i][0]) { // Judge right endpoint if (cur[1] < intervals[i][1]) { // merge cur = [cur[0], intervals[i][1]] } } else { res.push(cur) cur = intervals[i] } } res.push(cur) return res };

### Linked list sorting (medium)

- Merge sort
- The time complexity of O(nlogn) is required for advanced level. Merge sort, quick sort and heap sort are naturally thought of. Merge sort is selected here
- In fact, this problem can be decomposed into the combination of merging and sorting + double pointer finding the midpoint of single linked list + merging two sorting linked lists. On the whole, the writing of changing the question is still too complicated. It would be better to use more JS ready-made methods

var sortList = function(head) { if (!head || !head.next) return head let left = head let right let slow = head, fast = head.next // Double pointer to find the midpoint of the linked list while (fast && fast.next) { slow = slow.next fast = fast.next.next } right = slow.next slow.next = null // Merging two sorting linked lists let merge = (l, r) => { let head = new ListNode(0) let cur = head while (l !== null && r !== null) { if (l.val < r.val) { cur.next = l l = l.next } else { cur.next = r r = r.next } cur = cur.next } if (l) cur.next = l if (r) cur.next = r return head.next } // Merge and sort return merge(sortList(left), sortList(right)) };

### Adjust the array order so that odd numbers precede even numbers (simple)

- Violence: for loop judgment parity

var exchange = function(nums) { let a1 = [], a2 = [] for (let i = 0; i < nums.length; i++) { nums[i] % 2 === 0 ? a2.push(nums[i]) : a1.push(nums[i]) } return [...a1, ...a2] }

- Double pointers: Double pointers point to both ends and traverse to the middle (asynchronous). As long as the left pointer points to an even number and the right pointer points to an odd number, two numbers will be exchanged

var exchange = function(nums) { for (let i = 0, j = nums.length - 1; i < j;) { if (nums[i] % 2 === 0 && nums[j] % 2 === 1) { [nums[i], nums[j]] = [nums[j], nums[i]] i++ j-- continue } nums[i] % 2 === 0 ? j-- : i++ } return nums };

If you think it's helpful to you, give it a compliment~

Anyway, posting doesn't make money. Make friends~

If you need to reprint, please indicate the source foolBirdd