New week, continue to study.

1. Yang Hui Triangle

Given a non-negative integer numRows, generate the former numRows of the Yanghui triangle. In Yang Hui’s triangle, each number is the sum of the numbers on its upper left and upper right.

The new column is calculated from the previous column, and the result is calculated from the corresponding number of rows

var generate = function(numRows) { let result = [[1]]; let row = [1] for(let i = 0 ; i<numRows-1; i++){ let newRow = [] row.unshift(0); row.push(0) for(let j = 0; j<row.length-1; j++) { let num = row[j] + row[j+1]; newRow.push(num) } result.push(newRow); row = [...newRow] } return result };Copy the code

2. The best time to buy and sell stocks

Given an array prices, its ith element prices[I] represents the price of a given stock on day I. You can only buy the stock on one day and sell it on a different day in the future. Design an algorithm to calculate the maximum profit you can make. Return the maximum profit you can make from the transaction. If you can’t make any profit, return 0.

Find the minimum from the first element (the buy point), then find the maximum difference after the minimum, and only find the difference if a new minimum appears.

var maxProfit = function(prices) { let sum = 0; let min = prices[0]; for(let i = 0; i<prices.length-1; i++) { if(prices[i] <= min) { min = prices[i] for(let j = i+1; j<prices.length; j++){ if(prices[j] - prices[i] > sum){ sum = prices[j] - prices[i] } } } } return sum };Copy the code

3. Verify palindrome strings

Given a string, verify that it is a palindrome string, considering only alphanumeric characters, regardless of letter case. Note: In this case, we define an empty string as a valid palindrome string.

First, split the incoming argument into a string array consisting only of letters and numbers, and compare the first and last entries to see if they meet the palindrome condition.

var isPalindrome = function(s) { if(s === '') { return true; } let reg = /[^a-zA-Z0-9]/; let arr = s.toLowerCase().split('').filter(item => ! reg.test(item)) for(let i = 0; i<arr.length/2; i++) { if(arr[i] ! == arr[arr.length-i-1]) { return false; } } return true; };Copy the code

4. Sum of two numbers II – Enter ordered array

Given an array of integers in ascending order numbers, find two numbers that add up to the target number. The function should return the subscript values of these two numbers as an array of integers of length 2. The subscript of numbers starts at 1, so the answer array should be 1 <= answer[0] < answer[1] <= numbers. Length. You can assume that each input corresponds to a unique answer, and you can’t reuse the same elements.

Thinking: the questions refer to the comment on ideas, according to the characteristics of the ascending of the array by two Numbers and the result was compared with the target size a subscript to find qualified Numbers.

var twoSum = function(numbers, target) { for(let i = 0,j = numbers.length-1; i<j;) { if(target === numbers[i] + numbers[j]) { let arr = [i+1,j+1] return arr }else if(target < (numbers[i] + numbers[j])) {  j-- }else { i++ } } };Copy the code