Add solution to 'a lot' of problems

This commit is contained in:
Arkaprabha Chakraborty
2022-08-01 13:28:06 +05:30
parent 20d44dfd5e
commit ccc376aea0
9 changed files with 180 additions and 1 deletions

View File

@@ -0,0 +1,17 @@
#include <vector>
class Solution {
public:
int climbStairs(int n) {
if (n == 0) {
return 0;
}
std::vector<int> shiftTwoFibo{1, 2};
for (int i = 2; i < n; ++i) {
shiftTwoFibo.push_back(shiftTwoFibo[i - 1] + shiftTwoFibo[i - 2]);
}
return shiftTwoFibo[n - 1];
}
};

View File

@@ -0,0 +1,27 @@
#include <cmath>
#include <unordered_map>
#include <vector>
class Solution {
public:
bool containsDuplicate(std::vector<int> &nums, int k) {
std::unordered_map<int, std::vector<int>> unset;
int size = nums.size();
for (int i = 0; i < size; ++i) {
if (unset.find(nums[i]) != unset.end()) {
std::vector<int> indices = unset[nums[i]];
for (int j = 0; j < indices.size(); ++j) {
if (std::abs(indices[j] - i) <= k) {
return true;
}
}
}
unset[nums[i]].push_back(i);
}
return false;
}
};

View File

@@ -1,5 +1,5 @@
class Solution {
public:
public:
bool containsDuplicate(std::vector<int> &nums) {
std::unordered_set<int> unset;

View File

@@ -0,0 +1,38 @@
#include <cstdint>
#include <iostream>
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
class Solution {
public:
void traverse(TreeNode *root, int curlvl, int &minCount) {
if (root == nullptr) {
return;
}
if (root->left == nullptr && root->right == nullptr && curlvl < minCount) {
minCount = curlvl;
}
traverse(root->left, curlvl + 1, minCount);
traverse(root->right, curlvl + 1, minCount);
}
int minDepth(TreeNode *root) {
if (root == nullptr) {
return 0;
}
int minCount = INT32_MAX;
traverse(root, 1, minCount);
return minCount;
}
};

View File

@@ -0,0 +1,17 @@
#include <cmath>
#include <cstdint>
class Solution {
public:
uint32_t reverseBits(uint32_t n) {
uint32_t r = 0;
for (int i = 31; i >= 0; --i) {
if (n / (uint32_t)std::pow(2, i) >= 1) {
n -= (uint32_t)std::pow(2, i);
r += (uint32_t)std::pow(2, 31 - i);
}
}
return r;
}
};

View File

@@ -0,0 +1,21 @@
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode() : val(0), left(nullptr), right(nullptr) {}
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};
class Solution {
public:
bool isSameTree(TreeNode *p, TreeNode *q) {
if (p != nullptr && q != nullptr) {
return p->val == q->val && isSameTree(p->left, q->left) && isSameTree(p->right, q->right);
} else if (p == nullptr ^ q == nullptr) {
return false;
} else {
return true;
}
}
};

View File

@@ -0,0 +1,23 @@
#include <string>
#include <vector>
class Solution {
public:
std::string restoreString(std::string s, std::vector<int> &indices) {
for (int i = 1; i < indices.size(); i++) {
int itmp = indices[i];
char ctmp = s[i];
int j = i - 1;
while (j >= 0 && indices[j] > itmp) {
indices[j + 1] = indices[j];
s[j + 1] = s[j];
j = j - 1;
}
indices[j + 1] = itmp;
s[j + 1] = ctmp;
}
return s;
}
};

View File

@@ -0,0 +1,16 @@
#include <vector>
class Solution {
public:
std::vector<std::vector<int>> transpose(std::vector<std::vector<int>> &matrix) {
std::vector<std::vector<int>> trans(matrix[0].size(), std::vector<int>(matrix.size()));
for (int i = 0; i < matrix.size(); ++i) {
for (int j = 0; j < matrix[0].size(); ++j) {
trans[j][i] = matrix[i][j];
}
}
return trans;
}
};

View File

@@ -0,0 +1,20 @@
class Solution {
public boolean isPalindrome(String s) {
String t = "";
for (int i = 0; i < s.length(); ++i) {
int a = (int)s.charAt(i);
if (a >= (int)'A' && a <= (int)'Z') {
t += (char)(a - (int)'A' + (int)'a');
} else if (a >= (int)'a' && a <= (int)'z') {
t += (char)a;
} else if (a >= (int)'0' && a <= (int)'9') {
t += (char)a;
}
}
StringBuilder ob = new StringBuilder(t);
String r = ob.reverse().toString();
return t.compareTo(r) == 0;
}
}