Skip to content

Commit c146931

Browse files
authored
Merge branch 'master' into add/next_permutation
2 parents f31f1ce + e4c6481 commit c146931

File tree

7 files changed

+160
-1
lines changed

7 files changed

+160
-1
lines changed

LeetcodeProblems/Algorithms/2Sum.js

+43
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;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
Longest Common Prefix
3+
https://leetcode.com/problems/longest-common-prefix/
4+
5+
Write a function to find the longest common prefix string amongst an array of strings.
6+
7+
If there is no common prefix, return an empty string "".
8+
9+
Example 1:
10+
Input: strs = ["flower","flow","flight"]
11+
Output: "fl"
12+
13+
Example 2:
14+
Input: strs = ["dog","racecar","car"]
15+
Output: ""
16+
Explanation: There is no common prefix among the input strings.
17+
18+
*/
19+
20+
/**
21+
* @param {string[]} strs
22+
* @return {string}
23+
*/
24+
var longestCommonPrefix = function(strs) {
25+
if(strs.length === 0) return "";
26+
27+
return strs.reduce((result, curr)=>{
28+
let i = 0;
29+
while(result[i] && curr[i] && result[i] === curr[i]) i++;
30+
return result.slice(0, i);
31+
});
32+
};
33+
34+
module.exports.longestCommonPrefix = longestCommonPrefix;
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;
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+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
const assert = require('assert');
3+
const longestCommonPrefix = require('../../LeetcodeProblems/Algorithms/Longest_Common_Prefix').longestCommonPrefix;
4+
5+
function test() {
6+
assert.equal(longestCommonPrefix(["flower","flow","flight"]), "fl");
7+
assert.equal(longestCommonPrefix(["dog","racecar","car"]), "");
8+
assert.equal(longestCommonPrefix([]), "");
9+
}
10+
11+
module.exports.test = test;
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;

README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ To run a specific problem in your console run `node <problem_file_path>` (e.g.
6464
| [Maximum Sum of an Hourglass](/LeetcodeProblems/Algorithms/Maximise_Hour_Glass_Sum.js) | Medium | https://leetcode.com/problems/maximum-sum-of-an-hourglass/ |
6565
| [Next Permutation](/LeetcodeProblems/Algorithms/Next_Permutation.js) | Medium | https://leetcode.com/problems/next-permutation/ |
6666
| [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/ |
67+
| [Find Subarrays With Equal Sum ](/LeetcodeProblems/Algorithms/Find_Subarrays_With_Equal_Sums.js) | Medium | https://leetcode.com/problems/find-subarrays-with-equal-sum/ |
68+
| [Reverse Integer](/LeetcodeProblems/Algorithms/Reverse_Integer.js) | Medium | https://leetcode.com/problems/reverse-integer/ |
6869
| [Flood Fill ](/LeetcodeProblems/Algorithms/Flood_Fill.js) | Easy | https://leetcode.com/problems/flood-fill/ |
6970
| [Implement stack using queues ](/LeetcodeProblems/Algorithms/Implement_stack_using_queues.js) | Easy | https://leetcode.com/problems/implement-stack-using-queues/ |
7071
| [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/ |
@@ -79,6 +80,8 @@ To run a specific problem in your console run `node <problem_file_path>` (e.g.
7980
| [Binary Gap ](/LeetcodeProblems/Algorithms/Binary_Gap.js) | Easy | https://leetcode.com/problems/binary-gap/ |
8081
| [Binary Gap ](/LeetcodeProblems/Algorithms/Binary_Gap.js) | Easy | https://leetcode.com/problems/binary-gap/ |
8182
| [Majority Element](/LeetcodeProblems/Algorithms/Majority_Element.js) | Easy | https://leetcode.com/problems/majority-element/ |
83+
| [Longest Common Prefix](/LeetcodeProblems/Algorithms/Longest_Common_Prefix.js) | Easy | https://leetcode.com/problems/longest-common-prefix/ |
84+
| [Two Sum](/LeetcodeProblems/Algorithms/2Sum.js) | Easy | https://leetcode.com/problems/two-sum/ |
8285
| [Tic Tac Toe ](/LeetcodeProblems/Algorithms/Tic_Tac_Toe.js) | | |
8386
| [Permutations With Duplicates ](/LeetcodeProblems/Algorithms/Permutations_With_Duplicates.js) | | |
8487
| [Deletion Distance](/LeetcodeProblems/Algorithms/Deletion_Distance.js) | | |

0 commit comments

Comments
 (0)