Skip to content

Commit 19b5721

Browse files
Added 3Sum, Min Stack and SumOfSquare
1 parent 27a133e commit 19b5721

File tree

3 files changed

+191
-0
lines changed

3 files changed

+191
-0
lines changed

LeetcodeProblems/3Sum.js

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
3Sum
3+
https://leetcode.com/problems/3sum/description/
4+
5+
Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
6+
7+
Note:
8+
9+
The solution set must not contain duplicate triplets.
10+
11+
Example:
12+
13+
Given array nums = [-1, 0, 1, 2, -1, -4],
14+
15+
A solution set is:
16+
[
17+
[-1, 0, 1],
18+
[-1, -1, 2]
19+
]
20+
*/
21+
22+
/**
23+
* @param {number[]} nums
24+
* @return {number[][]}
25+
*/
26+
var threeSum = function(nums) {
27+
var ret = [];
28+
nums.sort(function(a, b) { return a - b });
29+
for(var i = 0; i < nums.length; i++) {
30+
if(i === 0 || i > 0 && nums[i] !== nums[i - 1]) {
31+
var left = i + 1;
32+
var right = nums.length - 1;
33+
while(left < right) {
34+
const sum = nums[left] + nums[right] + nums[i];
35+
if(left > i + 1 && nums[left] === nums[left - 1] || sum < 0) {
36+
left++
37+
} else if(right < nums.length - 1 && nums[right] === nums[right + 1] || sum > 0) {
38+
right--;
39+
} else if(sum === 0) {
40+
ret.push([nums[left], nums[right], nums[i]]);
41+
left++;
42+
right--;
43+
}
44+
}
45+
}
46+
}
47+
48+
return ret;
49+
};
50+
51+
var main = function() {
52+
console.log(threeSum([]));
53+
console.log(threeSum([-1, 0, 1, 2, -1, -4]));
54+
console.log(threeSum([0]));
55+
console.log(threeSum([0, 0]));
56+
console.log(threeSum([0, 0, 0]));
57+
console.log(threeSum([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]));
58+
}
59+
60+
module.exports.main = main;

LeetcodeProblems/Min_Stack.js

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
Min Stack
3+
4+
https://leetcode.com/problems/min-stack/description/
5+
6+
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
7+
8+
push(x) -- Push element x onto stack.
9+
pop() -- Removes the element on top of the stack.
10+
top() -- Get the top element.
11+
getMin() -- Retrieve the minimum element in the stack.
12+
Example:
13+
MinStack minStack = new MinStack();
14+
minStack.push(-2);
15+
minStack.push(0);
16+
minStack.push(-3);
17+
minStack.getMin(); --> Returns -3.
18+
minStack.pop();
19+
minStack.top(); --> Returns 0.
20+
minStack.getMin(); --> Returns -2.
21+
*/
22+
23+
class MinStack {
24+
constructor() {
25+
this.minStack = [];
26+
this.stack = [];
27+
this.countStack = 0;
28+
this.countMinStack = 0;
29+
}
30+
31+
push(value) {
32+
if(this.countStack === this.stack.length)
33+
this.stack.push(value);
34+
else
35+
this.stack[this.countStack] = value;
36+
this.countStack++;
37+
38+
const min = this.getMin();
39+
if(min === null || min >= value) {
40+
if(this.countMinStack === this.minStack.length)
41+
this.minStack.push(value);
42+
else
43+
this.minStack[this.countMinStack] = value;
44+
this.countMinStack++;
45+
}
46+
}
47+
48+
pop() {
49+
if(this.countStack === 0)
50+
return null;
51+
52+
var elem = this.stack[this.countStack - 1];
53+
this.countStack--;
54+
55+
if(elem === this.minStack[this.countMinStack - 1])
56+
this.countMinStack--;
57+
58+
return elem;
59+
}
60+
61+
top() {
62+
if(this.countStack === 0)
63+
return null;
64+
65+
return this.stack[this.countStack - 1];
66+
}
67+
68+
getMin() {
69+
if(this.countMinStack === 0)
70+
return null;
71+
72+
return this.minStack[this.countMinStack - 1]
73+
}
74+
}
75+
76+
var main = function() {
77+
var minStack = new MinStack();
78+
minStack.push(-2);
79+
minStack.push(0);
80+
minStack.push(-3);
81+
console.log(minStack.getMin());
82+
console.log(minStack.pop());
83+
console.log(minStack.top());
84+
console.log(minStack.getMin());
85+
}
86+
87+
module.exports.main = main;
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
Sum of Square Numbers
3+
https://leetcode.com/problems/sum-of-square-numbers/description/
4+
5+
Given a non-negative integer c, your task is to decide whether there're two integers a and b such that a2 + b2 = c.
6+
7+
Example 1:
8+
Input: 5
9+
Output: True
10+
Explanation: 1 * 1 + 2 * 2 = 5
11+
12+
Example 2:
13+
Input: 3
14+
Output: False
15+
*/
16+
17+
18+
/**
19+
* @param {number} c
20+
* @return {boolean}
21+
*/
22+
var judgeSquareSum = function(c) {
23+
var iter = 0;
24+
var set = new Set();
25+
while(iter ** 2 <= c) {
26+
var square = iter * iter;
27+
if(square * 2 === c || set.has(c - square))
28+
return true;
29+
30+
set.add(square);
31+
iter++;
32+
}
33+
34+
return false;
35+
};
36+
37+
var main = function() {
38+
console.log(judgeSquareSum(0));
39+
console.log(judgeSquareSum(1));
40+
console.log(judgeSquareSum(5));
41+
console.log(judgeSquareSum(16));
42+
console.log(judgeSquareSum(24));
43+
console.log(judgeSquareSum(25));
44+
}

0 commit comments

Comments
 (0)