class Queue {
arr = []
push (item) {
this.arr.push(item)
}
pop() {
return this.arr.shift()
}
getArr() {
return this.arr
}
size () {
return this.arr.length
}
isEmpty () {
return this.arr.length === 0
}
}
class Node {
data = ''
left = null
right = null
}
function fib(n = 1, arr = []) {
if (n === 1 || n === 2) return 1
if(arr[n]) return arr[n]
arr[n] = fib( n - 1, arr) + fib(n - 2, arr)
return arr[n]
}
// console.log(fib(1000))
let res = []
function backtrack(str = '', arr = '') {
if (arr.length === str.length) {
res.push(arr)
return
}
for(let i = 0; i < str.length; i++) {
if (arr.indexOf(str[i]) !== -1) {
continue
}
arr += str[i]
backtrack(str, arr)
arr = arr.substr(0, arr.length - 1)
}
}
// backtrack('12345678')
let resNQ = []
const isValid = (board, row, col) => {
let m = board.length
let n = board[0].length
let flag = true
// row
for(let i = 0; i < n; i++) {
}
return flag
}
function solveNQueens (board, row = 0) {
if (row === board.length) {
resNQ.push(board)
return
}
let n = board[row].length
for(let col = 0; col < n; col++) {
if (!isValid(board, row, col)) {
continue
}
// console.log(row, col)
board[row][col] = 'Q'
solveNQueens(board, row + 1)
board[row][col] = '.'
}
}
let board = []
for(let i = 0; i < 4; i++) {
let res = []
for(let j = 0; j < 4; j++) {
res.push('.')
}
board.push(res)
}
let q = new Queue()
q.push(1)
q.push(2)
console.log(q.getArr())
console.log(q.pop())
let treeArr= [3, 9, 20, null, null, 15, 7]
const createNode = (i, data)=> {
if (i >= data.length) return null
let node = new Node()
node.data = data[i]
node.left = createNode(i + 1, data)
node.right = createNode(i + 2, data)
return node
}
console.log(createNode(0,treeArr))
console