Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 1013219

Browse files
Merge branch 'master' into add/longest-common-prefix
2 parents 7884551 + ed4f498 commit 1013219

10 files changed

+234
-2
lines changed

LeetcodeProblems/Algorithms/2Sum.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/**
2+
2 Sum
3+
https://leetcode.com/problems/two-sum/
4+
5+
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
6+
7+
You may assume that each input would have exactly one solution, and you may not use the same element twice.
8+
9+
You can return the answer in any order.
10+
11+
Example 1:
12+
Input: nums = [2,7,11,15], target = 9
13+
Output: [0,1]
14+
Explanation: Because nums[0] + nums[1] == 9, we return [0, 1].
15+
16+
Example 2:
17+
Input: nums = [3,2,4], target = 6
18+
Output: [1,2]
19+
20+
Example 3:
21+
Input: nums = [3,3], target = 6
22+
Output: [0,1]
23+
24+
*/
25+
26+
/**
27+
* @param {number[]} nums
28+
* @param {number} target
29+
* @return {number[]}
30+
*/
31+
var twoSum = function(nums, target) {
32+
let map ={};
33+
for(let i=0;i<nums.length;i++){
34+
const sum = target-nums[i];
35+
if(map[parseInt(sum)] != 0){
36+
return [map[sum], i];
37+
} else{
38+
map[nums[i]] = i;
39+
}
40+
}
41+
};
42+
43+
module.exports.twoSum = twoSum;
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
Find Subarrays With Equal Sum
3+
https://leetcode.com/problems/find-subarrays-with-equal-sum/description/
4+
5+
Given a 0-indexed integer array nums, determine whether there exist two subarrays of length 2 with equal sum. Note that the two subarrays must begin at different indices.
6+
7+
Return true if these subarrays exist, and false otherwise.
8+
9+
A subarray is a contiguous non-empty sequence of elements within an array.
10+
11+
12+
13+
Example 1:
14+
15+
Input: nums = [4,2,4]
16+
Output: true
17+
Explanation: The subarrays with elements [4,2] and [2,4] have the same sum of 6.
18+
19+
20+
Example 2:
21+
22+
Input: nums = [1,2,3,4,5]
23+
Output: false
24+
Explanation: No two subarrays of size 2 have the same sum.
25+
26+
Example 3:
27+
28+
Input: nums = [0,0,0]
29+
Output: true
30+
Explanation: The subarrays [nums[0],nums[1]] and [nums[1],nums[2]] have the same sum of 0.
31+
Note that even though the subarrays have the same content, the two subarrays are considered different because they are in different positions in the original array.
32+
*/
33+
34+
/**
35+
* @param {number[]} nums
36+
* @return {boolean}
37+
*/
38+
var findSubarrays = function (nums) {
39+
const sumsSeen = new Set();
40+
41+
for (let i = 0; i < nums.length - 1; i++) {
42+
if (sumsSeen.has(nums[i] + nums[i + 1])) {
43+
return true;
44+
}
45+
sumsSeen.add(nums[i] + nums[i + 1]);
46+
}
47+
48+
return false;
49+
};
50+
51+
module.exports.findSubarrays = findSubarrays;
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/**
2+
Reverse Integer
3+
https://leetcode.com/problems/reverse-integer/
4+
5+
Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0.
6+
7+
Assume the environment does not allow you to store 64-bit integers (signed or unsigned).
8+
9+
Example 1:
10+
Input: x = 123
11+
Output: 321
12+
13+
Example 2:
14+
Input: x = -123
15+
Output: -321
16+
17+
Example 3:
18+
Input: x = 120
19+
Output: 21
20+
21+
*/
22+
23+
/**
24+
* @param {number} x
25+
* @return {number}
26+
*/
27+
var reverseInteger = function(x) {
28+
const sign = x >= 0 ? 1 : -1;
29+
30+
let baseNum = sign * x;
31+
let builder = 0;
32+
33+
while (baseNum > 0) {
34+
const digit = baseNum % 10;
35+
builder = builder * 10 + digit;
36+
baseNum = (baseNum / 10) | 0;
37+
}
38+
39+
builder *= sign;
40+
41+
const isWithinRange = (builder >= -Math.pow(2, 31) && builder <= Math.pow(2, 31) - 1);
42+
43+
return isWithinRange ? builder : 0;
44+
};
45+
46+
module.exports.reverseInteger = reverseInteger;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
You are given a binary string s. In one second, all occurrences of "01" are simultaneously replaced with "10". This process repeats until no occurrences of "01" exist.
3+
4+
Return the number of seconds needed to complete this process.
5+
6+
7+
8+
Example 1:
9+
10+
Input: s = "0110101"
11+
Output: 4
12+
Explanation:
13+
After one second, s becomes "1011010".
14+
After another second, s becomes "1101100".
15+
After the third second, s becomes "1110100".
16+
After the fourth second, s becomes "1111000".
17+
No occurrence of "01" exists any longer, and the process needed 4 seconds to complete,
18+
so we return 4.
19+
Example 2:
20+
21+
Input: s = "11100"
22+
Output: 0
23+
Explanation:
24+
No occurrence of "01" exists in s, and the processes needed 0 seconds to complete,
25+
so we return 0.
26+
27+
28+
Constraints:
29+
30+
1 <= s.length <= 1000
31+
s[i] is either '0' or '1'.
32+
*/
33+
34+
35+
/**
36+
* @param {string} s
37+
* @return {number}
38+
*/
39+
var secondsToRemoveOccurrences = function(s) {
40+
let result = 0;
41+
while (true) {
42+
const replaced = s.replaceAll('01', '10');
43+
if (s === replaced) return result;
44+
s = replaced;
45+
result += 1;
46+
}
47+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const assert = require('assert');
2+
const twoSum = require('../../LeetcodeProblems/Algorithms/2Sum').twoSum;
3+
4+
var test = function () {
5+
assert.deepEqual([0,1], twoSum([2,7,11,15], 9));
6+
assert.deepEqual([1,2], twoSum([3,2,4], 6));
7+
assert.deepEqual([0,1], twoSum([3,3], 6));
8+
}
9+
10+
module.exports.test = test;
11+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
const assert = require('assert');
2+
const findSubarrays = require('../../LeetcodeProblems/Algorithms/Find_Subarrays_With_Equal_Sum').findSubarrays;
3+
4+
var test = function () {
5+
assert.equal(findSubarrays([4,2,4]), true);
6+
assert.equal(findSubarrays([1,2,3,4,5]), false);
7+
assert.equal(findSubarrays([0,0,0]), true);
8+
}
9+
10+
module.exports.test = test;

LeetcodeProblemsTests/Algorithms/Maximise_Hour_Glass_Sum.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ const assert = require('assert');
22
const maxSum = require('../../LeetcodeProblems/Algorithms/Maximise_Hour_Glass_Sum').maxSum;
33

44
function test() {
5-
assert.equal(maxSum([[6,2,1,3],[4,2,1,5],[9,2,8,7],[4,1,2,9]]));
6-
assert.equal(maxSum([[1,2,3],[4,5,6],[7,8,9]]));
5+
assert.equal(maxSum([[6,2,1,3],[4,2,1,5],[9,2,8,7],[4,1,2,9]]), 30);
6+
assert.equal(maxSum([[1,2,3],[4,5,6],[7,8,9]]), 35);
77
}
88

99
module.exports.test = test
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const assert = require('assert');
2+
const reverseInteger = require('../../LeetcodeProblems/Algorithms/Reverse_Integer').reverseInteger;
3+
4+
var test = function() {
5+
assert.equal(reverseInteger(123), 321);
6+
assert.equal(reverseInteger(-123), -321);
7+
assert.equal(reverseInteger(120), 21);
8+
}
9+
10+
11+
module.exports.test = test;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const assert = require('assert');
2+
const secondsToRemoveOccurrences = require('../../LeetcodeProblems/Algorithms/Time_Needed_Rearrange_Binary_String').secondsToRemoveOccurrences;
3+
4+
function test() {
5+
assert.equal(secondsToRemoveOccurrences("0110101"), 4);
6+
assert.equal(secondsToRemoveOccurrences("11100"), 0)
7+
};
8+
9+
module.exports.test = test

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ To run a specific problem in your console run `node <problem_file_path>` (e.g.
6262
| [Construct Binary Tree from Preorder and Inorder Traversal ](/LeetcodeProblems/Algorithms/Construct_Binary_Tree_from_Preorder_and_Inorder_Traversal.js) | Medium | https://leetcode.com/problems/construct-binary-tree-from-preorder-and-inorder-traversal/ |
6363
| [Lowest Common Ancestor of a Binary Tree ](/LeetcodeProblems/Algorithms/Lowest_Common_Ancestor_of_a_Binary_Tree.js) | Medium | https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ |
6464
| [Maximum Sum of an Hourglass](/LeetcodeProblems/Algorithms/Maximise_Hour_Glass_Sum.js) | Medium | https://leetcode.com/problems/maximum-sum-of-an-hourglass/ |
65+
| [Reverse Integer](/LeetcodeProblems/Algorithms/Reverse_Integer.js) | Medium | https://leetcode.com/problems/reverse-integer/ |
66+
| [Time Needed to Rearrange a Binary String](/LeetcodeProblems/Algorithms/Time_Needed_Rearrange_Binary_String.js) | Medium | https://leetcode.com/problems/time-needed-to-rearrange-a-binary-string/ |
67+
| [Find Subarrays With Equal Sum ](/LeetcodeProblems/Algorithms/Find_Subarrays_With_Equal_Sums.js) | Medium | https://leetcode.com/problems/find-subarrays-with-equal-sum/ |
6568
| [Flood Fill ](/LeetcodeProblems/Algorithms/Flood_Fill.js) | Easy | https://leetcode.com/problems/flood-fill/ |
6669
| [Implement stack using queues ](/LeetcodeProblems/Algorithms/Implement_stack_using_queues.js) | Easy | https://leetcode.com/problems/implement-stack-using-queues/ |
6770
| [Number of Segments in a String ](/LeetcodeProblems/Algorithms/Number_of_Segments_in_a_String.js) | Easy | https://leetcode.com/problems/number-of-segments-in-a-string/ |
@@ -77,6 +80,7 @@ To run a specific problem in your console run `node <problem_file_path>` (e.g.
7780
| [Binary Gap ](/LeetcodeProblems/Algorithms/Binary_Gap.js) | Easy | https://leetcode.com/problems/binary-gap/ |
7881
| [Majority Element](/LeetcodeProblems/Algorithms/Majority_Element.js) | Easy | https://leetcode.com/problems/majority-element/ |
7982
| [Longest Common Prefix](/LeetcodeProblems/Algorithms/Longest_Common_Prefix.js) | Easy | https://leetcode.com/problems/longest-common-prefix/ |
83+
| [Two Sum](/LeetcodeProblems/Algorithms/2Sum.js) | Easy | https://leetcode.com/problems/two-sum/ |
8084
| [Tic Tac Toe ](/LeetcodeProblems/Algorithms/Tic_Tac_Toe.js) | | |
8185
| [Permutations With Duplicates ](/LeetcodeProblems/Algorithms/Permutations_With_Duplicates.js) | | |
8286
| [Deletion Distance](/LeetcodeProblems/Algorithms/Deletion_Distance.js) | | |

0 commit comments

Comments
 (0)