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,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) {
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
#include <unordered_set>
|
||||
#include <vector>
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
bool containsDuplicate(std::vector<int> &nums) {
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
#include <unordered_set>
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
public:
|
||||
/*
|
||||
int firstUniqChar(std::string line) {
|
||||
std::unordered_map<char, int[2]> hmap;
|
||||
|
||||
@@ -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); }
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
#include <algorithm>
|
||||
#include <vector>
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
public:
|
||||
/*
|
||||
std::vector<int> intersect(std::vector<int> &nums1,
|
||||
std::vector<int> &nums2) {
|
||||
|
||||
@@ -9,7 +9,7 @@ struct TreeNode {
|
||||
};
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
public:
|
||||
TreeNode *invertTree(TreeNode *root) {
|
||||
if (root != nullptr) {
|
||||
invertTree(root->left);
|
||||
|
||||
@@ -8,7 +8,7 @@ struct ListNode {
|
||||
};
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
public:
|
||||
// bool hasCycle(ListNode *head) {
|
||||
// std::unordered_map<ListNode *, int> hmap;
|
||||
// ListNode *curr = head;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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,5 +1,8 @@
|
||||
#include <cstdint>
|
||||
#include <vector>
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
public:
|
||||
int maxSubArray(std::vector<int> &nums) {
|
||||
int max = INT32_MIN;
|
||||
int tmax = 0;
|
||||
|
||||
@@ -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];
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
#include <stdbool.h>
|
||||
|
||||
bool isPalindrome(long int a) {
|
||||
if (a < 0)
|
||||
return false;
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#include <vector>
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
public:
|
||||
std::vector<int> getRow(int n) {
|
||||
std::vector<int> prow = {1};
|
||||
|
||||
|
||||
@@ -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];
|
||||
|
||||
|
||||
@@ -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++()) {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -7,7 +7,7 @@ struct ListNode {
|
||||
};
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
public:
|
||||
ListNode *removeElements(ListNode *head, int val) {
|
||||
if (head == nullptr) {
|
||||
return nullptr;
|
||||
|
||||
@@ -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,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();
|
||||
|
||||
@@ -7,7 +7,7 @@ struct ListNode {
|
||||
};
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
public:
|
||||
ListNode *reverseList(ListNode *head) {
|
||||
ListNode *prev = nullptr, *next = nullptr, *curr = head;
|
||||
while (curr != nullptr) {
|
||||
|
||||
@@ -9,7 +9,7 @@ struct TreeNode {
|
||||
};
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
public:
|
||||
TreeNode *searchBST(TreeNode *root, int val) {
|
||||
if (root == nullptr) {
|
||||
return nullptr;
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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];
|
||||
|
||||
@@ -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++()) {
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
#include <string>
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
public:
|
||||
int longestValidParentheses(std::string line) {
|
||||
std::stack<int> shelf;
|
||||
shelf.push(-1);
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
#include <vector>
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
public:
|
||||
std::array<int, 2> nextEmpty(std::vector<std::vector<char>> &board) {
|
||||
for (int i = 0; i < 9; ++i) {
|
||||
for (int j = 0; j < 9; ++j) {
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
#include <vector>
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
public:
|
||||
int maxArea(std::vector<int> &height) {
|
||||
int left = 0;
|
||||
int right = height.size() - 1;
|
||||
|
||||
@@ -9,7 +9,7 @@ struct TreeNode {
|
||||
};
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
public:
|
||||
TreeNode *insertIntoBST(TreeNode *root, int val) {
|
||||
TreeNode *node = root;
|
||||
while (node != nullptr) {
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
#include <vector>
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
public:
|
||||
std::vector<std::string> letterCombinations(std::string digits) {
|
||||
std::string letters[] = {"abc", "def", "ghi", "jkl",
|
||||
"mno", "pqrs", "tuv", "wxyz"};
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#include <string>
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
public:
|
||||
std::string longestPalindrome(std::string line) {
|
||||
if (line.size() < 2) {
|
||||
return line;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
#include <vector>
|
||||
|
||||
class Solution {
|
||||
private:
|
||||
private:
|
||||
bool binarySearch(std::vector<int> &nums, int start, int end, int target) {
|
||||
while (end >= start) {
|
||||
int middle = start + (end - start) / 2;
|
||||
@@ -36,7 +36,7 @@ class Solution {
|
||||
}
|
||||
*/
|
||||
|
||||
public:
|
||||
public:
|
||||
bool searchMatrix(std::vector<std::vector<int>> &matrix, int target) {
|
||||
int row = matrix.size();
|
||||
int col = matrix[0].size();
|
||||
|
||||
@@ -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,5 +1,7 @@
|
||||
#include <string>
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
public:
|
||||
int myAtoi(std::string s) {
|
||||
|
||||
unsigned long value = 0;
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#include <vector>
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
public:
|
||||
std::vector<int> twoSum(std::vector<int> &numbers, int target) {
|
||||
int j = -1;
|
||||
for (int i = 0; i < numbers.size(); ++i) {
|
||||
@@ -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