GithubHelp home page GithubHelp logo

hanxiaoluan / vdom Goto Github PK

View Code? Open in Web Editor NEW

This project forked from bubucuo/vdom

1.0 1.0 0.0 55 KB

精讲虚拟DOM,包括虚拟DOM构造、React与Vue的DIFF算法等。

License: MIT License

JavaScript 100.00%

vdom's Introduction

vdom

精讲虚拟 DOM,包括虚拟 DOM 构造、React 与 Vue 的 DIFF 算法等。

该下班了

function getSequence(arr) { // return [1, 2];

const n = arr.length;
// 路径,最不济也是有0个元素
const result = [0];

let resultLast;

// 复制一份arr,记录元素i的真实位置
const recordIndexOfI = arr.slice();

for (let i = 0; i < n; i++) {
  const arrI = arr[i];
  // 如果元素为0,证明当前位置上没有新元素,不需要就算到lis里,跳过即可
  if (arrI !== 0) {
    // result最后一个元素
    resultLast = result[result.length - 1];
    // 如果arrI大于lis中最后一个元素,那么当前元素与老li构成新的lis,这个时候把arrI插入到lis里即可
    if (arr[resultLast] < arrI) {
      // 如果
      recordIndexOfI[i] = resultLast;
      result.push(i);
      continue;
    }
    // 否则把arrI插入到lis中,因为lis是有序的,所以这个时候我们可以使用二分插入
    let left = 0,
      right = result.length - 1;
    while (left < right) {
      const mid = (left + right) >> 1;
      if (arr[result[mid]] < arrI) {
        // mid < 目标元素, 在右边
        left = mid + 1;
      } else {
        right = mid;
      }
    }

    // 要插入的位置在left
    if (arrI < arr[result[left]]) {
      if (left > 0) {
        // 如果left是有效位
      }
      //
      result[left] = i;
    }
  }
}

return result;

}

vdom's People

Contributors

bubucuo avatar

Stargazers

Roman avatar

Watchers

James Cloos avatar

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.