@@ -362,3 +362,104 @@ arr = mergeSort(arr, 0, arr.length - 1, []);
362362console .log (arr); // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
363363// 平均时间复杂度 O(nlogn) 最好情况 O(nlogn) 最坏情况 O(nlogn) 空间复杂度 O(n) 稳定排序
364364```
365+
366+ ## 最大的第K个数
367+
368+ ``` javascript
369+ const adjust = (index , arr ) => {
370+ let changeIndex = - 1 ;
371+ const n = arr .length ;
372+ if (index < n && arr[index] < arr[index * 2 + 1 ]){
373+ changeIndex = index * 2 + 1 ;
374+ [arr[index], arr[index * 2 + 1 ]] = [arr[index * 2 + 1 ], arr[index]];
375+ }
376+ if (index < n && arr[index] < arr[index * 2 + 2 ]){
377+ changeIndex = index * 2 + 2 ;
378+ [arr[index], arr[index * 2 + 2 ]] = [arr[index * 2 + 2 ], arr[index]];
379+ }
380+ if (changeIndex !== - 1 ) adjust (changeIndex, arr);
381+ }
382+
383+ const find = (arr , k ) => {
384+ for (let n = arr .length , i = n- 1 ; i>= 0 ; -- i) adjust (i, arr);
385+ for (let i= 0 ; i< k- 1 ; ++ i){
386+ arr .shift ();
387+ [arr[0 ], arr[arr .length - 1 ]] = [arr[arr .length - 1 ], arr[0 ]];
388+ adjust (0 , arr);
389+ }
390+ console .log (arr .shift ());
391+ }
392+
393+ const k = 3 ;
394+ const arr = [4 , 3 , 5 , 1 , 6 , 2 , 7 , 8 ];
395+ find (arr, k);
396+ ```
397+
398+ ## 防抖和节流
399+
400+ ``` javascript
401+ // 防抖
402+ const d = function (time , funct , ... args ){
403+ let timer = null ;
404+ return () => {
405+ clearTimeout (timer);
406+ timer = null ;
407+ timer = setTimeout (() => funct (... args), time);
408+ }
409+ }
410+ window .onscroll = d (1000 , (a ) => console .log (a), 1 );
411+ ```
412+ ``` javascript
413+ // 节流
414+ const t = function (time , funct , ... args ){
415+ let timer = null ;
416+ return () => {
417+ if (! timer){
418+ funct (... args);
419+ timer = setTimeout (() => {
420+ clearTimeout (timer);
421+ timer = null ;
422+ }, time);
423+ }
424+ }
425+ }
426+ window .onscroll = t (1000 , (a ) => console .log (a), 1 );
427+
428+ ```
429+
430+ ## 继承
431+ ``` javascript
432+ // 寄生组合继承
433+ function Parent (from ){
434+ this .name = " parent" ;
435+ this .say = function (){
436+ console .log (this .name );
437+ }
438+ this .from = from;
439+ }
440+ function Child (from ){
441+ Parent .call (this , from);
442+ this .name = " child" ;
443+ }
444+ let f = function (){};
445+ f .prototype = Parent .prototype ;
446+ Child .prototype = new f ();
447+ Child .prototype .construce = Child;
448+
449+ let child = new Child (" child" );
450+ child .say (); // child
451+ console .log (child .from ); // child
452+ ```
453+
454+ ## 十进制转二进制
455+ ``` javascript
456+ (function (num ){
457+ num = num >> 0 ;
458+ const target = [];
459+ while (num){
460+ target .unshift (num % 2 );
461+ num = (num / 2 ) >> 0 ;
462+ }
463+ console .log (target .join (" " ));
464+ })(10 );
465+ ```
0 commit comments