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

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 ❤ ️ ~

The queue

Queue data structure, a queue is a set of ordered items that follow FIFO, first-in, first-out, or first-come, first-served principles. Much like a queue in real life (the most common example of a queue), the first person in line is served first.

Create queue:

Function Queue() {// Here are attributes and methods} // a data structure used to store the elements in the Queue: array let items = []; // Add one or more new items to the end of the queue enqueue(Element (s)) // Remove the first item in the queue and return the removed element dequeue() // Return the first element in the queue, which was first added and will be the first element removed, Front () returns true if the queue contains no elements, false if it does not. IsEmpty () returns the number of elements in the queue, similar to the size() property of the array.Copy the code

To add an element to a queue, new items can only be added to the end of the queue

The enqueue method is implemented

this.enqueue = function(element) {
 items.push(element);
};
Copy the code

Removes elements from the queue

To implement the dequeue method, shift removes the element stored in index 0 (the first position) from the array

this.dequeue = function() {
 return items.shift();
}
Copy the code

Looking at the queue header element, use the front method, which returns the first item in the queue

this.font = function() {
 return item[0];
};
Copy the code

To check whether the queue isEmpty, use the isEmpty method

this.isEmpty = function() {
 return item.length == 0;
};
Copy the code
this.size = function() {
 return items.length;
}
Copy the code

Print queue elements using the print method

this.print = function() {
 console.log(items.toSring());
};
Copy the code

The only difference between a queue and a stack is the dequeue method and the front method. This is due to the fifO and LIFO principles

Instantiation:

let queue = new Queue(); console.log(queue.isEmpty()); Enqueue ("jeskson"); // True queue.enqueue("jeskson"); Queue. Enqueue (Nezha);Copy the code

Implement the Queue class using ES6

Use WeakMap to hold the private items property and wrap the Queue class with an outer function (closure).

let Queue2 = (function () { const items = new WeakMap(); class Queue2 { constructor () { items.set(this, []); } enqueue(element) { let q = items.get(this); q.push(element); } dequeue() { let q = items.get(this); let r = q.shift(); return r; } return Queue2; }) ();Copy the code

Implement a priority queue:

function PriorityQueue() { let items = []; Function QueueElement (element, priority){// The element to be added to the queue // the priority in the queue this.element = element; this.priority = priority; } this.enqueue = function(element, priority){ let queueElement = new QueueElement(element, priority); let added = false; for (let i=0; i<items.length; i++){ if (queueElement.priority < items[i].priority){ items.splice(i,0,queueElement); added = true; break; } } if (! added){ items.push(queueElement); }}; this.print = function(){ for (let i=0; i<items.length; i++){ console.log(`${items[i].element} - ${items[i].priority}`); }}; // The other methods are the same as the default Queue}Copy the code

Circular queue

Function hotPotato (nameList, num){function hotPotato (nameList, num){let Queue = new Queue(); for (let i=0; i<nameList.length; I ++){queue. Enqueue (nameList[I]); } // Given a number, let eliminated = "; While (queue.size() > 1){// Remove an item from the queue and add it to the queue for (let I =0; i<num; i++){ queue.enqueue(queue.dequeue()); } // eliminate queue = query.eliminated (); } return queue.dequeue(); / / {5}}Copy the code

When we open a new TAB in the browser, a task queue is created, and each TAB is single-threaded for all tasks, called an event loop.

Browsers are responsible for multiple tasks, such as rendering HTML, executing JavaScript code, handling user interactions, executing and processing asynchronous requests, etc.

66. Plus one

1. Title Description

Given a non-negative integer represented by a non-empty array of integers, add one to that number.

The highest digit is stored at the beginning of an array, where each element stores only a single digit.

You can assume that this integer does not start with zero, except for the integer 0.

Example 1: Input: digits = [1,2,3] Output: [1,2,4] Explanation: The input array represents the number 123. Example 2: input: digits = [4,3,2,1] output: [4,3,2,2] explanation: the input array represents the number 4321. Example 3: Input: digits = [0] Output: [1]Copy the code

Second, train of thought analysis

If the last bit of the array is less than 9, add 1 to the last bit

If the last bit of the array is equal to 9, set that bit to 0 and generate a carry, then look at the last bit.

If the previous digit is less than 9, just add 1 to the tens place and return it

If the previous digit is equal to 9, set that digit to 0 and carry is generated, then look at the previous digit (hundreds)

Sum = arr[arr. Length - 1] + 1 // <9 arr[arr. Length - 1] = sum return arr[arr. Length - 1] = sum Result = new array with size of arr.length + 1 result[0] = 1 result[1]...... result[result.length - 1] = 0Copy the code

Answer code

/** * @param {number[]} @return {number[]} */ var plusOne = function(digits) digits.length-1; i>=0; i--){ if(digits[i] ! ==9){ digits[i]++ return digits; }else{// iS 9 digits[I] = 0}} let result = [1,...digits]; return result; };Copy the code
var plusOne = function (digits) { var carry = 1; // Carry for (var I = digits. Length-1; // Carry for (var I = digits. i > -1; i--) { if (carry) { var sum = carry + digits[i]; digits[i] = sum % 10; carry = sum > 9 ? 1:0; // Carry}} if (carry === 1) {digits.unshift(1); } return digits;} return digits;} return digits;} return digits;} return digits; };Copy the code

Four,

Plus one, queue problem solving

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