zoukankan      html  css  js  c++  java
  • 渡一 15-1 数组去重(上下)

    封装type方法

    //封装type方法
    
    typeof([])             --array
    typeof({})             --object
    typeof(function...)  --object
    typeof(new Number)   --object number
    typeof(123)            --number
    
    
    function myTypeof(target){
        //1.分两类 原始值 引用值
        //2.区分引用值
        var ret = typeof(target);
        var template = {
            "[object Array]" : "array",
            "[object Object]" : "object",
            "[object Number]" : "number - object",
            "[object Boolean]" : "boolean - object",
            "[object string]" : "string - object"
        }
        if(target === null){
            return null;
        }else if(ret == "object"){
            //分类 数组 对象 包装类 Object.prototype.toString
            // return template[object.prototype.toString(target)]
            var str = Object.prototype.toString.call(target);
            return template[str];
        }else{
            return ret;
        }
    }

    数组去重

    //要求在原型链上编程
    var arr=[1,1,3,3,5,5,"a","a"]
    Array.prototype.unique = function(){
        //利用对象
        var obj={},
            arr=[],
            len = this.length;
        for(var i=0;i<len;i++){
            if(!obj[this[i]]){             //obj里this[i]没有属性值
                obj[this[i]] = "abc";   //随便赋值
                arr.push(this[i])        //更新到数组里去
            }
        }
        return arr;
    }

    渡一 15-2 数组去重(下)复习

    //包装类
    var str = "abc";
    str.length; //包装类 new String("abc").length;
    
    var num = 123;
    num.abc="abc";
    new Number(num).abc = "abc";-->delete
    //原型
    var demo = {
        lastName : "deng"
    }
    var obj = Object.create(demo);
    // var obj = Object.create(prototype,definedProperty(可配置属性);
    obj = {
        __proto__:demo
    }
    
    //属性的可配置性
    var num = 123;
    delet num //false
    
    window.name = 123;
    delete name
    window.name //undefined
    
    delete 不能删除var后的window属性
    //this call
    function test(){
        var num = 123;
        function a(){
    
        }
    }
    test()
    AO{
        arguments:{},
        this:window,
        num:undefined,
        a:function(){}
    }
    test.call({name:"deng"}) this-->{name:"deng"}
    
    //1.预编译 this-->window
    //2.谁调用的 this 指向谁
    //3.call apply
    //4.全局 this --> window
    
    
    var name = "window";
    var obj = {
        name : "obj",
        say : function(){
            console.log(this.name);
        }
    }
    obj.say();//obj
    obj.say().call(window)//window
    var fun = obj.say;
    fun()//window
    //闭包除了return还有这种方式
    var obj = {};
    function a(){
        function b(){
    
        }
        obj.fun = b;
    }

    undefined,NaN 不能和数字比较,不能进行隐式转换

    function Person(name,age,sex){
        var a = 0;
        this.name = name;
        this.age = age;
        this.sex = sex;
        function sss(){
            a++;
            document.write(a);
        }
        this.say = sss;
    }
    var oPerson = new Person();
    oPerson.say(); //1
    operson.say(); //2
    var oPerson1 = new Person();
    oPreson1.say(); //1
    (function(x){
        var x;//var x;无法delete;
        delete x;
        return x;
    })(1)
    
    var h = function a(){ //表达式执行就销毁
        return 23;
    }
    console.log(typeof a()); //error:a is not defined
    //返回星期几
    function retDate(date){
        var arr = ["一","二","三"];
        var ret = arr[date-1];
        ret === undefined ? return "error" : return ret;
    }

    1.一个字符串[a-z]组成,请找出该字符串第一个只出现一次的字母;

    function findFist(){
        var str = "dfkdfajdkfdksfsdfosfjosdbfjsfoie" //a
        var obj={},count=1;
        for(var i=0;i<str.length;i++){
            /*obj={
                d:1,
                f:1
            }*/
            
            !obj[str[i]] ? obj[str[i]] = count : obj[str[i]]++;
    
        }
        for(var prop in obj){
            if(obj[prop] == 1){
                return prop;
            }
        }
    }
    
    var num = findFist(); //a

    2.字符串去重;

    function del(){
        var str = "dfkdfajdkfdksfsdfosfjosdbfjsfoie" //a
        var obj={},arr=[],count=1;
        for(var i=0;i<str.length;i++){
            if(!obj[str[i]]){
                obj[str[i]] = count;
                arr.push(str[i])
            }
        }
        // console.log(arr.join(""))
        return arr.join("")
    }
    del()
  • 相关阅读:
    Codeforces Round #200 (Div. 2) E. Read Time(二分)
    Codeforces Round #160 (Div. 2) D. Maxim and Restaurant(DP)
    TC SRM 593 DIV1 250
    TC SRM 593 DIV2 1000
    HDU 2825 Wireless Password(AC自动机+DP)
    Codeforces Round #203 (Div. 2)
    TC SRM 591 DIV2 1000
    HDU 4758 Walk Through Squares(AC自动机+DP)
    CF 346B. Lucky Common Subsequence(DP+KMP)
    HDU 4753 Fishhead’s Little Game(DFS)
  • 原文地址:https://www.cnblogs.com/lisa2544/p/15347299.html
Copyright © 2011-2022 走看看