mirror of
https://github.com/arkorty/LeetCode.git
synced 2026-03-17 16:51:46 +00:00
Initial commit
This commit is contained in:
44
Medium/add-two-numbers/Solution.java
Normal file
44
Medium/add-two-numbers/Solution.java
Normal file
@@ -0,0 +1,44 @@
|
||||
class ListNode {
|
||||
int val;
|
||||
ListNode next;
|
||||
|
||||
ListNode() {
|
||||
}
|
||||
|
||||
ListNode(int val) {
|
||||
this.val = val;
|
||||
}
|
||||
|
||||
ListNode(int val, ListNode next) {
|
||||
this.val = val;
|
||||
this.next = next;
|
||||
}
|
||||
}
|
||||
|
||||
class Solution {
|
||||
public ListNode addTwoNumbers(ListNode list1, ListNode list2) {
|
||||
ListNode head = new ListNode(0);
|
||||
ListNode cur = head;
|
||||
|
||||
int carry = 0;
|
||||
while (list1 != null || list2 != null) {
|
||||
int dig1 = list1 != null ? list1.val : 0;
|
||||
int dig2 = list2 != null ? list2.val : 0;
|
||||
|
||||
int sum = dig1 + dig2 + carry;
|
||||
cur.next = new ListNode(sum % 10);
|
||||
cur = cur.next;
|
||||
carry = sum / 10;
|
||||
|
||||
if (list1 != null)
|
||||
list1 = list1.next;
|
||||
if (list2 != null)
|
||||
list2 = list2.next;
|
||||
}
|
||||
|
||||
if (carry > 0)
|
||||
cur.next = new ListNode(carry);
|
||||
|
||||
return head.next;
|
||||
}
|
||||
}
|
||||
35
Medium/binary-tree-level-order-traversal/solution.cpp
Normal file
35
Medium/binary-tree-level-order-traversal/solution.cpp
Normal file
@@ -0,0 +1,35 @@
|
||||
#include <vector>
|
||||
|
||||
struct TreeNode {
|
||||
int val;
|
||||
TreeNode *left;
|
||||
TreeNode *right;
|
||||
TreeNode() : val(0), left(nullptr), right(nullptr) {}
|
||||
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
|
||||
TreeNode(int x, TreeNode *left, TreeNode *right)
|
||||
: val(x), left(left), right(right) {}
|
||||
};
|
||||
|
||||
class Solution {
|
||||
private:
|
||||
void levelOrder(std::vector<std::vector<int>> &levels, TreeNode *root,
|
||||
int index) {
|
||||
if (root == nullptr) {
|
||||
return;
|
||||
} else if (levels.size() == index) {
|
||||
levels.push_back(std::vector<int>{});
|
||||
}
|
||||
|
||||
levels[index].push_back(root->val);
|
||||
levelOrder(levels, root->left, index + 1);
|
||||
levelOrder(levels, root->right, index + 1);
|
||||
}
|
||||
|
||||
public:
|
||||
std::vector<std::vector<int>> levelOrder(TreeNode *root) {
|
||||
std::vector<std::vector<int>> levels;
|
||||
levelOrder(levels, root, 0);
|
||||
|
||||
return levels;
|
||||
}
|
||||
};
|
||||
26
Medium/container-with-most-water/solution.cpp
Normal file
26
Medium/container-with-most-water/solution.cpp
Normal file
@@ -0,0 +1,26 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
int maxArea(std::vector<int> &height) {
|
||||
int left = 0;
|
||||
int right = height.size() - 1;
|
||||
int maxArea = 0;
|
||||
int curArea = 0;
|
||||
while (left < right) {
|
||||
curArea =
|
||||
(right - left) *
|
||||
(height[left] < height[right] ? height[left] : height[right]);
|
||||
maxArea = maxArea > curArea ? maxArea : curArea;
|
||||
|
||||
if (height[left] > height[right]) {
|
||||
--right;
|
||||
} else {
|
||||
++left;
|
||||
}
|
||||
}
|
||||
|
||||
return maxArea;
|
||||
}
|
||||
};
|
||||
25
Medium/equal-row-and-column-pairs/solution.cpp
Normal file
25
Medium/equal-row-and-column-pairs/solution.cpp
Normal file
@@ -0,0 +1,25 @@
|
||||
#include <vector>
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
int equalPairs(std::vector<std::vector<int>> &grid) {
|
||||
int count = 0;
|
||||
for (int i = 0; i < grid.size(); ++i) {
|
||||
for (int j = 0; j < grid.size(); ++j) {
|
||||
bool match = true;
|
||||
for (int k = 0; k < grid.size(); ++k) {
|
||||
if (grid[i][k] != grid[k][j]) {
|
||||
match = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (match) {
|
||||
++count;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return count;
|
||||
}
|
||||
};
|
||||
31
Medium/insert-into-a-binary-search-tree/solution.cpp
Normal file
31
Medium/insert-into-a-binary-search-tree/solution.cpp
Normal file
@@ -0,0 +1,31 @@
|
||||
struct TreeNode {
|
||||
int val;
|
||||
TreeNode *left;
|
||||
TreeNode *right;
|
||||
TreeNode() : val(0), left(nullptr), right(nullptr) {}
|
||||
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
|
||||
TreeNode(int x, TreeNode *left, TreeNode *right)
|
||||
: val(x), left(left), right(right) {}
|
||||
};
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
TreeNode *insertIntoBST(TreeNode *root, int val) {
|
||||
TreeNode *node = root;
|
||||
while (node != nullptr) {
|
||||
if (val < node->val && node->left == nullptr) {
|
||||
node->left = new TreeNode(val);
|
||||
break;
|
||||
} else if (val > node->val && node->right == nullptr) {
|
||||
node->right = new TreeNode(val);
|
||||
break;
|
||||
} else if (val > node->val) {
|
||||
node = node->right;
|
||||
} else if (val < node->val) {
|
||||
node = node->left;
|
||||
}
|
||||
}
|
||||
|
||||
return root != nullptr ? root : new TreeNode(val);
|
||||
}
|
||||
};
|
||||
68
Medium/letter-combinations-of-a-phone-number/solution.cpp
Normal file
68
Medium/letter-combinations-of-a-phone-number/solution.cpp
Normal file
@@ -0,0 +1,68 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
std::vector<std::string> letterCombinations(std::string digits) {
|
||||
std::string letters[] = {"abc", "def", "ghi", "jkl",
|
||||
"mno", "pqrs", "tuv", "wxyz"};
|
||||
|
||||
std::vector<std::string> finvec;
|
||||
|
||||
if (digits.length() == 1) {
|
||||
std::string tmplet = letters[(int)digits[0] - 50];
|
||||
|
||||
for (int i = 0; i < tmplet.length(); ++i) {
|
||||
finvec.push_back(std::string("") + tmplet[i]);
|
||||
}
|
||||
}
|
||||
|
||||
else if (digits.length() == 2) {
|
||||
std::string tmplet_0 = letters[(int)digits[0] - 50];
|
||||
std::string tmplet_1 = letters[(int)digits[1] - 50];
|
||||
|
||||
for (int i = 0; i < tmplet_0.length(); ++i) {
|
||||
for (int j = 0; j < tmplet_1.length(); ++j) {
|
||||
finvec.push_back(std::string("") + tmplet_0[i] +
|
||||
tmplet_1[j]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
else if (digits.length() == 3) {
|
||||
std::string tmplet_0 = letters[(int)digits[0] - 50];
|
||||
std::string tmplet_1 = letters[(int)digits[1] - 50];
|
||||
std::string tmplet_2 = letters[(int)digits[2] - 50];
|
||||
|
||||
for (int i = 0; i < tmplet_0.length(); ++i) {
|
||||
for (int j = 0; j < tmplet_1.length(); ++j) {
|
||||
for (int k = 0; k < tmplet_2.length(); ++k) {
|
||||
finvec.push_back(std::string("") + tmplet_0[i] +
|
||||
tmplet_1[j] + tmplet_2[k]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
else if (digits.length() == 4) {
|
||||
std::string tmplet_0 = letters[(int)digits[0] - 50];
|
||||
std::string tmplet_1 = letters[(int)digits[1] - 50];
|
||||
std::string tmplet_2 = letters[(int)digits[2] - 50];
|
||||
std::string tmplet_3 = letters[(int)digits[3] - 50];
|
||||
|
||||
for (int i = 0; i < tmplet_0.length(); ++i) {
|
||||
for (int j = 0; j < tmplet_1.length(); ++j) {
|
||||
for (int k = 0; k < tmplet_2.length(); ++k) {
|
||||
for (int l = 0; l < tmplet_3.length(); ++l) {
|
||||
finvec.push_back(std::string("") + tmplet_0[i] +
|
||||
tmplet_1[j] + tmplet_2[k] +
|
||||
tmplet_3[l]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return finvec;
|
||||
}
|
||||
};
|
||||
28
Medium/longest-palindromic-substring/Solution.java
Normal file
28
Medium/longest-palindromic-substring/Solution.java
Normal file
@@ -0,0 +1,28 @@
|
||||
class Solution {
|
||||
public String longestPalindrome(String line) {
|
||||
if (line.length() < 2) {
|
||||
return line;
|
||||
} else {
|
||||
int start = 0, size = 0, left = 0, right = 0, middle = 0;
|
||||
while (middle < line.length()) {
|
||||
left = right = middle;
|
||||
while (right < line.length() - 1 && line.charAt(right) == line.charAt(right + 1)) {
|
||||
++right;
|
||||
}
|
||||
|
||||
middle = right + 1;
|
||||
while (left > 0 && right < line.length() - 1 && line.charAt(left - 1) == line.charAt(right + 1)) {
|
||||
--left;
|
||||
++right;
|
||||
}
|
||||
|
||||
if (right - left + 1 > size) {
|
||||
start = left;
|
||||
size = right - left + 1;
|
||||
}
|
||||
}
|
||||
|
||||
return line.substring(start, start + size);
|
||||
}
|
||||
}
|
||||
}
|
||||
33
Medium/longest-palindromic-substring/solution.cpp
Normal file
33
Medium/longest-palindromic-substring/solution.cpp
Normal file
@@ -0,0 +1,33 @@
|
||||
#include <string>
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
std::string longestPalindrome(std::string line) {
|
||||
if (line.size() < 2) {
|
||||
return line;
|
||||
} else {
|
||||
int size = 0, start = 0, left = 0, right = 0, middle = 0;
|
||||
while (middle < line.size()) {
|
||||
left = middle, right = middle;
|
||||
while (right < line.size() - 1 &&
|
||||
line[right] == line[right + 1]) {
|
||||
++right;
|
||||
}
|
||||
|
||||
middle = right + 1;
|
||||
while (right < line.size() - 1 && left > 0 &&
|
||||
line[right + 1] == line[left - 1]) {
|
||||
--left;
|
||||
++right;
|
||||
}
|
||||
|
||||
if (right - left + 1 > size) {
|
||||
start = left;
|
||||
size = right - left + 1;
|
||||
}
|
||||
}
|
||||
|
||||
return line.substr(start, size);
|
||||
}
|
||||
}
|
||||
};
|
||||
13
Medium/reverse-integer/Solution.java
Normal file
13
Medium/reverse-integer/Solution.java
Normal file
@@ -0,0 +1,13 @@
|
||||
class IntegerReverse {
|
||||
public int reverse(int num) {
|
||||
if (num == 0)
|
||||
return 0;
|
||||
|
||||
try {
|
||||
return Integer.parseInt(new StringBuilder(Integer.toString(Math.abs(num))).reverse().toString())
|
||||
* (num < 0 ? -1 : 1);
|
||||
} catch (NumberFormatException err) {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
53
Medium/search-a-2d-matrix/solution.cpp
Normal file
53
Medium/search-a-2d-matrix/solution.cpp
Normal file
@@ -0,0 +1,53 @@
|
||||
#include <vector>
|
||||
|
||||
class Solution {
|
||||
private:
|
||||
bool binarySearch(std::vector<int> &nums, int start, int end, int target) {
|
||||
while (end >= start) {
|
||||
int middle = start + (end - start) / 2;
|
||||
|
||||
if (nums[middle] == target) {
|
||||
return true;
|
||||
} else if (nums[middle] > target) {
|
||||
end = middle - 1;
|
||||
} else {
|
||||
start = middle + 1;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
bool binarySearch(std::vector<int> &nums, int start, int end, int target) {
|
||||
if (end >= start) {
|
||||
int middle = start + (end - start) / 2;
|
||||
|
||||
if (nums[middle] == target) {
|
||||
return true;
|
||||
} else if (nums[middle] > target) {
|
||||
return binarySearch(nums, start, middle - 1, target);
|
||||
} else {
|
||||
return binarySearch(nums, middle + 1, end, target);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
*/
|
||||
|
||||
public:
|
||||
bool searchMatrix(std::vector<std::vector<int>> &matrix, int target) {
|
||||
int row = matrix.size();
|
||||
int col = matrix[0].size();
|
||||
for (int i = 0; i < row; ++i) {
|
||||
if (target > matrix[i][col - 1]) {
|
||||
continue;
|
||||
} else {
|
||||
return binarySearch(matrix[i], 0, col - 1, target);
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
};
|
||||
9
Medium/single-element-in-a-sorted-array/Solution.java
Normal file
9
Medium/single-element-in-a-sorted-array/Solution.java
Normal file
@@ -0,0 +1,9 @@
|
||||
class Solution {
|
||||
public int singleNonDuplicate(int[] nums) {
|
||||
for (int i = 0; i < nums.length - 1; i += 2)
|
||||
if (nums[i] != nums[i + 1])
|
||||
return i;
|
||||
|
||||
return nums.length - 1;
|
||||
}
|
||||
}
|
||||
32
Medium/sort-list/solution.c
Normal file
32
Medium/sort-list/solution.c
Normal file
@@ -0,0 +1,32 @@
|
||||
struct ListNode *merge(struct ListNode *left, struct ListNode *right) {
|
||||
if (!left) {
|
||||
return right;
|
||||
}
|
||||
if (!right) {
|
||||
return left;
|
||||
}
|
||||
|
||||
if (left->val < right->val) {
|
||||
left->next = merge(left->next, right);
|
||||
return left;
|
||||
} else {
|
||||
right->next = merge(left, right->next);
|
||||
return right;
|
||||
}
|
||||
}
|
||||
|
||||
struct ListNode *sortList(struct ListNode *head) {
|
||||
if (!head || !head->next) {
|
||||
return head;
|
||||
}
|
||||
struct ListNode *fast, *midd, *bmid;
|
||||
fast = midd = head;
|
||||
while (fast && fast->next) {
|
||||
bmid = midd;
|
||||
fast = fast->next->next;
|
||||
midd = midd->next;
|
||||
}
|
||||
|
||||
bmid->next = NULL;
|
||||
return merge(sortList(head), sortList(midd));
|
||||
}
|
||||
70
Medium/string-to-integer-atoi/solution.cpp
Normal file
70
Medium/string-to-integer-atoi/solution.cpp
Normal file
@@ -0,0 +1,70 @@
|
||||
class Solution {
|
||||
public:
|
||||
int myAtoi(std::string s) {
|
||||
|
||||
unsigned long value = 0;
|
||||
|
||||
bool found = false, minus = false, signi = false;
|
||||
|
||||
for (int i = 0, j = 0; i <= 200; ++i) {
|
||||
char chr = s[i];
|
||||
|
||||
if (j > 18)
|
||||
break;
|
||||
|
||||
else if ((int)chr == 0)
|
||||
break;
|
||||
|
||||
else if (((int)chr == 43 || (int)chr == 45) && !found) {
|
||||
if ((int)chr == 45)
|
||||
minus = true;
|
||||
found = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
else if (((int)chr == 43 || (int)chr == 45) && found) {
|
||||
break;
|
||||
}
|
||||
|
||||
else if (((int)chr == 46 || ((int)chr >= 65 && (int)chr <= 90) ||
|
||||
((int)chr >= 97 && (int)chr <= 122)) &&
|
||||
!found) {
|
||||
break;
|
||||
}
|
||||
|
||||
else if (((int)chr == 46 || ((int)chr >= 65 && (int)chr <= 90) ||
|
||||
((int)chr >= 97 && (int)chr <= 122)) &&
|
||||
found) {
|
||||
break;
|
||||
}
|
||||
|
||||
else if ((int)chr == 32 && found)
|
||||
break;
|
||||
|
||||
else if ((int)chr >= 48 && (int)chr <= 57) {
|
||||
if ((int)chr != 48)
|
||||
signi = true;
|
||||
|
||||
value = value * 10 + ((int)chr - 48);
|
||||
found = true;
|
||||
|
||||
if (signi)
|
||||
++j;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
else if ((int)chr == 32 && !found)
|
||||
continue;
|
||||
}
|
||||
|
||||
if (value > 2147483648 && minus)
|
||||
return -2147483648;
|
||||
else if (minus)
|
||||
return value * -1;
|
||||
else if (value > 2147483647)
|
||||
return 2147483647;
|
||||
else
|
||||
return value;
|
||||
}
|
||||
};
|
||||
33
Medium/two-sum-ii-input-array-is-sorted/solution.cpp
Normal file
33
Medium/two-sum-ii-input-array-is-sorted/solution.cpp
Normal file
@@ -0,0 +1,33 @@
|
||||
class Solution {
|
||||
public:
|
||||
std::vector<int> twoSum(std::vector<int> &numbers, int target) {
|
||||
int j = -1;
|
||||
for (int i = 0; i < numbers.size(); ++i) {
|
||||
int otherhalf = target - numbers[i];
|
||||
|
||||
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};
|
||||
}
|
||||
}
|
||||
|
||||
return std::vector{-1, -1};
|
||||
}
|
||||
|
||||
int binarySearch(std::vector<int> &numbers, int start, int end, int key) {
|
||||
if (start <= end) {
|
||||
int middle = (start + end) / 2;
|
||||
if (numbers[middle] == key)
|
||||
return middle;
|
||||
else if (numbers[middle] > key)
|
||||
return binarySearch(numbers, start, middle - 1, key);
|
||||
else
|
||||
return binarySearch(numbers, middle + 1, end, key);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
};
|
||||
66
Medium/valid-sudoku/solution.cpp
Normal file
66
Medium/valid-sudoku/solution.cpp
Normal file
@@ -0,0 +1,66 @@
|
||||
#include <iostream>
|
||||
#include <unordered_map>
|
||||
#include <vector>
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
/*
|
||||
bool isValidSudoku(std::vector<std::vector<char>> &board) {
|
||||
for (int r = 0; r < 9; ++r) {
|
||||
for (int c = 0; c < 9; ++c) {
|
||||
char num = board[r][c];
|
||||
if (num != '.') {
|
||||
int count = 0;
|
||||
for (int i = 0; i < 9; ++i) {
|
||||
if (num == board[r][i]) {
|
||||
++count;
|
||||
}
|
||||
if (num == board[i][c]) {
|
||||
++count;
|
||||
}
|
||||
}
|
||||
|
||||
int rof = r - r % 3;
|
||||
int cof = c - c % 3;
|
||||
for (int i = 0; i < 3; ++i) {
|
||||
for (int j = 0; j < 3; ++j) {
|
||||
if (num == board[i + rof][j + cof]) {
|
||||
++count;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (count != 3) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
*/
|
||||
|
||||
bool isValidSudoku(std::vector<std::vector<char>> &board) {
|
||||
std::unordered_map<char, int[9]> count[3];
|
||||
|
||||
for (int i = 0; i < 9; ++i) {
|
||||
for (int j = 0; j < 9; ++j) {
|
||||
char num = board[i][j];
|
||||
|
||||
if (num != '.') {
|
||||
if (count[0][num][i] != 0 || count[1][num][j] != 0 ||
|
||||
count[2][num][i / 3 * 3 + j / 3] != 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
++count[0][num][i];
|
||||
++count[1][num][j];
|
||||
++count[2][num][i / 3 * 3 + j / 3];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
};
|
||||
29
Medium/validate-binary-search-tree/solution.cpp
Normal file
29
Medium/validate-binary-search-tree/solution.cpp
Normal file
@@ -0,0 +1,29 @@
|
||||
struct TreeNode {
|
||||
int val;
|
||||
TreeNode *left;
|
||||
TreeNode *right;
|
||||
TreeNode() : val(0), left(nullptr), right(nullptr) {}
|
||||
TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
|
||||
TreeNode(int x, TreeNode *left, TreeNode *right)
|
||||
: val(x), left(left), right(right) {}
|
||||
};
|
||||
|
||||
class Solution {
|
||||
private:
|
||||
bool isValidBST(TreeNode *root, int *minptr, int *maxptr) {
|
||||
if (root == nullptr) {
|
||||
return true;
|
||||
} else if ((maxptr != nullptr && *maxptr <= root->val) ||
|
||||
(minptr != nullptr && *minptr >= root->val)) {
|
||||
return false;
|
||||
} else {
|
||||
return isValidBST(root->left, minptr, &root->val) &&
|
||||
isValidBST(root->right, &root->val, maxptr);
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
bool isValidBST(TreeNode *root) {
|
||||
return isValidBST(root, nullptr, nullptr);
|
||||
}
|
||||
};
|
||||
26
Medium/zigzag-conversion/Solution.java
Normal file
26
Medium/zigzag-conversion/Solution.java
Normal file
@@ -0,0 +1,26 @@
|
||||
class Solution {
|
||||
public String convert(String str, int rows) {
|
||||
String[] zigzag = new String[rows];
|
||||
for (int i = 0; i < rows; i++)
|
||||
zigzag[i] = "";
|
||||
|
||||
for (int i = 0, j = 0, k = 0; i < str.length(); i++) {
|
||||
zigzag[j] += str.charAt(i);
|
||||
|
||||
if (j == rows - 1)
|
||||
k = 1;
|
||||
if (j == 0)
|
||||
k = 0;
|
||||
if (k == 1 && j > 0)
|
||||
j--;
|
||||
if (k == 0 && j < rows - 1)
|
||||
j++;
|
||||
}
|
||||
|
||||
String ret = "";
|
||||
for (int i = 0; i < rows; i++)
|
||||
ret += zigzag[i];
|
||||
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user