Skip to content

Commit c571fe5

Browse files
committed
Added solutions for c++, java and js
1 parent e10c1c9 commit c571fe5

File tree

414 files changed

+8170
-110
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

414 files changed

+8170
-110
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <vector>
2+
#include <unordered_map>
3+
#include <set>
4+
#include <algorithm>
5+
6+
using namespace std;
7+
8+
class Solution {
9+
public:
10+
vector<vector<int>> threeSum(vector<int>& nums) {
11+
unordered_map<int, int> indexMap;
12+
set<vector<int>> result;
13+
int n = nums.size();
14+
15+
// Build the index map
16+
for (int i = 0; i < n; ++i) {
17+
indexMap[nums[i]] = i;
18+
}
19+
20+
// Iterate over each pair
21+
for (int i = 0; i < n; ++i) {
22+
for (int j = i + 1; j < n; ++j) {
23+
int desired = -nums[i] - nums[j];
24+
if (indexMap.find(desired) != indexMap.end() &&
25+
indexMap[desired] != i && indexMap[desired] != j) {
26+
vector<int> triplet = {nums[i], nums[j], desired};
27+
sort(triplet.begin(), triplet.end());
28+
result.insert(triplet);
29+
}
30+
}
31+
}
32+
33+
return vector<vector<int>>(result.begin(), result.end());
34+
}
35+
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import java.util.*;
2+
3+
public class Solution {
4+
public List<List<Integer>> threeSum(int[] nums) {
5+
Set<List<Integer>> result = new HashSet<>();
6+
Map<Integer, Integer> indexMap = new HashMap<>();
7+
int n = nums.length;
8+
9+
// Build the index map
10+
for (int i = 0; i < n; i++) {
11+
indexMap.put(nums[i], i);
12+
}
13+
14+
// Iterate over each pair
15+
for (int i = 0; i < n; i++) {
16+
for (int j = i + 1; j < n; j++) {
17+
int desired = -nums[i] - nums[j];
18+
if (indexMap.containsKey(desired) && indexMap.get(desired) != i && indexMap.get(desired) != j) {
19+
List<Integer> triplet = Arrays.asList(nums[i], nums[j], desired);
20+
Collections.sort(triplet);
21+
result.add(triplet);
22+
}
23+
}
24+
}
25+
26+
return new ArrayList<>(result);
27+
}
28+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number[][]}
4+
*/
5+
var threeSum = function(nums) {
6+
const indexMap = new Map();
7+
const result = new Set();
8+
const n = nums.length;
9+
10+
// Build the index map
11+
for (let i = 0; i < n; i++) {
12+
indexMap.set(nums[i], i);
13+
}
14+
15+
// Iterate over each pair
16+
for (let i = 0; i < n; i++) {
17+
for (let j = i + 1; j < n; j++) {
18+
const desired = -nums[i] - nums[j];
19+
if (indexMap.has(desired) && indexMap.get(desired) !== i && indexMap.get(desired) !== j) {
20+
const triplet = [nums[i], nums[j], desired].sort((a, b) => a - b);
21+
result.add(triplet.toString());
22+
}
23+
}
24+
}
25+
26+
return Array.from(result, str => str.split(',').map(Number));
27+
};
File renamed without changes.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include <vector>
2+
#include <queue>
3+
4+
class Solution {
5+
public:
6+
vector<double> averageOfLevels(TreeNode* root) {
7+
vector<double> avgs;
8+
if (!root) return avgs;
9+
10+
queue<TreeNode*> q;
11+
q.push(root);
12+
13+
while (!q.empty()) {
14+
double sum = 0;
15+
int size = q.size();
16+
for (int i = 0; i < size; ++i) {
17+
TreeNode* node = q.front();
18+
q.pop();
19+
sum += node->val;
20+
21+
if (node->left) {
22+
q.push(node->left);
23+
}
24+
if (node->right) {
25+
q.push(node->right);
26+
}
27+
}
28+
avgs.push_back(sum / size);
29+
}
30+
31+
return avgs;
32+
}
33+
};
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import java.util.*;
2+
3+
public class Solution {
4+
public List<Double> averageOfLevels(TreeNode root) {
5+
List<Double> avgs = new ArrayList<>();
6+
if (root == null) return avgs;
7+
8+
Queue<TreeNode> q = new LinkedList<>();
9+
q.add(root);
10+
11+
while (!q.isEmpty()) {
12+
double sum = 0;
13+
int size = q.size();
14+
for (int i = 0; i < size; i++) {
15+
TreeNode node = q.poll();
16+
sum += node.val;
17+
18+
if (node.left != null) {
19+
q.add(node.left);
20+
}
21+
if (node.right != null) {
22+
q.add(node.right);
23+
}
24+
}
25+
avgs.add(sum / size);
26+
}
27+
28+
return avgs;
29+
}
30+
}
31+
32+
class TreeNode {
33+
int val;
34+
TreeNode left;
35+
TreeNode right;
36+
TreeNode() {}
37+
TreeNode(int val) { this.val = val; }
38+
TreeNode(int val, TreeNode left, TreeNode right) {
39+
this.val = val;
40+
this.left = left;
41+
this.right = right;
42+
}
43+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val, left, right) {
4+
* this.val = val;
5+
* this.left = left === undefined ? null : left;
6+
* this.right = right === undefined ? null : right;
7+
* }
8+
*/
9+
10+
var averageOfLevels = function(root) {
11+
let avgs = [];
12+
if (root === null) return avgs;
13+
14+
let q = [root];
15+
16+
while (q.length > 0) {
17+
let sum = 0;
18+
let size = q.length;
19+
for (let i = 0; i < size; i++) {
20+
let node = q.shift();
21+
sum += node.val;
22+
23+
if (node.left !== null) {
24+
q.push(node.left);
25+
}
26+
if (node.right !== null) {
27+
q.push(node.right);
28+
}
29+
}
30+
avgs.push(sum / size);
31+
}
32+
33+
return avgs;
34+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
class Solution {
3+
public:
4+
bool isBalanced(TreeNode* root) {
5+
return height(root) != -1;
6+
}
7+
8+
private:
9+
int height(TreeNode* node) {
10+
if (!node) return 0;
11+
12+
int leftHeight = height(node->left);
13+
if (leftHeight == -1) return -1;
14+
15+
int rightHeight = height(node->right);
16+
if (rightHeight == -1) return -1;
17+
18+
if (abs(leftHeight - rightHeight) > 1) {
19+
return -1;
20+
}
21+
22+
return 1 + max(leftHeight, rightHeight);
23+
}
24+
};
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
public class Solution {
2+
public boolean isBalanced(TreeNode root) {
3+
return height(root) != -1;
4+
}
5+
6+
private int height(TreeNode node) {
7+
if (node == null) return 0;
8+
9+
int leftHeight = height(node.left);
10+
if (leftHeight == -1) return -1;
11+
12+
int rightHeight = height(node.right);
13+
if (rightHeight == -1) return -1;
14+
15+
if (Math.abs(leftHeight - rightHeight) > 1) {
16+
return -1;
17+
}
18+
19+
return 1 + Math.max(leftHeight, rightHeight);
20+
}
21+
}
22+
23+
class TreeNode {
24+
int val;
25+
TreeNode left;
26+
TreeNode right;
27+
TreeNode() {}
28+
TreeNode(int val) { this.val = val; }
29+
TreeNode(int val, TreeNode left, TreeNode right) {
30+
this.val = val;
31+
this.left = left;
32+
this.right = right;
33+
}
34+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val, left, right) {
4+
* this.val = val;
5+
* this.left = left === undefined ? null : left;
6+
* this.right = right === undefined ? null : right;
7+
* }
8+
*/
9+
10+
var isBalanced = function(root) {
11+
return height(root) !== -1;
12+
};
13+
14+
function height(node) {
15+
if (node === null) return 0;
16+
17+
const leftHeight = height(node.left);
18+
if (leftHeight === -1) return -1;
19+
20+
const rightHeight = height(node.right);
21+
if (rightHeight === -1) return -1;
22+
23+
if (Math.abs(leftHeight - rightHeight) > 1) {
24+
return -1;
25+
}
26+
27+
return 1 + Math.max(leftHeight, rightHeight);
28+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
#include <vector>
2+
#include <stack>
3+
#include <string>
4+
#include <sstream>
5+
6+
using namespace std;
7+
8+
class Solution {
9+
public:
10+
int calPoints(vector<string>& operations) {
11+
stack<int> stk;
12+
13+
for (const string& op : operations) {
14+
if (op == "+") {
15+
int top = stk.top(); stk.pop();
16+
int newTop = top + stk.top();
17+
stk.push(top);
18+
stk.push(newTop);
19+
} else if (op == "D") {
20+
stk.push(2 * stk.top());
21+
} else if (op == "C") {
22+
stk.pop();
23+
} else {
24+
stk.push(stoi(op));
25+
}
26+
}
27+
28+
int sum = 0;
29+
while (!stk.empty()) {
30+
sum += stk.top(); stk.pop();
31+
}
32+
33+
return sum;
34+
}
35+
};
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import java.util.*;
2+
3+
public class Solution {
4+
public int calPoints(String[] operations) {
5+
Stack<Integer> stack = new Stack<>();
6+
7+
for (String op : operations) {
8+
if (op.equals("+")) {
9+
int top = stack.pop();
10+
int newTop = top + stack.peek();
11+
stack.push(top);
12+
stack.push(newTop);
13+
} else if (op.equals("D")) {
14+
stack.push(stack.peek() * 2);
15+
} else if (op.equals("C")) {
16+
stack.pop();
17+
} else {
18+
stack.push(Integer.parseInt(op));
19+
}
20+
}
21+
22+
int sum = 0;
23+
while (!stack.isEmpty()) {
24+
sum += stack.pop();
25+
}
26+
27+
return sum;
28+
}
29+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* @param {string[]} operations
3+
* @return {number}
4+
*/
5+
var calPoints = function(operations) {
6+
const stack = [];
7+
8+
for (const op of operations) {
9+
if (op === "+") {
10+
const top = stack.pop();
11+
const newTop = top + stack[stack.length - 1];
12+
stack.push(top);
13+
stack.push(newTop);
14+
} else if (op === "D") {
15+
stack.push(stack[stack.length - 1] * 2);
16+
} else if (op === "C") {
17+
stack.pop();
18+
} else {
19+
stack.push(parseInt(op));
20+
}
21+
}
22+
23+
return stack.reduce((acc, val) => acc + val, 0);
24+
};

0 commit comments

Comments
 (0)