Refactor almost everything

This commit is contained in:
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 { class Solution {
public: public:
int maxProfit(std::vector<int> &prices) { int maxProfit(std::vector<int> &prices) {

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,8 +2,7 @@ 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

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

View File

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

View File

@@ -1,3 +1,6 @@
#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) {

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

View File

@@ -1,3 +1,6 @@
#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) {

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

View File

@@ -1,3 +1,5 @@
#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) {
@@ -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) {