Code Monkey home page Code Monkey logo

daily's People

Contributors

yoluxi avatar

daily's Issues

function

函数定义的两种方式
1 表达式

const a = function() {
}

// 使用场景 递归  在堆栈跟踪函数
const a = function a() {
}

2 声明式

function a() {
}

递归函数就是使用了函数堆栈

闭包:

可以使内部函数的使用更安全

notice
如果内部函数跟外部函数使用同样的变量名  内部函数则无法访问到外部的变量

颜色分类

给定一个包含红色、白色和蓝色,一共 n 个元素的数组,原地对它们进行排序,使得相同颜色的元素相邻,并按照红色、白色、蓝色顺序排列。

此题中,我们使用整数 0、 1 和 2 分别表示红色、白色和蓝色。

注意:

不能使用代码库中的排序函数来解决这道题。

示例:

输入: [2,0,2,1,1,0]
输出: [0,0,1,1,2,2]

高阶组件

什么是高阶组件?

通过函数包装后返回的功能增强的组件
const EnhancedComponent = higherOrderComponent(WrappedComponent);

如何使用高阶组件?

高阶组件能解决什么问题?

  1. 解决横切关注点问题 (用来在多个组件**享代码)

高阶组件的使用场景(或现有库的使用)?

  1. 属性代理
  2. 反向继承

Redux的conect

与mixins有哪些区别?

mixins用来做什么

  1. 组件中添加工具方法
  2. 生命周期的继承,props和state的合并

有没有缺点?或者更好的方式?

约定

  1. 将与HOC不相关的props传递给被包裹组件
  2. 最大化可组合性
  3. 包装显示名称便于调试

注意事项

  1. 不要改变原始组件,使用组合
  2. 不要在render中使用
  3. 务必复制静态方法
  4. refs不会被传递

移除元素

给定一个数组 nums 和一个值 val,你需要原地移除所有数值等于 val 的元素,返回移除后数组的新长度。

不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。

元素的顺序可以改变。你不需要考虑数组中超出新长度后面的元素

示例 :

给定 nums = [3,2,2,3], val = 3,

函数应该返回新的长度 2, 并且 nums 中的前两个元素均为 2。

你不需要考虑数组中超出新长度后面的元素。

删除排序数组中的重复项 II

给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素最多出现两次,返回移除后数组的新长度。

不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。

示例

给定 nums = [0,0,1,1,1,1,2,3,3],

函数应返回新长度 length = 7, 并且原数组的前五个元素被修改为 0, 0, 1, 1, 2, 3, 3 。

你不需要考虑数组中超出新长度后面的元素。

删除排序数组中的重复项

给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度。

不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。

示例 :

给定 nums = [0,0,1,1,1,2,2,3,3,4],

函数应该返回新的长度 5, 并且原数组 nums 的前五个元素被修改为 0, 1, 2, 3, 4。

你不需要考虑数组中超出新长度后面的元素。

Immutable.js

优点:
1 降低的可变的复杂度
2 节省内存空间 不修改的属性共享 修改的属性隔离
3 便于处理undo/redo
4 fp

缺点:
1 与原生对象易混淆

移动零

给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。

示例:

输入: [0,1,0,3,12] 输出: [1,3,12,0,0]

说明:

  1. 必须在原数组上操作,不能拷贝额外的数组。
  2. 尽量减少操作次数

冒泡排序

function bubbleSort(arr) {
    console.time('冒泡排序耗时');
    var temp = 0
    for (var i = 0; i < arr.length - 1; i++) {
        let sortable = true
        for (var j = 0; j < arr.length - 1 - i; j++) {
            if (arr[j] > arr[j+1]) {
                temp = arr[j]
                arr[j] = arr[j+1]
                arr[j+1] = temp
                sortable = false
            }
        }
        if (sortable) {
            break
        }
    }
    console.timeEnd('冒泡排序耗时');
    return arr
}

插入排序

直接插入排序

function insertSort(arr) {
    console.time('直接插入排序')
    let current = 0
    let prevIndex = 0
    for ( var i = 0; i < arr.length; i++ ) {
        current = arr[i]
        prevIndex = i - 1 
        while(prevIndex >= 0 && arr[prevIndex] > current ) {
            arr[prevIndex + 1] = arr[prevIndex]
            prevIndex--
        }

        if (prevIndex + 1 != i) {
            arr[prevIndex + 1] = current
        }
    }
    console.timeEnd('直接插入排序')
    return arr
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.