- Notifications
You must be signed in to change notification settings - Fork2.4k
Refactor JS - Array & Hash#426
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,22 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @param {number} target | ||
* Time O(N) || Space O(N) | ||
* @return {number[]} | ||
*/ | ||
var twoSum = (nums, target, map = new Map()) => { | ||
for (let index = 0; index < nums.length; index++) { | ||
const num = nums[index]; | ||
const complement = target - num; | ||
if (map.has(complement)) { | ||
const sumIndex = map.get(complement); | ||
return [ index, sumIndex ] | ||
} | ||
map.set(num, index); | ||
} | ||
return [ -1, -1 ] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. I don't think | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,90 +1,23 @@ | ||
/** | ||
* @param {number[]} nums | ||
* Time O (N) | Space O(N) | ||
* @return {number} | ||
*/ | ||
var longestConsecutive = function(nums, longestStreak = 0) { | ||
const numSet = new Set(nums); | ||
for (const num of [ ...numSet ]) { | ||
if (numSet.has(num - 1)) continue; | ||
let [ currentNum, currentStreak ] = [ num, 1 ]; | ||
while (numSet.has(currentNum + 1)) { | ||
currentNum += 1; | ||
currentStreak += 1; | ||
} | ||
longestStreak = Math.max(longestStreak, currentStreak); | ||
} | ||
returnlongestStreak; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,8 @@ | ||
/** | ||
* @param {number[]} nums | ||
* Time O(N) | Space O(N) | ||
* @return {boolean} | ||
*/ | ||
var containsDuplicate = function(nums) { | ||
return (new Set(nums)).size !== nums.length | ||
Comment on lines 1 to +7 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. This solution has already been proposed (#371) | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,27 @@ | ||
/** | ||
* @param {number[]} nums | ||
* Time O(N) | Space O(N) | ||
* @return {number[]} | ||
*/ | ||
var productExceptSelf = function(nums) { | ||
constproducts =new Array(nums.length).fill(1); | ||
carryForward(nums, products); | ||
carryBackward(nums, products); | ||
return products; | ||
}; | ||
const carryForward = (nums, products, product = 1) => { | ||
for (let index = 0; index < nums.length; index++) { | ||
products[index] = product; | ||
product *= nums[index]; | ||
} | ||
} | ||
const carryBackward = (nums, products, product = 1) => { | ||
for (let index = (nums.length - 1); 0 <= index; index--) { | ||
products[index] *= product; | ||
product *= nums[index]; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,34 +1,52 @@ | ||
/** | ||
* @param {string} s | ||
* @param {string} t | ||
* Time O(N * logN) | Space O(N) | ||
* @return {boolean} | ||
*/ | ||
var isAnagram = function(s, t) { | ||
if (s.length !== t.length) return false; | ||
const reOrder = (str) => str | ||
.split('') | ||
.sort((a, b) => a.localeCompare(b)) | ||
.join(''); | ||
return reOrder(s) === reOrder(t) | ||
}; | ||
/** | ||
* @param {string} s | ||
* @param {string} t | ||
* Time O(N) | Space O(N) | ||
* @return {boolean} | ||
*/ | ||
var isAnagram = function(s, t, map = new Map()) { | ||
if (s.length !== t.length) return false; | ||
addCharFrequency(s, map); | ||
return subtractCharFrequency(t, map) | ||
}; | ||
Comment on lines 7 to +30 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others.Learn more. Adding multiple solutions is being discussed here (#189) | ||
const addCharFrequency = (str, map) => { | ||
for (const char of str) { | ||
map.set(char, (map.get(char) || 0) + 1); | ||
} | ||
} | ||
const subtractCharFrequency = (str,map) => { | ||
for (const char of str) { | ||
if (!map.has(char)) { | ||
return false; | ||
} | ||
map.set(char, (map.get(char) - 1)); | ||
} | ||
return true; | ||
} | ||
This file was deleted.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.