编程题

// 防抖
function debounce(fn, wait) {    
    var timeout = null;    
    return function() {        
        if(timeout !== null)   clearTimeout(timeout);        
        timeout = setTimeout(fn, wait);    
    }
}

// 节流throttle代码(定时器):
var throttle = function(func, delay) {            
    var timer = null;            
    return function() {                
        var context = this;               
        var args = arguments;                
        if (!timer) {                    
            timer = setTimeout(function() {                        
                func.apply(context, args);                        
                timer = null;                    
            }, delay);                
        }            
    }        
}  

颜色转换

function rgb2hex(sRGB) {
    var reg=new RegExp(/^rgb\d1,3,\s∗\d1,3,\s∗\d1,3$/);
    if(reg.test(sRGB)){
        var num = sRGB.slice(4,-1).split(',');
        var res = "#";
        for(var i=0;i=0 && num[i]<=255){
                num[i]=('0'+parseInt(num[i]).toString(16)).slice(-2);
                res += num[i];
            }else{
                return sRGB;
            }
        }
        return res;
    }else{
        return sRGB;
    }
}

多维数组转一维数组

var arr = ['mu','zi',['dig',['big','love']]]
function flatten(arr){ 
  var res = []; 
  for(var i=0;i

编程题_第1张图片

选择排序
编程题_第2张图片
直接插入排序
编程题_第3张图片
二分插入排序
编程题_第4张图片

  • 0\ 在Object原型上扩展一个方法实现深度克隆
    Extend a method on the Object prototype to achieve deep cloning
  • 1\ 实现两个字符串相加(大数字)
    Implement two string additions (large numbers)
  • 2\ 冒泡排序
    bubble sorting
  • 3\ 快速排序
    Quick sort
  • 4\ 二叉树反转
    binary tree reversal
  • 5\ [1,2,3]变成[[1,2],[1,3],[2,1],[2,3],[3,1],[3,2]]
    高级版: 可以变成 [1, 2, 3] [1, 3, 2] [2, 1, 3] [2, 3, 1][3, 1, 2][3, 2, 1]
  • 6\ 去重
    Remove duplicates
  • 7\ 字符串1中替换掉字符串2中的字符
    Replace the characters in string 2 in string 1.
  • 8\ 反转字符串
    reverse string
  • 9\ 字符串中返回数字和连续数字 a1b23c45=>[1,23,45]
    Return numbers and consecutive numbers in a string
  • 10\ add(3)(4)(5)的调用方式 ,相加返回12
  • 11\ 手写继承 原型链/es6
    Handwriting inheritance
  • 12\ 去掉字符串的空格
    Remove the space of the string
  • 13\ 二叉树遍历(前序,中序,后序,广度)
  • 14\ 二分查找
    Binary tree traversal (pre-order(Preorder traversal), in-order, post-order)
  • 15\ n的阶乘后有几个0
    For an integer n find number of trailing zeroes in n! .
    -16\ ** 十进制转二进制**
  • 十大排序算法 冒泡(两个for,第二个,jlength-i-1),选择排序(O(n²),里面的for,j=i+1),插入排序(Insertion Sort),希尔排序(Shell Sort,归并排序(Merge Sort),堆排序(Heap Sort),计数排序(Counting Sort),桶排序(Bucket Sort),基数排序(Radix Sort)
    https://www.cnblogs.com/dushao/p/6004883.html

0\在Object原型上扩展一个方法实现深度克隆

编程题_第5张图片

1\实现两个字符串相加(大数字)

编程题_第6张图片

2\冒泡排序

编程题_第7张图片

3\快速排序

编程题_第8张图片

3\二叉树反转

编程题_第9张图片

5[1,2,3]变成[[1,2],[1,3],[2,1],[2,3],[3,1],[3,2]]

slice() 从数组中返回选定元素,这里slice()相当于新建一个数组.
编程题_第10张图片
高级版: 可以变成 [1, 2, 3] [1, 3, 2] [2, 1, 3] [2, 3, 1][3, 1, 2][3, 2, 1]

编程题_第11张图片

6\去重

function uniq(array){
var temp = []; //一个新的临时数组
for(var i = 0; i < array.length; i++){
if(temp.indexOf(array[i]) == -1){
temp.push(array[i]);
}
}
return temp;
}

7\字符串1中替换掉字符串2中的字符

var str=“They are staudents”;
var str1=“aeiou”;

for(var i=0;i {
str=str.split(str1[i]).join('');
}

console.log(str);

8\反转字符串

编程题_第12张图片

9\字符串中返回数字和连续数字 a1b23c45=>[1,23,45]

如果只是转数字或者什么的 用正则就好a.replace(/[^\d+]/g,"")或者a.replace(/[^0-9]/g,"")
编程题_第13张图片

10\add(3)(4)(5)的调用方式 ,相加返回12

temp.toString的重写只是为了函数不执行时能够返回最后运算的结果值,所以这个地方是可以任意修改的,你让它返回什么它就返回什么,
编程题_第14张图片

11\手写继承

  1. 原型链继承
    编程题_第15张图片
    2)构造函数继承 利用call,
    但是不好是父函数原型链上的东西不能被继承(原型方法不能用!):
    var fish = new Fish('小鱼'); fish.run()//undefiend
    解决方案:在子类继承父类后,加上一句 :Fish.prototype = Object.create(Sea.prototype); Fish.prototype.constructor = Fish;即可输出fish.run(),不直接用Fish.prototype = Sea.prototype;是因为会把Fish的constructor也变成Sea的.
    编程题_第16张图片
    3)ES6继承

编程题_第17张图片

12\去掉字符串前后所有空格:

代码如下:

 function Trim(str)
     { 
         return str.replace(/(^\s*)|(\s*$)/g, ""); 
 }

说明:如果使用jQuery直接使用$.trim(str)方法即可,str表示要去掉前后所有空格的字符串。
去除全部空格:.replace(/\s/g,"")

13\二叉树遍历(前序,中序,后序,广度(一层一层),深度(先中后序))

前序:根-左-右
编程题_第18张图片
中序:左-根-右
编程题_第19张图片
后序: 右-左-根
编程题_第20张图片

14\ 二分查找

编程题_第21张图片

15 n的阶乘后有几个0

var num =0
    for(var i=1;i<=inputData;i++){
        let count = 0;
        let j = i
        while( j>0 && j%5 ==0){
            j = j/5;
            count++
        }
        num += count
    }
    process.stdout.write("" + num + "\n");

15 十进制转二进制

var arr = [];
		var num = 44;
		if(num == 1){
			arr[0] = 1;
		}else{
			while(num != 0){
				var tmp =num % 2;
				arr.push(tmp);
				num = (num - tmp) / 2;
			}
		}
		console.log(arr.reverse().join(""));

你可能感兴趣的:(编程题)