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
Hello everyone, I’m Nezha. Nice to meet you ~~
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
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 ❤ ️ ~
An array of
- Arrays are the simplest in-memory data structures
- An array stores a series of values of the same data type, or you can store values of different types in an array
- use
push
Method that can add elements to the end of an array. It can add any element - use
unshift
Method to insert a value at the top of an array - use
pop
Method to delete the last element in the array - use
shift
Deletes the first element of the array
Add or remove elements at any location
Using the splice method, by specifying a location or index, you can remove elements of that location and number.
Example:
Numbers. The splice (5, 3); // drop numbers. Splice (5, 0, 1,2,3); // add the element numbers.splice(5, 3, 1,2,3) from index 5; // Three elements are removed from index 5Copy the code
2 d array
- Matrix example:
Function printMatrix(myMatrix) {for (var I =0; i<myMatrix.length; i++){ for (var j=0; j<myMatrix[i].length; j++){ console.log(myMatrix[i][j]); }}}Copy the code
For (var I =0; i<matrix3x3x3.length; i++){ for (var j=0; j<matrix3x3x3[i].length; j++){ for (var z=0; z<matrix3x3x3[i][j].length; z++){ console.log(matrix3x3x3[i][j][z]); }}}Copy the code
Array methods (methods used by array structures and algorithms)
concat
Joins two or more arrays and returns the resultevery
, runs the given function on each item in the array, returns true if the function returns true for each itemfilter
Returns an array of items that return true by running the given function on each item in the arrayforEach
, runs the given function on each item in the array. This method returns no valuejoin
Concatenate all array elements into a stringindexof
, returns the index of the first array element equal to the given argument, or if no element is found- 1
lastIndexOf
Returns the maximum value in the index of the element found in the array equal to the given parametermap
Returns an array of the results of each function call by running the given function on each item in the arrayreverse
Reversing the order of the elements in the array, the first element is now the last, and the last element is now the present
Some, run the given function on each item in the array. If any item returns true, it returns true. 12. It is possible to pass a function specifying the sorting method as argument 13. toString, which returns an array as a string
The every method iterates through each element of the array until it returns false; The some method iterates through each element of the array until the function returns true
Example:
ForEach (function(ex){console.log((x % 2 == 0)); });Copy the code
reduce
methods
The reduce method takes a function as a parameter, this function has four parameters: previousValue, currentValue, index and array.
Example:
numbers.reduce(function(previous,current,index){
return previous + current;
});
Copy the code
New array methods in Es6 and Es7:
@@iterator
Returns an iterator object containing key-value pairs of array elements that can be called synchronouslycopyWithin
Copies a list of elements in an array to the starting position specified in the same arrayentries
Object that contains all the key-value pairs of an array@@iterator
includes
Returns true if there is an element in the array, false otherwise.ES7
newfind
Retrieves an element from the array based on the condition given by the callback function, and returns the element if foundfindIndex
Retrieves an element from the array based on the condition given by the callback function, and returns the element’s index in the array if foundfill
Populate the array with static valuesfrom
Creates a new array based on the existing arraykeys
Object that contains all the indexes of an array@@iterator
of
Creates a new array based on the parameters passed invalues
Object that contains all the values in the array@@iterator
Using the new ES6 iterator (@@iterator)
ES6 adds an @@iterator attribute to Array that needs to be accessed through symbol. iterator
Example:
let iterator = numbers[Symbol.iterator](); console.log(iterator.next().value); // 1 console.log(iterator.next().value); // 2 console.log(iterator.next().value); // 3 console.log(iterator.next().value); // 4 console.log(iterator.next().value); / / 5Copy the code
The Entries method returns an @@iterator containing key-value pairs
Example:
let aEntries = numbers.entries(); // Get iterator console.log(aendies.next ().value); // Get iterator console.log(aendies.next ().value); // [0, 1] - Position 0 with the value of 1 console.log(aendies.next ().value); // [1, 2] - Position 1 is 2 console.log(aendies.next ().value); // [2, 3] - Position 2 has a value of 3Copy the code
The keys method returns an @@iterator containing an array index
Example:
let aKeys = numbers.keys(); // Get the iterator for the array index console.log(akeys.next ()); // {value: 0, done: false } console.log(aKeys.next()); // {value: 1, done: false } console.log(aKeys.next()); // {value: 2, done: false }Copy the code
If the done attribute is false, it means there are still iterable values. Otherwise, vice versa.
The @@iterator returned by the values method contains the values of the array
Example:
let aValues = numbers.values();
console.log(aValues.next()); // {value: 1, done: false }
console.log(aValues.next()); // {value: 2, done: false }
console.log(aValues.next()); // {value: 3, done: false }
Copy the code
The array. from method creates a new Array from an existing Array
Example:
Let copyNumbers = array. from(numbers);Copy the code
Filter value function:
let dada = Array.from(numbers, x=> (x % 2 == 0));
Copy the code
The array. of method creates a new Array based on the parameters passed in
Example:
let da1 = Array.of(1);
let da2 = Array.of(1, 2, 3, 4, 5, 6);
let da1 = [1];
let da2 = [1, 2, 3, 4, 5, 6];
Copy the code
Use this method to copy an existing array
Example:
let copyNumbers = Array.of(... da2);Copy the code
The fill method fills an array with static values
Example:
let numbersCopy = Array.of(1, 2, 3, 4, 5, 6); numbersCopy.fill(0); // [0, 0, 0, 0, 0, 0] numbersCopy.fill(2, 1); // [0, 2, 2, 2, 2, 2] let da = Array(6).fill(1); // [1, 1, 1, 1, 1]Copy the code
The copyWithin method copies a series of elements in an array to the starting position specified in the same array
Example:
let copyArray = [1, 2, 3, 4, 5, 6];
copyArray.copyWithin(0, 3);
// [4, 5, 6, 4, 5, 6]
copyArray = [1, 2, 3, 4, 5, 6];
copyArray.copyWithin(1, 3, 5);
// [1, 4, 5, 4, 5, 6]
Copy the code
search
indexOf
Method returns the index of the first element that matches the argumentlastIndexOf
Returns the index of the last element that matches the argument
Differences between find and findIndex -ECMAScript 6
find
Method returns the first value that satisfies the condition; Can not find,find
Returns theundefined
findIndex
Method returns the index of the value in the array, not found,findIndex
Return 1
Use the includes method -ecmascript 7
The toString and join
- Prints all the elements in the array as a string
Example:
console.log(numbers.toString());
var numbersString = numbers.join('-');
console.log(numbersString);
Copy the code
53. Maximum suborder sum
1. Title Description
Given an integer array nums, find a contiguous subarray with the maximum sum (the subarray contains at least one element) and return the maximum sum.
Example 1: input: nums = [-2,1,-3,4,-1,2,1,-5,4] output: 6 explanation: the maximum sum of consecutive subarrays [4,-1,2,1] is 6. Example 2: Input: nums = [1] Output: 1 Example 3: Input: nums = [0] Output: 0 Example 4: Input: nums = [-1] Output: -1 Example 5: Input: nums = [-100000] Output: -100000Copy the code
Second, train of thought analysis
Method 1: Greedy algorithm: if the sum before the current pointer element is less than 0, the sequence before the current element is discarded
Answer code
/** * @param {number[]} nums * @return {number} */ var maxSubArray = function(nums) { let res = nums[0]; let sum = 0; for(let i=0; i<nums.length; i++){ if(sum>0) { sum += nums[i]; }else { sum = nums[i]; } res = Math.max(res,sum); } return res; };Copy the code
Four,
Maximum suborder sum – solution!
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)
❤️ 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