Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Merged
ignacio-chiazzo merged 2 commits into ignacio-chiazzo:master from TemitopeAgbaje:Temitope
Oct 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 21 additions & 9 deletions LeetcodeProblems/Algorithms/2Sum.js
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Owner

@ignacio-chiazzo ignacio-chiazzo Oct 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you encapsulate the new solution into a new method named twoSum2?

There are other examples in other files like CoinChange https://github.com/ignacio-chiazzo/Algorithms-Leetcode-Javascript/blob/08563058f7e93ce1688956cc26d5dc2a98b36d1a/LeetcodeProblems/Algorithms/Coin_Change.js.

Also, could you run the tests for both functions twoSum and twoSum2?

Copy link
Contributor Author

@TemitopeAgbaje TemitopeAgbaje Oct 8, 2022

Choose a reason for hiding this comment

The 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;
14 changes: 11 additions & 3 deletions LeetcodeProblemsTests/Algorithms/2Sum_Test.js
View file Open in desktop
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;

10 changes: 5 additions & 5 deletions LeetcodeProblemsTests/Algorithms/3Sum_Closest_Test.js
View file Open in desktop
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;
21 changes: 9 additions & 12 deletions LeetcodeProblemsTests/Algorithms/3Sum_Test.js
View file Open in desktop
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;
7 changes: 3 additions & 4 deletions LeetcodeProblemsTests/Algorithms/Add_Two_Numbers_Test.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const assert = require('assert');
const addTwoNumbers = require('../../LeetcodeProblems/Algorithms/Add_Two_Numbers').addTwoNumbers;
const ListNodeTestHelper = require('../../UtilsClasses/ListNodeTestHelper');
var ListNode = require('../../UtilsClasses/ListNode').ListNode;
const addTwoNumbers = require("../../LeetcodeProblems/Algorithms/Add_Two_Numbers").addTwoNumbers;
const ListNodeTestHelper = require("../../UtilsClasses/ListNodeTestHelper");
var ListNode = require("../../UtilsClasses/ListNode").ListNode;

function test() {
const list1 = ListNode.linkenList([1,2,3,4]);
Expand Down
4 changes: 2 additions & 2 deletions LeetcodeProblemsTests/Algorithms/Award_Budget_Cuts_Test.js
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const assert = require('assert');
const cutAwardBadges = require('../../LeetcodeProblems/Algorithms/Award_Budget_Cuts').cutAwardBadges;
const assert = require("assert");
const cutAwardBadges = require("../../LeetcodeProblems/Algorithms/Award_Budget_Cuts").cutAwardBadges;

function test() {
assert.deepEqual(
Expand Down
31 changes: 20 additions & 11 deletions Test.js
View file Open in desktop
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();

AltStyle によって変換されたページ (->オリジナル) /