前言
本週花了大量的時間再研究算法,這個東西不是一會兒就能搞懂的,可能花了大量的時間的看不到見效,也就是得不到一些有效的反饋,這也是我如今比較苦惱的問題,是繼續先研究數據結構和算法,仍是直接刷leetcode呢?我也不清晰,兩眼摸黑,能怎麼辦呢?學唄,沒準一年後就入門了,天天啃一點,沒準哪天忽然領悟了,後面就會輕鬆一些html
特色算法
- 找重複
- 找變化
- 找邊界
全部的循環均可以改爲遞歸數組
查找必定區域的範圍值 const f2=(a,b)=>{ if(a>b) return; console.log(a); return f2(a + 1, b); }; 對數組求和 const f1=(a,b=0)=>{ if(a.length-1==b) return a[b]; return a[b]+f1(a,b+1); }; console.log(f1([1, 2, 3])); 字符串倒序 const s1=(a,b=0)=>{ if(b == 0 ) return a.charAt(0); return a.charAt(b) + s1(a, b - 1); }; console.log(s1('123456789',2)); const reverse(str)=>{ if(str.length<=1) return str; return reverse(str.slice(1))+str[0]; } 一串字符串,是否有兩個字符相等 const isPalindrome=(str)=>{ if(str.length) return true; if(str.length==2) return str[0] == str[1]; if(str[0]==str.slice(-1)){ return isPalindrome(str.slice(1)) } }; console.log(isPalindrome('aka')); 數組扁平化 const flatten = arr => arr.reduce((acc, val) => { return acc.concat(Array.isArray(val) ? flatten(val) : val); }, []); 接受一個對象,這個對象的值是偶數,讓其想加 let obj = { a: 1, b: 2, c: {d: 3}, e: {f: {g: 6}}, t: {f: {g: {f:10}}}, }; const objSum = obj => { let sum = 0; for (let key in obj) { if (typeof obj[key] == 'object') { sum += objSum(obj[key]) } else if (typeof obj[key] == 'number' && obj[key] % 2 == 0) { sum += obj[key]; } } return sum }; console.log(objSum(obj)); const reduceSum=obj=> Object.values(obj). reduce((acc,val)=> typeof val=='object'? acc+reduceSum(val): acc+val,0);
講A柱子上n-1個盤子暫時移到B柱子上數據結構
A柱子只剩下最大的盤子,把他移到目標柱子C上數據結構和算法
最後再將B柱子上的n-1個盤子移到目標C上ui
參數 最大編號 初始柱子 輔助的柱子 目標的柱子 const hanoi = (n, a, b, c) => { if (n == 1) { //a到c // move(a, c); console.log('Move' + n + ' ' + a + ' ' + c); } else { // a繞過c放到b hanoi(n - 1, a, c, b); // move(a, c); console.log('Move' + n + ' ' + a + ' ' + c); //b繞過a放到c hanoi(n - 1, b, a, c); } }; console.log(hanoi(3, 'a', 'b', 'c'));
左邊找(遞歸)code
中間比htm
右邊找(遞歸)對象
//在必定範圍內L R 求最大值 const getMax=(arr,L,R)=>{ if(L==R) return arr[L]; let mid=Math.floor((L+R)/2); let maxLeft = getMax(arr, L, mid); let maxRight = getMax(arr, mid + 1, R); return Math.max(maxLeft, maxRight); }; let arr = [1, 3, 44, 5, 6, 7, 8]; console.log(getMax(arr, 0, arr.length-1));
舉例說明 [9, 3, 6, 4] [9, 3] | [6, 4] [9] | [3] [6] | [4] [3, 9] | [4, 6] [3, 4, 6, 9] 咱們把數組拆分紅多個最小的塊,能夠利用slice方法來實現 // 排序合併 const merge=(left,right)=>{ let result=[]; while (left.length > 0 && right.length > 0) { if (left[0] < right[0]) { result.push(left.shift()) }else{ result.push(right.shift()) } } /*把長度不同的鏈接起來*/ return result.concat(left).concat(right) } //拆開 const mergeSort=array=>{ if(array.length<2) return array; let mid = Math.floor(array.length / 2) let left = array.slice(0, mid); let right = array.slice(mid); return merge(mergeSort(left), mergeSort(right)) } console.log(mergeSort([1, 2, 3, 4, 200, 1, 2, 3]))
每個元素都像一個黑盒子blog
不須要準備的知道每一個元素到底是什麼
只須要可以兩兩比較他們的大小
插入 比較 冒泡 合併 快速 分塊
[1,2,3].sort((a,b)=>a-b)
相似合併排序--相同點-- 分治策略
先找到一個基準點(通常指數組的中部),而後數組被該基準點分爲兩部分,依次與該基準點數據比較,若是比它小,放左邊;反之,放右邊。
const quickSort=arr=>{ if(arr.length<=1) return arr; let pivotIndex = Math.floor(arr.length / 2) //取中心點 let pivot=arr.splice(pivotIndex,1) let left=[]; let right=[]; for (let i = 0; i < arr.length; i++) { if (arr[i] < pivot) {//小的放左邊 left.push(arr[i]) }else{ right.push(arr[i]) } } return quickSort(left).concat(pivot, quickSort(right)) }
非比較型排序
通常用於範圍小於100的排序
時間複雜度爲O(n),空間複雜度爲數組的數字範圍
太變態了,沒法理解!!!!
每個元素對應位置上數字的大小進行排序:個位與個位,十位與十位...
看得我頭皮發麻,算了,來點輕鬆的東西
去重(直接保留剛開始的第0項) const uniq=array=>array.sort((a, b) => a - b) .filter((item,index,array)=>!index||item!==array[index-1])
"Attribute" 是在html中定義的,而"property" 是在DOM上定義的
說明區別 <input type="text" value="Hello"> const input = document.querySelector('input'); console.log(input.getAttribute('value')); // Hello console.log(input.value); // Hello console.log(input.getAttribute('value')); // Hello console.log(input.value); // Hello World!