Source: making power button (LeetCode) | | to brush the topic card for stars ✨ | give a ❤ ️ attention, ❤ ️ thumb up, ❤ ️ encourages the author
[Opened] Task 1: Brush the question punch card * 10
Nezha’s life creed: If you like what you learn, there will be strong motivation to support it.
Learn programming every day, so that you can get a step away from your dream. Thank you for not living up to every programmer who loves programming. No matter how strange the knowledge point is, work with me to calm down the wandering heart and keep going. Welcome to follow me vx:xiaoda0423, welcome to like, favorites and comments
Time: March 1 ~ March 13
- Force buckle (LeetCode)- Sum of two numbers, valid parentheses, sum of two numbers | brush questions punch card – March 1
- LeetCode – Merges two ordered lists, removes duplicates in sorted arrays,JavaScript Notes | Flash cards – March 2
- Force Button (LeetCode)- Maximum suborder sum,JavaScript Data Structures and Algorithms (Arrays) | Brush questions punched -3 March
- Say something about CSS | Tech Review – March 4
- Force Buckle (LeetCode)- Stack, parenthesis generation | brush questions punch card – 5 March
- It wasn’t that hard! Vue mall development | technical review – on March 6
- Force buckle (LeetCode)- Plus one, queue | brush questions punch – 7 March
- JavaScript data structure of the chain table | technical review – March 8
preface
If this article is helpful to you, give ❤️ a follow, ❤️ like, ❤️ encourage the author, accept the challenge? Article public account launch, concern programmer Doraemon first time access to the latest article
❤ ️ cartridge ❤ ️ ~
Arrays, stacks, queues, linked lists
A collection of
A set is an unordered set of unique items, which can be understood as an array with neither repeating elements nor any notion of order.
- Union, intersection, difference
Function Set() {let items = {}; }Copy the code
add(value)
Adds a new item to the collectiondelete(value)
Removes a value from the collectionhas(value)
, if the value is in the collectiontrue
Otherwise returnfalse
clear()
Removes all items from the collectionsize()
, returns the number of elements contained in the collectionvalues()
Returns an array containing all the values in the collection
From the (value) method
this.has = function(value) {
return value in items;
};
Copy the code
this.has = function(value) {
return items.hasOwnProperty(value);
};
Copy the code
Implement the add method:
this.add = function(value) { if(! This.has (value)) {// Add value to the collection if it does not exist. Items [value] = value; return true; } return false; };Copy the code
When you add a value, store it as both a key and a value, because it makes it easier to find the value.
Implement the remove method:
This.remove = function(value) {if(this.has(value)) {// Delete items[value]; return true; } return false; };Copy the code
Example code for using the Set class:
let set = new Set();
set.add(1);
set.add(2);
Copy the code
Remove all values from the collection:
This. Clear = function() {items = {}};Copy the code
The size method
- The use of a
length
Variable whenever usedadd
orremove
Method when controlling it, like usingLinkedList
Kind of like - Example:
This.size = function() {// Return an array containing all the properties of the given Object. };Copy the code
- Example:
this.sizeLegacy = function() { let count = 0; For (let key in items) {// Iterate over all attributes of the items object if(items.hasownProperty (key)) // Check whether they are attributes of the object itself // If so, increment the value of the count variable ++count; } // At the end of the method, return count; };Copy the code
The values method extracts all the properties of the Items object and returns them as an array
this.values = function() {
let values = [];
for(let i=0, keys = Object.keys(items); i < keys.length; i++) {
values.push(items[keys[i]]);
}
return values;
};
Copy the code
this.valuesLegacy = function() { let values = []; If (items.hasownProperty (key)) {values.push(item[key]); if(items.hasownProperty (key)) {values.push(item[key]); } } return values; };Copy the code
Using the Set class
let set = new Set(0; set.add(1); console.log(set.values()); // Array type console.log(set.has(1)); // true console.log(set.size()); / / 1...Copy the code
Set operations
Given two sets
- Union, which returns a new set containing all the elements of both sets
- Intersection, which returns a new set containing elements in both sets
- Difference set, which returns a new set containing all elements that exist in the first set and not in the second set
- Subset, which verifies whether a given set is a subset of another set
- And set
This. Union = function(otherSet) {// let unionSet = new Set(); Let values = this.values(); // Get all values of the first set, iterate over and add them to the set representing the union. for(let i=0; i<values.length; i++) { unionSet.add(values[i]); } values = otherSet.values(); // Set 2 does the same thing for(let I =0; i<values.length; i++) { unionSet.add(values[i]); } return unionSet; }Copy the code
- intersection
This. Intersection = function(otherSet) {// Create a new Set instance so that it can return the shared elements let intersectionSet = new Set(); let values = this.vlues(); For (let I =0; i<values.length; i++) { if(otherSet.has(values[i])){ intersectionSet.add(values[i]); } } return intersectionSet; }Copy the code
- Difference set
A minus B, the x element exists in A and x does not exist in B.
This.differenceset = function(otherSet){// Get all values in both sets let differenceSet = new Set(); let values = this.values(); // for(let i=0; i<values.length; i++){ // if(! Otherset.has (values[I])){// Get all values that exist in set A but not in set B differenceset.add (values[I]); } } return differenceSet };Copy the code
- A subset of
Set A is A subset of set B:
This. Subset = function(otherSet){if (this.size() > otherset.size ()){return false; // If the current instance has more elements than the otherSet instance, it is not a subset} else {let values = this.values(); for (let i=0; i<values.length; I++){// to iterate over all elements in the collection if (! Has (values[I])){// Verify that these elements also exist in otherSet return false; // If any element does not exist in otherSet, that means it is not a subset, return false}} return true; // If all elements exist in otherSet, return true}};Copy the code
The Set class
let set = new Set(); set.add(1); // ES6's Set values method returns Iterator // instead of an array of values console.log(set.values()); // output @iterator console.log(set.has(1)); // Print true console.log(set.size); / / output 1Copy the code
ES6 Set class
let setA = new Set();
setA.add(1);
setA.add(2);
setA.add(3);
let setB = new Set();
setB.add(2);
setB.add(3);
setB.add(4);
Copy the code
- And set
let unionAb = new Set(); Unionab.add (x); for (let x of setA) unionab.add (x); for (let x of setB) unionAb.add(x);Copy the code
- intersection
Intersection = function(setA, setB) {let intersection = function(setA, setB) {let intersectionSet = new Set(); for (let x of setA) { if (setB.has(x)) { intersectionSet.add(x); } } return intersectionSet; }; let intersectionAB = intersection(setA, setB);Copy the code
Simple syntax:
intersectionAb = new Set([x for (x of setA) if (setB.has(x))]);
Copy the code
- Difference set
let difference = function(setA, setB) { let differenceSet = new Set(); for (let x of setA) { if (! Setb.has (x)) {// add only elements in the differenceSet that setA has and setB does not. } } return differenceSet; }; let differenceAB = difference(setA, setB);Copy the code
Simple syntax:
differenceAB = new Set([x for (x of setA) if (!setB.has(x))]);
Copy the code
Summary: JavaScript data structures – collections
Go back to my previous articles and you may get more!
- A qualified junior front-end engineer needs to master module notes
- Vue.js pen test questions Solve common business problems
- [Primary] Personally share notes of Vue front-end development tutorial
- A long summary of JavaScript to consolidate the front end foundation
- ES6 comprehensive summary
- Dada front-end personal Web share 92 JavaScript interview questions with additional answers
- [Illustrated, like collection oh!] Re-study to reinforce your Vuejs knowledge
- 【 Mind Mapping 】 Front-end development – consolidate your JavaScript knowledge
- 14 – even liver 7 nights, summed up the computer network knowledge point! (66 items in total)
- This was my first JavaScript primer
- LocalStorage and sessionStorage localStorage
- Drag and drop in HTML5
- Challenge the front-end HTTP/ECMAScript
- Must learn must learn – Audio and video
- 170 Interview questions + answer Learn to organize (conscience making)
- Front-end HTML5 interviewers and candidates ask and answer questions
- Ne Zha is sweeping the sea
- Tencent location service development applications
- [Advanced] The interviewer asked me how Chrome renders (6000 words)
- The interviewer started by asking me about Chrome’s underlying principles and HTTP protocol (swastika)
- Staying up late summed up the “HTML5 Canvas”
- This /call/apply/bind
- The HTTP/HTTPS/HTTP2 / DNS/TCP/classic problem
- Execute context/scope chain/closure/first-class citizen
- Web page creation basics
- Learn the summary of HTML5 finger front-end (suggested collection, illustrated)
❤️ follow + like + favorites + comments + forward ❤️, original is not easy, encourage the author to create better articles
Likes, favorites and comments
I’m Jeskson, thanks for your talent: likes, favorites and comments, and we’ll see you next time! ☞ Thank you for learning with me.
See you next time!
This article is constantly updated. You can search “Programmer Doraemon” on wechat to read it for the first time, and reply [information] there are materials of first-line big factories prepared by me, which have been included in this article www.dadaqianduan.cn/#/
Star: github.com/webVueBlog/…
This article is participating in the “Nuggets 2021 Spring Recruitment Campaign”, click to see the details of the campaign