Last active
May 10, 2019 14:35
-
-
Save Ampa1R/5fc17de9e235cac3a38b009e4c7d2ff7 to your computer and use it in GitHub Desktop.
leetcode js
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
| SELECT | |
| `Department`.`Name` AS `Department`, | |
| `Employee`.`Name` AS `Employee`, | |
| `Employee`.`Salary` AS `Salary` | |
| FROM `Department` | |
| JOIN `Employee` | |
| ON `Department`.`Id` = `Employee`.`DepartmentId` | |
| AND `Salary` = ( | |
| SELECT MAX(`Salary`) | |
| FROM `Employee` | |
| WHERE `Employee`.`DepartmentId` = `Department`.`Id` | |
| ); |
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
| const divide = (dividend, divisor) => { | |
| const MAX_VALUE = 2 ** 31 - 1; | |
| const MIN_VALUE = -(2 ** 31); | |
| let res = 0; | |
| const signIsNegative = (Math.sign(dividend) + Math.sign(divisor)) === 0; | |
| dividend = Math.abs(dividend); | |
| divisor = Math.abs(divisor); | |
| while (dividend >= divisor) { | |
| let leftShift = 0; | |
| let shiftedDivisor = divisor; | |
| while (shiftedDivisor <= (dividend >> 1)) { | |
| leftShift++; | |
| shiftedDivisor = shiftedDivisor << 1; | |
| } | |
| res += 1 << leftShift; | |
| dividend = dividend - shiftedDivisor; | |
| } | |
| if(signIsNegative) res = -res; | |
| if(res > MAX_VALUE) return MAX_VALUE; | |
| else if(res < MIN_VALUE) return MIN_VALUE; | |
| else return res; | |
| }; |
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
| const LRUCache = function(capacity) { | |
| this.capacity = capacity; | |
| this.items = {}; | |
| this.recent = []; | |
| return this; | |
| }; | |
| LRUCache.prototype.get = function(key) { | |
| let val = this.items[key]; | |
| if(val) { | |
| let index = this.recent.indexOf(key); | |
| this.recent.splice(index, 1); | |
| this.recent.push(key); | |
| return val; | |
| } | |
| else return -1; | |
| }; | |
| LRUCache.prototype.put = function(key, value) { | |
| if(this.items[key] !== undefined) { | |
| let index = this.recent.indexOf(key); | |
| this.recent.splice(index, 1); | |
| } | |
| else if(this.recent.length >= this.capacity) { | |
| let itemToDelete = this.recent.shift(); | |
| delete this.items[itemToDelete]; | |
| } | |
| this.recent.push(key); | |
| this.items[key] = value; | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment