-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path43165.js
More file actions
55 lines (49 loc) · 1.11 KB
/
43165.js
File metadata and controls
55 lines (49 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
class Node{
constructor(item){
this.item = item;
this.next=null;
}
}
class Queue{
constructor(){
this.head=null;
this.tail=null
}
push(item){
const node = new Node(item);
if(this.head===null){
this.head=node;
this.head.next = this.tail;
}
else{
this.tail.next=node;
}
this.tail=node;
}
pop(){
const popedItem = this.head;
this.head=this.head.next;
return popedItem;
}
}
function solution(numbers, target) {
var answer = 0;
var queue=new Queue();
queue.push(numbers[0]);
for(var i=1; i<numbers.length; i++){
var count=2**(i-1);
for(var j=0; j<count; j++){
var temp = queue.pop();
queue.push(temp.item-numbers[i]);
queue.push(temp.item+numbers[i]);
}
}
let current = queue.head;
while(current){
if((current.item===target)||current.item===-target){
answer=answer+1;
}
current=current.next;
}
return answer;
}