### 01 - sum of two numbers - js

describe

Given an integer array numbers and a target value target, please find two subscripts in the array that add up to the target value, and the returned subscripts are arranged in ascending order.

Example 1 Input:[3,2,4],6 Return value:[2,3] Note: because 2+4=6 ，And the subscript of 2 is 2, and the subscript of 4 is 3, because of subscript 2 < Subscript 3, so output[2,3]

Idea: when the double-layer loop finds that the sum of two numbers is equal to the target value, the generation sequence returns two subscript values (note to skip the addition of itself and itself)

Code implementation:

function twoSum( numbers , target ) { // write code here for(let i = 0; i < numbers.length; i ++){ for(let j = 0; j < numbers.length; j ++){ if(i === j) break if(numbers[i] + numbers[j] === target){ if(i > j){ return [j + 1 , i + 1] }else{ return [i + 1 , j + 1] } } } } }

### 02 - judge whether it is a balanced binary tree - js

describe

Enter a binary tree with n nodes to judge whether the binary tree is a balanced binary tree.

Here, we only need to consider its balance, not whether it is a sorted binary tree

Balanced Binary Tree has the following properties: it is an empty tree or the absolute value of the height difference between its left and right subtrees does not exceed 1, and both the left and right subtrees are a Balanced Binary Tree.

Example 1 Input:{1,2,3,4,5,6,7} Return value: true

[thinking]: Recursively judge whether the height difference between the left and right subtrees of a node exceeds 1, and then judge each node

Code implementation:

function IsBalanced_Solution(pRoot){ if(pRoot == null) return true // The default empty tree is also a balanced binary tree // Gets the tree height of a node function getNodeTreeHigh(node){ if(node == null) return 0 let leftHigh = getNodeTreeHigh(node.left) let rightHigh = getNodeTreeHigh(node.right) return Math.max(leftHigh,rightHigh) + 1 } // Get the left subtree height and right subtree height of each node let left = getNodeTreeHigh(pRoot.left) let right = getNodeTreeHigh(pRoot.right) if(Math.abs(left - right) > 1) return false return IsBalanced_Solution(pRoot.left) && IsBalanced_Solution(pRoot.right) }

### 03 - Poker shunzi - js

describe

Now there are two pairs of playing cards, five playing cards at random from the playing cards. We need to judge whether shunzi is right or not.

There are the following rules:

- A is 1, J is 11, Q is 12, K is 13, and a cannot be regarded as 14
- Big and small Wang are 0, 0 can be regarded as any card
- If the given five cards can form a shunzi (that is, the five cards are continuous), it will output true, otherwise it will output false.
- The data guarantees that there are 5 numbers in each group, and each group contains at most 4 zeros. The number value of the array is [0, 13]

Example 1 Input:[6,0,2,0,4] Return value: true Note: the two zeros in the middle are regarded as 3 and 5. Namely:[6,3,2,5,4] So these five cards are[2,6]Interval continuous, output true

Idea: 1.The key is to get the maximum and minimum values in the array, only in the maximum value max - minimum value min Less than 4 can satisfy the condition of forming CIS. 2.Calculate the number of zeros in the array 3.De duplication of data other than 0 in the array 4.Whether the length of the array after de duplication plus the number of zeros is equal to 5 and max - min Less than 4.If these two conditions are met, it means that it can form a cis son and return true If it is not satisfied, it means that it cannot form a cis son return false

Code implementation:

function IsContinuous(numbers){ // write code here let zeroNums = 0 let newNums = [] for(let i = 0; i < numbers.length; i ++){ if(numbers[i] === 0){ zeroNums ++ }else { newNums.push(numbers[i]) } } let max = newNums[0] let min = newNums[0] for (let i = 0; i < newNums.length; i++) { if(min > newNums[i]){ min = newNums[i] } if (max < newNums[i]) { max = newNums[i] } } newNums = [...new Set(newNums)] if(newNums.length + zeroNums == 5 && max - min < 5) return true else return false }

### 04 - step jumping - js

describe

A frog can jump up one step or two steps at a time. Find out the total number of jumping methods for the frog to jump up an n-step step (different results are calculated in different order).

Example 1 Input: 2 Return value: 2 Note: there are two ways for frogs to jump up two steps: jump one step first, then one step, or directly jump two steps. So the answer is 2

Idea: you can only jump one or two steps at a time, and there is only one step and two steps. arrive n At the step, you can n-1 You can also jump from n-2 Jump over, so n= (n - 1)-(n - 2). Every step before that is the same result, so it can be calculated recursively.

Code implementation:

function jumpFloor(number){ // write code here if(number === 0) return 0 if(number === 1) return 1 if(number === 2) return 2 else { return jumpFloor(number - 1) + jumpFloor(number -2) } }

### 05 - the first common node of two linked lists & & the penultimate k nodes in the linked list - js

Both topics are the same idea. Traverse all the data in the linked list and put it in a new array. After finding the target value, traverse the linked list again to perform the return operation

#### 5.1 the first common node of two linked lists

describe

Enter two acyclic one-way linked lists and find their first public node. If there is no public node, it returns null. (note that because the incoming data is a linked list, the prompt of error test data is displayed in other ways to ensure that the incoming data is correct)

Example 1 Input:{1,2,3},{4,5},{6,7} Return value:{6,7} Description: first parameter{1,2,3}Represents the non-public part of the first linked list and the second parameter{4,5}The representative is the non-public part of the second linked list and the last one{6,7}It represents the common part of two linked lists These three parameters will finally be assembled into two acyclic single linked lists with public nodes in the background

Code implementation:

function FindFirstCommonNode(pHead1, pHead2){ // write code here if(!pHead1 && !pHead2) return null let headArr1 = [] let headArr2 = [] let node1 = pHead1 let node2 = pHead2 while(node1){ headArr1.push(node1.val) node1 = node1.next } while(node2){ headArr2.push(node2.val) node2 = node2.next } let repeated = 0 for(let i = 0; i < headArr1.length; i ++){ for(let j = 0 ; j < headArr2.length ; j ++){ // Here, one more bit of data should be judged backward to prevent duplication of data at a single location if(headArr1[i] === headArr2[j] && headArr1[i + 1] === headArr2[ j + 1]){ repeated = headArr2[j] break } } if(repeated !== 0) break } if(repeated === 0) return null else { while(pHead1){ if(pHead1.val === repeated){ return pHead1 } pHead1 = pHead1.next } } }

#### 5.2 the last k nodes in the linked list

describe

Enter a linked list with length n, set the value of the element in the linked list as ai, and return the penultimate node in the linked list.

If the length of the linked list is less than k, please return a linked list with a length of 0.

Example 1 Input:{1,2,3,4,5},2 Return value:{4,5} Note: return the penultimate node 4, and the system will print all the following nodes for comparison.

Code implementation:

function FindKthToTail( pHead , k ) { // write code here if(pHead == null) return pHead let nodeArr = [] let node = pHead while(node){ nodeArr.push(node.val) node = node.next } let indexVal = nodeArr[nodeArr.length - k] while(pHead){ if(indexVal === pHead.val){ return pHead } pHead = pHead.next } }