This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //from http://2ality.com/2011/04/singleton-pattern-in-javascript-not.html | |
| var namespace = { | |
| _singleton:null, | |
| get singleton(){ | |
| if(!this._singleton){ | |
| this._singleton = { | |
| amethod:function(){ | |
| console.log('I am single') | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function shuffleCards(){ | |
| let cards = []; | |
| let n=51; | |
| while(n>=0){ | |
| cards[n] = n; | |
| n--; | |
| }; | |
| for(let i=0; i<cards.length; i++){ | |
| let rand = Math.floor(Math.random()*(51-i)) + 1; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function spiralMatrix(m, n){ | |
| var colS = 0, | |
| colE = n-1, | |
| rowS = 0, | |
| rowE = n-1; | |
| while(colS <= colE && rowS <= rowE){ | |
| let c = colS, | |
| r = rowS; | |
| for(c=colS; c<=colE;c++) | |
| console.log(m[r][c]) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function partition(a, start, end){ | |
| function swap(_a, i, j){ | |
| var tmp = _a[i] | |
| _a[i] = _a[j] | |
| _a[j] = tmp | |
| } | |
| var pivot = a[end] | |
| var pIndex = start | |
| for(let i=start; i<end; i++){ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function binarySearchIterative(a,n,data){ | |
| var high = n | |
| var low = 0 | |
| while(1){ | |
| let mid = Math.abs((low + high)/2) | |
| if(a[mid] === data) return mid | |
| if(data < a[mid]) high = mid-1 | |
| else low = mid + 1 | |
| } | |
| return -1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function mergeSort(a, low, high){ | |
| if(!a || !a.length || a.length < 2) return a | |
| if(low === undefined) low = 0 | |
| if(high === undefined) high = a.length -1 | |
| if(low >=high) return a | |
| let mid = Math.floor((low + high)/2) | |
| mergeSort(a, low, mid) | |
| mergeSort(a, mid + 1, high) | |
| merge(a,low,high,mid) | |
| return a |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class BinarryTree{ | |
| constructor(root, left, right){ | |
| this.root = root || null | |
| this.left = left || null | |
| this.right = right || null | |
| } | |
| levelOrderTree(t){ | |
| var q = [] | |
| q.unshift(t) | |
| while (q.length>0) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class Q { | |
| constructor(capacity){ | |
| this.front = this.rear = -1 | |
| this.capacity = capacity | |
| this.arr = [] | |
| } | |
| isFull(){ | |
| return (this.rear + 1) % this.capacity === this.front | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function reveseStack(s){ | |
| if(s.length === 0) return | |
| var data = s.pop() | |
| console.log('reveseStack -> ' + data) | |
| reveseStack(s) | |
| instertAtBottom(s, data) | |
| } | |
| function instertAtBottom (s, data){ | |
| console.log('instertAtBottom -> ' + data) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class LinkedList{ | |
| constructor(){ | |
| this.head = null | |
| this.tail = null | |
| this.count = 0 | |
| } | |
| insertAtBegining(data){ | |
| const node = { | |
| data:data, |