-
Notifications
You must be signed in to change notification settings - Fork 101
Added another method to 2sum Problem #60
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,16 +28,28 @@ Output: [0,1] | |
* @param {number} target | ||
* @return {number[]} | ||
*/ | ||
var twoSum = function(nums, target) { | ||
let map ={}; | ||
for(let i=0;i<nums.length;i++){ | ||
const sum = target-nums[i]; | ||
if(map[parseInt(sum)] != 0){ | ||
return [map[sum], i]; | ||
} else{ | ||
map[nums[i]] = i; | ||
var twoSum = function (nums, target) { | ||
let map = {}; | ||
for (let i = 0; i < nums.length; i++) { | ||
const sum = target - nums[i]; | ||
if (map[parseInt(sum)] != 0) { | ||
return [map[sum], i]; | ||
} else { | ||
map[nums[i]] = i; | ||
} | ||
} | ||
}; | ||
|
||
//Another method | ||
var twoSum2 = function (nums, target) { | ||
for (let i = 0; i < nums.length; i++) { | ||
for (let j = i + 1; j < nums.length; i++) { | ||
if (nums[1] + nums[j] === target) { | ||
return [i, j]; | ||
} | ||
} | ||
} | ||
}; | ||
Comment on lines
+45
to
52
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. Could you encapsulate the new solution into a new method named There are other examples in other files like Also, could you run the tests for both functions 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. Alright.. i would fix that |
||
|
||
module.exports.twoSum = twoSum; | ||
module.exports.twoSum = twoSum; | ||
module.exports.twoSum2 = twoSum2; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,19 @@ | ||
const assert = require('assert'); | ||
const twoSum = require('../../LeetcodeProblems/Algorithms/2Sum').twoSum; | ||
const assert = require("assert"); | ||
const twoSum = require("../../LeetcodeProblems/Algorithms/2Sum").twoSum; | ||
const twoSum2 = require("../../LeetcodeProblems/Algorithms/2Sum").twoSum2; | ||
|
||
var test = function () { | ||
assert.deepEqual([0,1], twoSum([2,7,11,15], 9)); | ||
assert.deepEqual([1,2], twoSum([3,2,4], 6)); | ||
assert.deepEqual([0,1], twoSum([3,3], 6)); | ||
} | ||
}; | ||
|
||
var test2 = function () { | ||
assert.deepEqual([0,1], twoSum2([2,7,11,15], 9)); | ||
assert.deepEqual([1,2], twoSum2([3,2,4], 6)); | ||
assert.deepEqual([0,1], twoSum2([3,3], 6)); | ||
}; | ||
|
||
module.exports.test = test; | ||
module.exports.test2 = test2; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
const assert = require('assert'); | ||
const threeSumClosest = require('../../LeetcodeProblems/Algorithms/3SumClosest').threeSumClosest; | ||
const assert = require("assert"); | ||
const threeSumClosest = | ||
require("../../LeetcodeProblems/Algorithms/3SumClosest").threeSumClosest; | ||
|
||
var test = function () { | ||
assert.equal(2, threeSumClosest([-1, 2, 1, -4], 1)); | ||
assert.equal(2, threeSumClosest([-1, 2, 1, -4], 1)); | ||
assert.equal(0, threeSumClosest([0, 0, 0], 1)); | ||
|
||
} | ||
}; | ||
|
||
module.exports.test = test; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,19 @@ | ||
const assert = require('assert'); | ||
const threeSum = require('../../LeetcodeProblems/Algorithms/3Sum').threeSum; | ||
const assert = require("assert"); | ||
const threeSum = require("../../LeetcodeProblems/Algorithms/3Sum").threeSum; | ||
|
||
var test = function () { | ||
assert.deepEqual(threeSum([]), []); | ||
assert.deepEqual(threeSum([0]), []); | ||
assert.deepEqual(threeSum([0, 0]), []); | ||
assert.deepEqual(threeSum([0, 0, 0]), [[0, 0, 0]]); | ||
assert.deepEqual( | ||
threeSum([0, 0, 0]), | ||
threeSum([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]), | ||
[[0, 0, 0]] | ||
); | ||
assert.deepEqual( | ||
threeSum([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]), | ||
[[0, 0, 0]] | ||
); | ||
assert.deepEqual( | ||
threeSum([-1, 0, 1, 2, -1, -4]), | ||
[ [ -1, 2, -1 ], [ 0, 1, -1 ] ] | ||
); | ||
} | ||
assert.deepEqual(threeSum([-1, 0, 1, 2, -1, -4]), [ | ||
[-1, 2, -1], | ||
[0, 1, -1], | ||
]); | ||
}; | ||
|
||
module.exports.test = test; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,47 @@ | ||
const fs = require('fs'); | ||
/* eslint-disable no-useless-escape */ | ||
/* eslint-disable no-undef */ | ||
const fs = require("fs"); | ||
|
||
const TESTS_FOLDER = './LeetcodeProblemsTests/Algorithms/'; | ||
const TESTS_FOLDER = "./LeetcodeProblemsTests/Algorithms/"; | ||
const REGEX_PATTERN_HIDDEN_FILES = /(^|\/)\.[^\/\.]/g; | ||
|
||
var test_all = async function() { | ||
var test_all = async function() { | ||
try { | ||
const problems = await loadProblems(); | ||
for(i in problems) { | ||
for(i in problems) { | ||
console.log("Solving: " + problems[i]); | ||
const problem = require(TESTS_FOLDER + problems[i]); | ||
|
||
if (typeof(problem.test) !=='undefined') { | ||
if (typeofproblem.test !== "undefined") { | ||
problem.test(); | ||
console.log("✅ Tests for " + problems[i] + " run successfully \n"); | ||
} else { | ||
console.warn(problem, "🔴 The problem " + problems[i] + " doesn't have a test method implemented.\n"); | ||
} | ||
console.warn( | ||
problem, | ||
"🔴 The problem " + | ||
problems[i] + | ||
" doesn't have a test method implemented.\n" | ||
); | ||
} | ||
} | ||
} catch (error) { | ||
throw new Error(error); | ||
} | ||
} | ||
}; | ||
|
||
var loadProblems = () => { | ||
return new Promise(function (resolve, reject) { | ||
fs.readdir(TESTS_FOLDER, (error, files) => { | ||
if (error) { | ||
reject(error); | ||
} else { | ||
problems = files.filter(item => !(REGEX_PATTERN_HIDDEN_FILES).test(item)); | ||
problems = files.filter( | ||
(item) => !REGEX_PATTERN_HIDDEN_FILES.test(item) | ||
); | ||
resolve(problems); | ||
} | ||
}) | ||
}); | ||
}); | ||
} | ||
}; | ||
|
||
test_all(); |