3

Today, I will three 16113838fdcb37 questions on with friends JS to write 16113838fdcb35 questions that you may not know about.

tip1 - ES6+

First intersperse a little knowledge: How is the JS submitted by LeetCode executed?

The code we submitted in Likou is run in the background of Likou, while the JS code in the background of Likou is run in the --harmony mode in node.

It looks like this:

node --harmony  index.js

The index.js is the code you submitted.

for example:

// 前面 LeetCode 会添加一些代码
function sum(a, b) {
  // you code
}

// 这里是 LeetCode 的测试用例
expect(sum(1, 2)).toBe(3);
expect(sum(1, 8)).toBe(9); // 如果测试用例不通过,则直接抛出错误给前端

Therefore, ES6 features are fully supported, so you can use it with confidence.

For example, we can use ES6's destructuring syntax to complete the exchange of two values in an array.

[a, b] = [b, a];

The following is the use of ES6 array destructuring syntax. For more ES6+, please refer to related documents.

tip2 - lodash

In LeetCode, lodash can be accessed directly through _ by default.

This is because LeetCode directly added lodash require. similar:

const _ = require("lodash");

// 前面 LeetCode 会添加一些代码
function sum(a, b) {
  // you code
  // 你的代码可以通过 _ 访问到 lodash 的所有功能。
}

// 这里是 LeetCode 的测试用例
expect(sum(1, 2)).toBe(3);
expect(sum(1, 8)).toBe(9); // 如果测试用例不通过,则直接抛出错误给前端

lodash has many useful functions that can be used directly. Xifa recommends you if you can write it by hand, then you can use the function provided by lodash with confidence.

For example, the array is flattened:

_.flatten([1, [2, [3, [4]], 5]]);
// => [1, 2, [3, [4]], 5]

Another example is deep copy:

var objects = [{ a: 1 }, { b: 2 }];

var deep = _.cloneDeep(objects);
console.log(deep[0] === objects[0]);
// => false

For more APIs, please refer to the official documentation.

tip3 - queue & priority-queue

In order to make up for the lack of JS's built-in data structure. In addition to the built-in data structure of JS, LeetCode platform also provides two additional data structures for JS. They are:

  • queue
  • priority-queue

Both of these data structures use the third-party datastructures-js implemented by 06113838fdcd3f. I have read the code and it is still easy to understand.

queue

LeetCode provides JS support for queues.

// empty queue
const queue = new Queue();

// from an array
const queue = new Queue([1, 2, 3]);

The implementation of queue also uses array simulation. But instead of directly using shift to delete head elements, the worst-case time complexity of using shift to delete is $O(n)$. Here it uses a marking technique, that is, every time the head element is deleted, it is not actually removed, but to mark that it has been removed.

The time complexity of this approach can be reduced to O(1)$. It's just that if you keep entering and leaving the team, the space complexity will be very high, because all the elements that have been dequeued will be retained. Therefore, it will perform shrinking (similar to array expansion) every time it dequeues more than half. In this way, the time complexity will increase to $O(logn)$, but the space will be saved.

For detailed usage, please refer to: https://github.com/datastructures-js/queue

In addition, I have implemented a set of queue by myself. I implemented it using a linked list. In theory, has a better complexity and . The time complexity of insertion and deletion is O(1), and there is no waste of space. The core code is 20 lines . But in actual use, the performance is not necessarily better. Why, can you think about it?

priority-queue

In addition to ordinary queues, LeetCode also provides a special queue-priority queue.

// empty queue with default priority the element value itself.
const numbersQueue = new MinPriorityQueue();

// empty queue, will provide priority in .enqueue
const patientsQueue = new MinPriorityQueue();

// empty queue with priority returned from a prop of the queued object
const biddersQueue = new MaxPriorityQueue({ priority: (bid) => bid.value });

Priority-queue API can refer to https://github.com/datastructures-js/priority-queue

In the same way, Western France has also realized the heap, you can refer to it.

It is worth mentioning that Xifa has also implemented some advanced functions of the heap. For details, please refer to indexed_priority_queue

Summarize

LeetCode's support for JS mainly includes:

  • ES6+ syntax support
  • Built-in lodash library, you can use the functions on it _
  • The built-in data structure supports queues and priority queues.

The data structure code of my own implementation mentioned in the article comes from js-algorithm-light, which is a lightweight JavaScript data structure and algorithm library. In order to allow friends who use JS to learn and use some commonly used data structures, I opened up this warehouse. The tentative goal is to benchmark all the built-in data structures and algorithms of Python.

Post the data structure that Xifa has implemented.

Ask for one-click triple support. If you like more, Xifa will arrange the next one immediately. Next time I will share that you don’t know about 16113838fdcf9a.


lucifer
5.3k 声望4.6k 粉丝