mirror of
https://github.com/arkorty/LeetCode.git
synced 2026-03-18 00:57:17 +00:00
Refactor almost everything
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
#include <vector>
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
int maxProfit(std::vector<int> &prices) {
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
#include <unordered_set>
|
||||
#include <vector>
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
bool containsDuplicate(std::vector<int> &nums) {
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
/*
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
int maxSubArray(std::vector<int> &nums) {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
#include <stdbool.h>
|
||||
|
||||
bool isPalindrome(long int a) {
|
||||
if (a < 0)
|
||||
return false;
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
#include <vector>
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
std::vector<int> getRow(int n) {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
#include <vector>
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
std::vector<std::vector<int>>
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
std::vector<int> twoSum(std::vector<int> &nums, int target) {
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
#include <string>
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
int myAtoi(std::string s) {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user