zoukankan      html  css  js  c++  java
  • 牛客网面试题

    请给Array本地对象增加一个原型方法,它用于删除数组条目中重复的条目(可能有多个),返回值是一个包含被删除的重复条目的新数组。

    Array.prototype.distinct = function() {
        var ret = [];
        for (var i = 0; i < this.length; i++)
        {
            for (var j = i+1; j < this.length;) {   
                if (this[i] === this[j]) {
                    ret.push(this.splice(j, 1)[0]);
                } else {
                    j++;
                }
            }
         }
         return ret;
    }
    //for test
    alert(['a','b','c','d','b','a','e'].distinct());
    

    请填充代码,使mySort()能使传入的参数按照从小到大的顺序显示出来。

    function mySort() {
        var tags = new Array();//使用数组作为参数存储容器
        请补充你的代码
        return tags;//返回已经排序的数组
    }
     
    var result = mySort(50,11,16,32,24,99,57,100);/传入参数个数不确定
    console.info(result);//显示结果
    
    //参考答案
    for(var i = 0;i < arguments.length;i++) {
            tags.push(arguments[i]);
        }
        tags.sort(function(compare1,compare2) {
            return compare1- compare2;
        });
    
    tags = Array.prototype.slice.call(arguments).sort(function(x,y){
    		return x-y;
    });
    
  • 相关阅读:
    HDU 1022 Train Problem I
    HDU 1702 ACboy needs your help again!
    HDU 1294 Rooted Trees Problem
    HDU 1027 Ignatius and the Princess II
    HDU 3398 String
    HDU 1709 The Balance
    HDU 2152 Fruit
    HDU 1398 Square Coins
    HDU 3571 N-dimensional Sphere
    HDU 2451 Simple Addition Expression
  • 原文地址:https://www.cnblogs.com/EvanVue/p/6566224.html
Copyright © 2011-2022 走看看