1、冒泡排序

依次比较两个相邻的元素,如果顺序错误就把他们交换过来。

  1. const arr = [911,520,888,666,555,2323]
  2. function bubbleSort(arr){
  3. let temp;
  4. for (let i = 0; i < arr.length-1; i++) {
  5. for (let j = 0; j < arr.length-1-i; j++) {
  6. if (arr[j]>arr[j+1]) {
  7. temp = arr[j];
  8. arr[j]= arr[j+1];
  9. arr[j+1] = temp;
  10. }
  11. }
  12. }
  13. return arr;
  14. }
  15. console.log(bubbleSort(arr));

2、递归(深拷贝)

  1. function deepClone(obj){
  2. if (typeof obj !== 'object' || obj == null) {
  3. return obj;
  4. }
  5. var result = obj instanceof Array?[]:{};
  6. for (const k in obj) {
  7. if (obj.hasOwnProperty(k)) {
  8. result[k] = deepClone(obj[k]);
  9. }
  10. }
  11. return result;
  12. }
  13. const obj = {
  14. name:"cq",
  15. age:18,
  16. still:{
  17. web:'vue',
  18. others:'react'
  19. }
  20. }
  21. const newObj = deepClone(obj)
  22. newObj.still.web = 'angular'
  23. console.log(newObj);
  24. console.log(obj);

3、数组去重

  1. function unique1(arr){
  2. for (let i = 0; i < arr.length; i++) {
  3. for (let j = i+1; j < arr.length; j++) {
  4. if(arr[i] === arr[j]){
  5. arr.splice(j,1);
  6. j--;
  7. }
  8. }
  9. }
  10. return arr;
  11. }
  12. function unique2(arr){
  13. var result = [];
  14. for(let i=0;i<arr.length;i++){
  15. if(result.indexOf(arr[i]) === -1){
  16. result.push(arr[i])
  17. }
  18. }
  19. return result;
  20. }
  21. function unique3(arr){
  22. return Array.from(new Set(arr))
  23. }
  24. const arr = [1,2,3,1,1,2,4]
  25. console.log(unique1(arr));
  26. console.log(unique2(arr));
  27. console.log(unique3(arr));

4、快速排序

(1)在数据集之中,选择一个元素作为”基准”(pivot)。
(2)所有小于”基准”的元素,都移到”基准”的左边;所有大于”基准”的元素,都移到”基准”的右边。
(3)对”基准”左边和右边的两个子集,不断重复第一步和第二步,直到所有子集只剩下一个元素为止。

  1. const arr = [911,520,888,666,555,2323]
  2. //阮一峰版
  3. function quickSort(arr){
  4. if (arr.length<=1) return arr;
  5. var pivotIndex = 0;
  6. var pivot = arr.splice(pivotIndex,1)[0];
  7. var left = [];
  8. var right = [];
  9. for (let i = 0; i < arr.length; i++) {
  10. if(arr[i]<pivot){
  11. left.push(arr[i])
  12. }else{
  13. right.push(arr[i])
  14. }
  15. }
  16. return quickSort(left).concat([pivot],quickSort(right))
  17. }
  18. //新版
  19. function quickSort(arr) {
  20. function swap(arr, i, k) {
  21. let temp = arr[i];
  22. arr[i] = arr[k];
  23. arr[k] = temp;
  24. }
  25. // 数组分区,左小右大
  26. function partition(arr, left, right) {
  27. let storeIndex = left;
  28. let pivot = arr[right]; // 直接选最右边的元素为基准元素
  29. for(let i = left; i < right; i++) {
  30. if(arr[i] < pivot) {
  31. swap(arr, storeIndex, i);
  32. storeIndex++; // 交换位置后,storeIndex 自增 1,代表下一个可能要交换的位置
  33. }
  34. }
  35. swap(arr, storeIndex, right); // 将基准元素放置到最后的正确位置上
  36. return storeIndex;
  37. }
  38. function sort(arr, left, right) {
  39. if(left > right) {
  40. return;
  41. }
  42. let storeIndex = partition(arr, left, right);
  43. sort(arr, left, storeIndex - 1);
  44. sort(arr, storeIndex + 1, right);
  45. }
  46. sort(arr, 0, arr.length - 1);
  47. return arr;
  48. }
  49. console.log(quickSort(arr));

5、翻转字符串

  1. function reverseString(str) {
  2. str=str.split('').reverse().join('');
  3. return str;
  4. }
  5. reverseString("hello");

6、翻转二叉树

这是一道很经典的二叉树问题。显然,我们从根节点开始,递归地对树进行遍历,并从叶子节点先开始翻转。如果当前遍历到的节点 root 的左右两棵子树都已经翻转,那么我们只需要交换两棵子树的位置,即可完成以root 为根节点的整棵子树的翻转。

  1. var invertTree = function(root) {
  2. if (root === null) {
  3. return null;
  4. }
  5. const left = invertTree(root.left);
  6. const right = invertTree(root.right);
  7. root.left = right;
  8. root.right = left;
  9. return root;
  10. };
  11. //测试
  12. var obj={
  13. 'id':'4',
  14. 'left':{
  15. 'id':'2',
  16. 'left':{
  17. 'id':'1',
  18. 'left':null,
  19. 'right':null
  20. },
  21. 'right':{
  22. 'id':'3',
  23. 'left':null,
  24. 'right':null
  25. }
  26. },
  27. 'right':{
  28. 'id':'7',
  29. 'left':{
  30. 'id':'6',
  31. 'left':null,
  32. 'right':null
  33. },
  34. 'right':{
  35. 'id':'9',
  36. 'left':null,
  37. 'right':null
  38. }
  39. }
  40. }
  41. invertTree(obj);
  42. console.log(obj);

leetcode链接:https://leetcode-cn.com/problems/invert-binary-tree/

7、防抖节流

防抖:事件触发后,在n秒内,如果再次触发事件,则会重新计时

  1. debunce(fn,delay){
  2. let timer = null;
  3. return function(){
  4. if(timer){
  5. clearTimeout(timer)
  6. }
  7. timer = setTimeout(() =>{
  8. fn()
  9. },delay)
  10. }
  11. }

节流:事件触发后,在n秒内,再次触发事件,都会无效,只会触发一次

  1. throttle(fn,delay){
  2. let lock = true
  3. return function () {
  4. if(!lock){
  5. return
  6. }
  7. lock = false
  8. setTimeout(() => {
  9. fn()
  10. lock = true
  11. },delay)
  12. }
  13. },

8、打印 out5 in0 in1 in2 in3 in4

  1. for (var i = 0; i < 5; i++) {
  2. (function(i){
  3. setTimeout(async function() {
  4. console.log('in', i);
  5. }, 1000);
  6. })(i)
  7. }
  8. console.log('out', i);

9、判断一个{}为空

  1. var a = {}
  2. console.log(JSON.stringify(a) === '{}');