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 { class Solution {
public: public:
int maxProfit(std::vector<int> &prices) { int maxProfit(std::vector<int> &prices) {
int prof = 0; int prof = 0;
for (int i = prices.size() - 1, max = 0; i >= 0; --i) { for (int i = prices.size() - 1, max = 0; i >= 0; --i) {

View File

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

View File

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

View File

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

View File

@@ -2,7 +2,7 @@ class Solution {
public int titleToNumber(String ttl) { public int titleToNumber(String ttl) {
int num = 0; int num = 0;
for (int i = 0; i < ttl.length(); i++) 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; return num;
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -7,7 +7,7 @@ struct ListNode {
}; };
class Solution { class Solution {
public: public:
ListNode *removeElements(ListNode *head, int val) { ListNode *removeElements(ListNode *head, int val) {
if (head == nullptr) { if (head == nullptr) {
return 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 { class Solution {
public: public:
std::vector<std::vector<int>> std::vector<std::vector<int>>
matrixReshape(std::vector<std::vector<int>> &mat, int r, int c) { matrixReshape(std::vector<std::vector<int>> &mat, int r, int c) {
int m = mat.size(), n = mat[0].size(); int m = mat.size(), n = mat[0].size();

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -2,7 +2,7 @@
#include <string> #include <string>
class Solution { class Solution {
public: public:
int longestValidParentheses(std::string line) { int longestValidParentheses(std::string line) {
std::stack<int> shelf; std::stack<int> shelf;
shelf.push(-1); shelf.push(-1);

View File

@@ -2,7 +2,7 @@
#include <vector> #include <vector>
class Solution { class Solution {
public: public:
std::array<int, 2> nextEmpty(std::vector<std::vector<char>> &board) { std::array<int, 2> nextEmpty(std::vector<std::vector<char>> &board) {
for (int i = 0; i < 9; ++i) { for (int i = 0; i < 9; ++i) {
for (int j = 0; j < 9; ++j) { for (int j = 0; j < 9; ++j) {

View File

@@ -2,7 +2,7 @@
#include <vector> #include <vector>
class Solution { class Solution {
public: public:
int maxArea(std::vector<int> &height) { int maxArea(std::vector<int> &height) {
int left = 0; int left = 0;
int right = height.size() - 1; int right = height.size() - 1;

View File

@@ -9,7 +9,7 @@ struct TreeNode {
}; };
class Solution { class Solution {
public: public:
TreeNode *insertIntoBST(TreeNode *root, int val) { TreeNode *insertIntoBST(TreeNode *root, int val) {
TreeNode *node = root; TreeNode *node = root;
while (node != nullptr) { while (node != nullptr) {

View File

@@ -2,9 +2,9 @@
#include <vector> #include <vector>
class Solution { class Solution {
public: public:
std::vector<std::string> letterCombinations(std::string digits) { std::vector<std::string> letterCombinations(std::string digits) {
std::string letters[] = {"abc", "def", "ghi", "jkl", std::string letters[] = {"abc", "def", "ghi", "jkl",
"mno", "pqrs", "tuv", "wxyz"}; "mno", "pqrs", "tuv", "wxyz"};
std::vector<std::string> finvec; std::vector<std::string> finvec;

View File

@@ -1,7 +1,7 @@
#include <string> #include <string>
class Solution { class Solution {
public: public:
std::string longestPalindrome(std::string line) { std::string longestPalindrome(std::string line) {
if (line.size() < 2) { if (line.size() < 2) {
return line; return line;

View File

@@ -1,7 +1,7 @@
#include <vector> #include <vector>
class Solution { class Solution {
private: private:
bool binarySearch(std::vector<int> &nums, int start, int end, int target) { bool binarySearch(std::vector<int> &nums, int start, int end, int target) {
while (end >= start) { while (end >= start) {
int middle = start + (end - start) / 2; int middle = start + (end - start) / 2;
@@ -36,7 +36,7 @@ class Solution {
} }
*/ */
public: public:
bool searchMatrix(std::vector<std::vector<int>> &matrix, int target) { bool searchMatrix(std::vector<std::vector<int>> &matrix, int target) {
int row = matrix.size(); int row = matrix.size();
int col = matrix[0].size(); int col = matrix[0].size();

View File

@@ -1,3 +1,10 @@
#include <stdlib.h>
struct ListNode {
int val;
struct ListNode *next;
};
struct ListNode *merge(struct ListNode *left, struct ListNode *right) { struct ListNode *merge(struct ListNode *left, struct ListNode *right) {
if (!left) { if (!left) {
return right; return right;

View File

@@ -1,5 +1,7 @@
#include <string>
class Solution { class Solution {
public: public:
int myAtoi(std::string s) { int myAtoi(std::string s) {
unsigned long value = 0; unsigned long value = 0;

View File

@@ -1,5 +1,7 @@
#include <vector>
class Solution { class Solution {
public: public:
std::vector<int> twoSum(std::vector<int> &numbers, int target) { std::vector<int> twoSum(std::vector<int> &numbers, int target) {
int j = -1; int j = -1;
for (int i = 0; i < numbers.size(); ++i) { for (int i = 0; i < numbers.size(); ++i) {
@@ -8,14 +10,15 @@ class Solution {
j = binarySearch(numbers, 0, numbers.size() - 1, otherhalf); j = binarySearch(numbers, 0, numbers.size() - 1, otherhalf);
if (j != -1 && i != j) { if (j != -1 && i != j) {
if (i < j) if (i < j) {
return std::vector{i + 1, j + 1}; return {i + 1, j + 1};
else } else {
return std::vector{j + 1, i + 1}; return {j + 1, i + 1};
}
} }
} }
return std::vector{-1, -1}; return {-1, -1};
} }
int binarySearch(std::vector<int> &numbers, int start, int end, int key) { int binarySearch(std::vector<int> &numbers, int start, int end, int key) {