Refactor almost everything

This commit is contained in:
Arkaprabha Chakraborty
2022-08-02 12:21:43 +05:30
parent ccc376aea0
commit cab1180397
39 changed files with 117 additions and 54 deletions

View File

@@ -1,5 +1,7 @@
#include <vector>
class Solution {
public:
public:
int maxProfit(std::vector<int> &prices) {
int prof = 0;
for (int i = prices.size() - 1, max = 0; i >= 0; --i) {

View File

@@ -11,7 +11,7 @@ struct TreeNode {
};
class Solution {
private:
private:
void traverse(std::vector<int> &list, TreeNode *root) {
if (root == nullptr) {
return;
@@ -22,7 +22,7 @@ class Solution {
}
}
public:
public:
std::vector<int> postorderTraversal(TreeNode *root) {
std::vector<int> list;
traverse(list, root);

View File

@@ -11,7 +11,7 @@ struct TreeNode {
};
class Solution {
private:
private:
void traverse(std::vector<int> &list, TreeNode *root) {
if (root == nullptr) {
return;
@@ -22,7 +22,7 @@ class Solution {
}
}
public:
public:
std::vector<int> preorderTraversal(TreeNode *root) {
std::vector<int> list;
traverse(list, root);

View File

@@ -1,3 +1,6 @@
#include <unordered_set>
#include <vector>
class Solution {
public:
bool containsDuplicate(std::vector<int> &nums) {

View File

@@ -2,7 +2,7 @@ class Solution {
public int titleToNumber(String ttl) {
int num = 0;
for (int i = 0; i < ttl.length(); i++)
num += Math.pow(26, i) * (int) (ttl.charAt(ttl.length() - i - 1) - 64);
num += Math.pow(26, i) * (int)(ttl.charAt(ttl.length() - i - 1) - 64);
return num;
}

View File

@@ -2,9 +2,8 @@ class Solution {
public String convertToTitle(int num) {
StringBuilder ttl = new StringBuilder();
while (num > 0) {
--num;
char tba = (char) (num % 26 + 65);
while (num-- > 0) {
char tba = (char)(num % 26 + 65);
ttl.insert(0, tba);
num /= 26;
}

View File

@@ -2,7 +2,7 @@
#include <unordered_set>
class Solution {
public:
public:
/*
int firstUniqChar(std::string line) {
std::unordered_map<char, int[2]> hmap;

View File

@@ -1,11 +1,11 @@
#include <stack>
class MyQueue {
private:
private:
std::stack<int> shelfA;
std::stack<int> shelfB;
public:
public:
MyQueue() {}
void push(int data) { shelfA.push(data); }

View File

@@ -1,5 +1,8 @@
#include <algorithm>
#include <vector>
class Solution {
public:
public:
/*
std::vector<int> intersect(std::vector<int> &nums1,
std::vector<int> &nums2) {

View File

@@ -9,7 +9,7 @@ struct TreeNode {
};
class Solution {
public:
public:
TreeNode *invertTree(TreeNode *root) {
if (root != nullptr) {
invertTree(root->left);

View File

@@ -8,7 +8,7 @@ struct ListNode {
};
class Solution {
public:
public:
// bool hasCycle(ListNode *head) {
// std::unordered_map<ListNode *, int> hmap;
// ListNode *curr = head;

View File

@@ -9,11 +9,11 @@ struct TreeNode {
};
class Solution {
private:
void maxDepth(TreeNode *root, int index, int *max) {
private:
void maxDepth(TreeNode *root, int index, int &max) {
if (root == nullptr) {
if (index > *max) {
*max = index;
if (index > max) {
max = index;
}
return;
@@ -23,10 +23,10 @@ class Solution {
}
}
public:
public:
int maxDepth(TreeNode *root) {
int max = 0;
maxDepth(root, 0, &max);
maxDepth(root, 0, max);
return max;
}

View File

@@ -5,14 +5,16 @@ class Solution {
int ret = 0;
for (boolean flag = true; words.hasMoreTokens(); flag = true) {
String word = words.nextToken();
for (int i = 0; i < chars.length(); i++)
for (int i = 0; i < chars.length(); i++) {
if (word.indexOf(chars.charAt(i)) > -1) {
flag = false;
break;
}
}
if (flag)
if (flag) {
++ret;
}
}
return ret;

View File

@@ -1,5 +1,8 @@
#include <cstdint>
#include <vector>
class Solution {
public:
public:
int maxSubArray(std::vector<int> &nums) {
int max = INT32_MIN;
int tmax = 0;

View File

@@ -1,5 +1,8 @@
#include <algorithm>
#include <vector>
class Solution {
public:
public:
void merge(std::vector<int> &nums1, int m, std::vector<int> &nums2, int n) {
for (int i = 0; i < n; ++i) {
nums1[m + i] = nums2[i];

View File

@@ -1,3 +1,5 @@
#include <stdbool.h>
bool isPalindrome(long int a) {
if (a < 0)
return false;

View File

@@ -1,5 +1,7 @@
#include <vector>
class Solution {
public:
public:
std::vector<int> getRow(int n) {
std::vector<int> prow = {1};

View File

@@ -2,7 +2,7 @@
#include <unordered_map>
class Solution {
public:
public:
bool canConstruct(std::string ransomNote, std::string magazine) {
std::unordered_map<char, int> hmap[2];

View File

@@ -3,7 +3,7 @@
#include <vector>
class Solution {
public:
public:
int removeDuplicates(std::vector<int> &nums) {
int k = 0, l = INT32_MIN;
for (auto iter = nums.begin(); iter != nums.end(); iter.operator++()) {

View File

@@ -7,7 +7,7 @@ struct ListNode {
};
class Solution {
private:
private:
ListNode *removeElements(ListNode *head, int val) {
if (head == nullptr) {
return nullptr;
@@ -19,7 +19,7 @@ class Solution {
}
}
public:
public:
ListNode *deleteDuplicates(ListNode *head) {
for (ListNode *curr = head; curr != nullptr && curr->next != nullptr;
curr = curr->next) {

View File

@@ -7,7 +7,7 @@ struct ListNode {
};
class Solution {
public:
public:
ListNode *removeElements(ListNode *head, int val) {
if (head == nullptr) {
return nullptr;

View File

@@ -0,0 +1,27 @@
#include <string>
class Solution {
public:
bool repeatedSubstringPattern(std::string s) {
for (int i = 1; i <= s.size() / 2; ++i) {
std::string sl(s.substr(0, i));
if (s.size() % i == 0) {
bool found = false;
for (int j = 1; j < s.size() / i; ++j) {
if (s.find(sl, sl.size() * j) == sl.size() * j) {
found = true;
} else {
found = false;
break;
}
}
if (found) {
return true;
}
}
}
return false;
}
};

View File

@@ -1,5 +1,7 @@
#include <vector>
class Solution {
public:
public:
std::vector<std::vector<int>>
matrixReshape(std::vector<std::vector<int>> &mat, int r, int c) {
int m = mat.size(), n = mat[0].size();

View File

@@ -7,7 +7,7 @@ struct ListNode {
};
class Solution {
public:
public:
ListNode *reverseList(ListNode *head) {
ListNode *prev = nullptr, *next = nullptr, *curr = head;
while (curr != nullptr) {

View File

@@ -9,7 +9,7 @@ struct TreeNode {
};
class Solution {
public:
public:
TreeNode *searchBST(TreeNode *root, int val) {
if (root == nullptr) {
return nullptr;

View File

@@ -1,5 +1,8 @@
#include <unordered_map>
#include <vector>
class Solution {
public:
public:
std::vector<int> twoSum(std::vector<int> &nums, int target) {
std::unordered_map<int, int> hmap;

View File

@@ -2,7 +2,7 @@
#include <unordered_map>
class Solution {
public:
public:
bool isAnagram(std::string s, std::string t) {
if (s.size() == t.size()) {
std::unordered_map<char, int> hmap[2];

View File

@@ -2,7 +2,7 @@
#include <stack>
class Solution {
public:
public:
bool isValid(std::string line) {
std::stack<char> shelf;
for (auto iter = line.begin(); iter != line.end(); iter.operator++()) {