Given
[1,3],[2,6],[8,10],[15,18]
, return[1,6],[8,10],[15,18]
.數組關鍵就是
a[1]>=b[0]
也就是array[i-1][1]>=array[i][0]
app
const merge = array => { array.sort((a, b) => a[0] - b[0]) for (let i = 1; i < array.length; i++) { let minLeft = Math.min(array[i - 1][0], array[i][0]); let maxRight = Math.max(array[i - 1][1], array[i][1]); if (array[i - 1][1] >= array[i][0]) { array[i - 1] = [minLeft, maxRight] array.splice(i, 1) i-- } } return array } console.log(merge([[1, 3], [8, 10], [2, 6], [15, 18]])) //[ [ 1, 6 ], [ 8, 10 ], [ 15, 18 ] ]
str.length % s.length
spa
const repeated = s => { for (let i = 1; i < s.length; i++) { //從第一個字符串開始 let str = s.slice(0, i) //他的上限就是兩個數相等 // if (str.length % s.length) { if (!(str.length == s.length)) { let emptyStr = '' let repeatNum = s.length / str.length //算重複的字符,而後判斷相等 for (let j = 0; j < repeatNum; j++) { emptyStr += str } if (emptyStr == s) { return true } } } return false } console.log(repeated('abab'))
//走樓梯 小孩最多走1,2,3個,問總樓梯數n,有多少種走法 Dp f(x,y)=f(x,y-1)+f(x-1,y) const recursion = n => { if (n < 0) return 0 if (n == 0 || n == 1) return 1 if (n == 2) return 2 return recursion(n-1)+recursion(n-2)+recursion(n-3) } console.log(recursion(3)) //4
//f(x,y)=f(x-1,y)+f(x,y-1) const solve = (x, y) => { if (x == 1 || y == 1) return 1 return solve(x - 1, y) + solve(x, y - 1) } console.log(solve(2, 3))
題目 [1,2,3,4] 1 2 3 4 "1(2(4))(3()())" 兩個括號在一塊兒就能夠省略 "1(2(4))(3)" [1,2,3,null,4] 1 2 3 4 "1(2()(4))(3)" let tree={ val:1, left:{ val:2, left:{ val:3 }, right:{ val:4 } }, right:{ val:5, right:{ val:6 } } } const tree2str = t => { if (t == null) { return '' } if (t.left == null && t.right == null) { return t.val + '' } if (t.left == null) { return t.val + '()' + '(' + tree2str(t.right) + ')' } if (t.right == null) { return t.val + '(' + tree2str(t.left) + ')' } return t.val + '(' + tree2str(t.left) + ')' + '(' + tree2str(t.right) + ')' } console.log(tree2str(tree))
n 組合 1 () 2 ()() (()) 3 ()()() (()()) ((())) const printPar = (l, r,s='', result=[]) => { if (l == 0 && r == 0) { return result.push(s) } if (l > 0) { printPar(l-1,r,s+'(',result) } if (r > 0 && l < r) { printPar(l,r-1,s+')',result) } return result } console.log(printPar(3, 3))
// 'abcab' => 'abc' // 'abcabc' => 'abc' // 'abcdababab' =>'abcd' //計數排序 const solution = (s) => { if (s == null || s.length == 0) { return 0 } let hash = Array.from({length:256},v=>0); let begin = 0, end = 0; let repeat = 0, res = 0; while (end < s.length) { //a++>0 先執行>再執行後++ //第一個判斷是把重複的放到數組中,repeat是計算重複了多少次 if (hash[s.charCodeAt(end++)]++ > 0) { //s.charCodeAt(end) 查找這個數的索引 放在數組中+1 //end++ repeat++是計算重複的次數 repeat++ } while (repeat > 0) { //這個是把重複的次數減去 if (hash[s.charCodeAt(begin++)]-- > 1) { repeat-- } } //總次數減去不重複的 5-2=3 res = Math.max(res, end - begin) } console.log(hash) return res } console.log(solution('abcab'))
輸入: 1 / \ 0 2 L = 1 R = 2 輸出: 1 \ 2 輸入: 3 / \ 0 4 \ 2 / 1 L = 1 R = 3 輸出: 3 / 2 / 1 二叉搜索樹,進行範圍限制 const trimBST = (root, l, r) => { if (root == null) { return null } if (root.val < l) { return trimBST(root.right, l, r) } if (root.val > r) { return trimBST(root.left, l, r) } root.left = trimBST(root.left, l, r) root.right = trimBST(root.right, l, r) return root }
//雙指針 const maxArea = height => { let left = 0, right = height.length - 1, maxVal = 0; while (left < right) { let contain = (right - left) * Math.min(height[left], height[right]) maxVal = Math.max(contain, maxVal) if (height[left] >= height[right]) { right-- } else { left++ } } return maxVal } console.log(maxArea([1, 2, 3, 4, 5, 5, 1, 2, 3, 4]))
() //true ()[]{} //true {[]} //true 邏輯 使用棧遍歷輸入字符串 若是當前字符串爲左括號時,則將壓入棧中 若是遇到右括號時: * 若是棧不爲空且爲對應的左半括號,則取出棧頂元素,繼續循環 * 若此時棧爲空,則直接返回false * 若不爲對應的左半括號,直接返回false const isValid = s => { let valid = true; const stack = []; const mapper = { '{': '}', '[': ']', '(': ')' }; for (let i in s) { if (['(', '[', '{'].indexOf(s[i]) > -1) { stack.push(s[i]) } else { //stack.pop出棧後,比較是否有反括號 if (s[i] !== mapper[stack.pop()]) { return false } } } if(stack.length>0) return false return valid } console.log(isValid('()'))
思路指針
使用快慢指針來記錄遍歷的座標code
- 開始時這兩個指針都指向第一個數字
- 若是兩個指針指的數字相同,則塊指針向前走一步
- 若是不一樣,則兩個指針都向前
- 當快指針走完整個數組後,慢指針當前的座標+1就是數組中不一樣數字的個數
const removeDuplicates = nums => { let slow = 0; for (let i = 0; i < nums.length; i++) { if (nums[i] !== nums[slow]) { slow++ nums[slow] = nums[i] } } return slow+1 } console.log(removeDuplicates([1, 2,3,4,4,4,4,4,5,6,7,8]))
const merge = (num1, num2) => { let ret = []; while (num1.length || num2.length) { if (num1.length == 0) { ret.push(num2.shift()) continue } if (num2.length == 0) { ret.push(num1.shift()) continue } if (num1[0] >= num2[0]) { ret.push(num2.shift()) } else { ret.push(num1.shift()) } } return ret } 簡潔版 const merge = (nums1, m, nums2, n) => { let i1 = m - 1, i2 = n - 1, k = m + n - 1; while (i1 >= 0 && i2 >= 0) { if (nums1[i1] < nums2[i2]) { nums1[k--] = nums2[i2--] } else { nums1[k--] = nums1[i1--] } } while (i2 >= 0) { nums1[k--] = nums[i2--] } return nums1 }
const maxDepth=root=>{ if(!root) return 0 if(!root.left&&!root.right) return 1 return 1+Math.max(maxDepth(root.left),maxDepth(root.right)) }