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,3 +1,5 @@
#include <vector>
class Solution {
public:
int maxProfit(std::vector<int> &prices) {

View File

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

View File

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

View File

@@ -1,3 +1,6 @@
#include <algorithm>
#include <vector>
class Solution {
public:
/*

View File

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

View File

@@ -5,15 +5,17 @@ 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,3 +1,6 @@
#include <cstdint>
#include <vector>
class Solution {
public:
int maxSubArray(std::vector<int> &nums) {

View File

@@ -1,3 +1,6 @@
#include <algorithm>
#include <vector>
class Solution {
public:
void merge(std::vector<int> &nums1, int m, std::vector<int> &nums2, int n) {

View File

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

View File

@@ -1,3 +1,5 @@
#include <vector>
class Solution {
public:
std::vector<int> getRow(int n) {

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,3 +1,5 @@
#include <vector>
class Solution {
public:
std::vector<std::vector<int>>

View File

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

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) {
if (!left) {
return right;

View File

@@ -1,3 +1,5 @@
#include <string>
class Solution {
public:
int myAtoi(std::string s) {

View File

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