[ Web Proxy ]
URL:
Viewing: https://raw.githubusercontent.com/wenzi0github/algorithm-by-javascript/master/README.md [Back]  [Original]

# algorithm by javascript

#  JavaScript 

 coding 

## 

### 



```javascript
import { gcd } from '@xiaowenzi/algorithm.js';

gcd(6, 4); // 2
```

## 

### 



```javascript
import { mergeSortedArray } from '@xiaowenzi/algorithm.js';

mergeSortedArray([1, 3, 5], [2, 4, 6, 8]); // [1, 2, 3, 4, 5, 6, 8]
```

### 



-  1 
-  2  2

```javascript
import { removeDuplicates } from '@xiaowenzi/algorithm.js';

removeDuplicates([1, 1, 2, 2, 2, 2, 1, 1, 3], 3); // [3]
```

## 

## 

### 

#### 

```javascript
import { isPrime } from '@xiaowenzi/algorithm.js';

isPrime(10); // false
isPrime(11); // true
```

#### 

```javascript
import { countPrimeEqualOrLessNum } from '@xiaowenzi/algorithm.js';

countPrimeEqualOrLessNum(20); // 8
countPrimeEqualOrLessNum(100); // 25
```

#### 

```javascript
import { getAllPrimesEqualOrLessNum } from '@xiaowenzi/algorithm.js';

getAllPrimesEqualOrLessNum(1); // []
getAllPrimesEqualOrLessNum(10); // [2, 3, 5, 7]
getAllPrimesEqualOrLessNum(20); // [2, 3, 5,7, 11, 13, 17, 19]
```

## 

### 

 true false

```javascript
import { hasCycleLinkedList } from '@xiaowenzi/algorithm.js';

const head = new ListNode(0);
const node1 = new ListNode(1);
const node2 = new ListNode(2);

head.next = node1;
node1.next = node2;
node2.next = head;

hasCycleLinkedList(head); // true
```

### 



```javascript
import { reverseLinkedList } from '@xiaowenzi/algorithm.js';

const head = new ListNode(0);
const node1 = new ListNode(1);
const node2 = new ListNode(2);

head.next = node1;
node1.next = node2;

const reverseHead = reverseLinkedList(head);
```

## 

### 



#### LeetCode 

LeetCode 

```javascript
import { array2binary } from '@xiaowenzi/algorithm.js';

const root = array2binary([3, 9, 20, null, null, 15, 7]);
```

#### 



```javascript
import { getTreeByPreOrder, getTreeByMidOrder, getTreeByPostOrder, getTreeByLevelOrder } from '@xiaowenzi/algorithm.js';

getTreeByPreOrder(root); // 
getTreeByMidOrder(root); // 
getTreeByPostOrder(root); // 
getTreeByLevelOrder(root); // 
```

#### 

 homebrew  Max Howell 

[leetcode-invert-binary-tree](https://www.xiabingbao.com/algorithm/2015/06/17/invert-binary-tree.html)

```javascript
import { reverseTree } from '@xiaowenzi/algorithm.js';

reverseTree(root); // root
```

#### 

```javascript
import { isSymmetricTree } from '@xiaowenzi/algorithm.js';

isSymmetricTree(root); // boolean
```

### 

### 

- search(word, isWord):  isWord  true false
- insert(word): 

```javascript
import { TrieTree } from '@xiaowenzi/algorithm.js';

const trie = new TrieTree(['cat', 'bat', 'rat', 'cabt']);
trie.search('ca'); // true
trie.search('ca', true); // false, ca
trie.insert('aabb');
trie.search('aabb', true); // true
```

## 

## 



```javascript
import { Queue } from '@xiaowenzi/algorithm.js';

const queue = new Queue();
const limitQueue = new Queue(5);
```

 5 6 

- push(): 
- pop(): 
- front():  null
- end():  null
- empty(): 
- size(): 

Web Proxy Viewer  |  New URL  |  Original Page