Compare commits
6 Commits
394eb78247
...
92b6e2833e
| Author | SHA1 | Date | |
|---|---|---|---|
| 92b6e2833e | |||
| 39b4c5191e | |||
| 8e567c7789 | |||
| 4fd9dbe8da | |||
| 99a222c319 | |||
| 35a2375f7e |
30
LengthOfLongestSubstring.cs
Normal file
30
LengthOfLongestSubstring.cs
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
public class Solution {
|
||||||
|
public int LengthOfLongestSubstring(string s) {
|
||||||
|
int max = 0;
|
||||||
|
int n = 0;
|
||||||
|
for (int i = 0; i < s.Length; i++)
|
||||||
|
{
|
||||||
|
n++;
|
||||||
|
if (n > 1)
|
||||||
|
{
|
||||||
|
n -= FindDuplicateCharacter(s.Substring(i - n + 1, n)) + 1;
|
||||||
|
}
|
||||||
|
if (n > max) {
|
||||||
|
max = n;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return max;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int FindDuplicateCharacter(string s) {
|
||||||
|
char last = s[s.Length - 1];
|
||||||
|
for (int i = 0; i < s.Length - 1; i++)
|
||||||
|
{
|
||||||
|
if (s[i] == last)
|
||||||
|
{
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
40
LongestPalindromicSubstring.cpp
Normal file
40
LongestPalindromicSubstring.cpp
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
#include <string>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
class Solution
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
string longestPalindrome(string s)
|
||||||
|
{
|
||||||
|
int max{};
|
||||||
|
int index{};
|
||||||
|
for (int i = 0; i < s.length(); i++)
|
||||||
|
{
|
||||||
|
int j = 0;
|
||||||
|
while (i - j >= 0 && i + j < s.length() && s[i - j] == s[i + j])
|
||||||
|
{
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
if (j * 2 - 1 > max)
|
||||||
|
{
|
||||||
|
max = j * 2 - 1;
|
||||||
|
index = i;
|
||||||
|
}
|
||||||
|
j = 0;
|
||||||
|
while (i - j >= 0 && i + j + 1 < s.length() && s[i - j] == s[i + j + 1])
|
||||||
|
{
|
||||||
|
j++;
|
||||||
|
}
|
||||||
|
if (j * 2 > max)
|
||||||
|
{
|
||||||
|
max = j * 2;
|
||||||
|
index = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (max % 2 == 0)
|
||||||
|
{
|
||||||
|
return s.substr(index - max / 2 + 1, max);
|
||||||
|
}
|
||||||
|
return s.substr(index - max / 2, max);
|
||||||
|
}
|
||||||
|
};
|
||||||
49
MedianOfTwoSortedArrays.cs
Normal file
49
MedianOfTwoSortedArrays.cs
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
public class Solution {
|
||||||
|
public double FindMedianSortedArrays(int[] nums1, int[] nums2) {
|
||||||
|
int middle = (nums1.Length + nums2.Length + 1) / 2 - 1;
|
||||||
|
int i = 0, j = 0;
|
||||||
|
while (i + j < middle)
|
||||||
|
{
|
||||||
|
if (i == nums1.Length || j == nums2.Length) {
|
||||||
|
return OneArrayLeft(nums1, nums2, i, j, middle);
|
||||||
|
}
|
||||||
|
CompareNumbers(nums1, nums2, ref i, ref j);
|
||||||
|
}
|
||||||
|
if (i == nums1.Length || j == nums2.Length) {
|
||||||
|
return OneArrayLeft(nums1, nums2, i, j, middle);
|
||||||
|
}
|
||||||
|
if (2 * middle + 2 == nums1.Length + nums2.Length) {
|
||||||
|
double result = CompareNumbers(nums1, nums2, ref i, ref j);
|
||||||
|
if (i == nums1.Length) {
|
||||||
|
return (result + nums2[j]) / 2.0;
|
||||||
|
}
|
||||||
|
if (j == nums2.Length) {
|
||||||
|
return (result + nums1[i]) / 2.0;
|
||||||
|
}
|
||||||
|
return (result + CompareNumbers(nums1, nums2, ref i, ref j)) / 2.0;
|
||||||
|
}
|
||||||
|
return nums1[i] < nums2[j] ? nums1[i] : nums2[j];
|
||||||
|
}
|
||||||
|
|
||||||
|
public int CompareNumbers(int[] nums1, int[] nums2, ref int i, ref int j) {
|
||||||
|
if (nums1[i] < nums2[j]) {
|
||||||
|
return nums1[++i - 1];
|
||||||
|
}
|
||||||
|
return nums2[++j - 1];
|
||||||
|
}
|
||||||
|
|
||||||
|
public double OneArrayLeft(int[] nums1, int[] nums2, int i, int j, int middle) {
|
||||||
|
if (i == nums1.Length) {
|
||||||
|
j += middle - i - j;
|
||||||
|
if (2 * middle + 2 == nums1.Length + nums2.Length) {
|
||||||
|
return (nums2[j] + nums2[j + 1]) / 2.0;
|
||||||
|
}
|
||||||
|
return nums2[j];
|
||||||
|
}
|
||||||
|
i += middle - i - j;
|
||||||
|
if (2 * middle + 2 == nums1.Length + nums2.Length) {
|
||||||
|
return (nums1[i] + nums1[i + 1]) / 2.0;
|
||||||
|
}
|
||||||
|
return nums1[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
26
TwoSum.cs
Normal file
26
TwoSum.cs
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
|
public class Solution {
|
||||||
|
public int[] TwoSum(int[] nums, int target) {
|
||||||
|
int[] sorted = new int[nums.Length];
|
||||||
|
nums.CopyTo(sorted, 0);
|
||||||
|
Array.Sort(sorted);
|
||||||
|
int i = 0, j = sorted.Length - 1;
|
||||||
|
while (sorted[i] + sorted[j] != target) {
|
||||||
|
while (sorted[i] + sorted[j] > target) {
|
||||||
|
j--;
|
||||||
|
}
|
||||||
|
while (sorted[i] + sorted[j] < target) {
|
||||||
|
i++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
int[] result = new int[2];
|
||||||
|
int r = 0;
|
||||||
|
for (int index = 0; r < 2; index++) {
|
||||||
|
if (nums[index] == sorted[i] || nums[index] == sorted[j]) {
|
||||||
|
result[r++] = index;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
44
ValidateBinarySearchTree.cpp
Normal file
44
ValidateBinarySearchTree.cpp
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
#include <bits/stdc++.h>
|
||||||
|
|
||||||
|
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:
|
||||||
|
bool isValidBST(TreeNode* root) {
|
||||||
|
if (root != nullptr) {
|
||||||
|
return valid(root, INT_MIN, INT_MAX, false, false);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
bool valid(TreeNode* t, int min, int max, bool wasMin, bool wasMax) {
|
||||||
|
if ((wasMin && t->val <= min) || (wasMax && t->val >= max)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (t->left != nullptr) {
|
||||||
|
bool l = valid(t->left, min, t->val, wasMin, true);
|
||||||
|
if (t->right != nullptr) {
|
||||||
|
return l && valid(t->right, t->val, max, true, wasMax);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return l;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if (t->right != nullptr) {
|
||||||
|
return valid(t->right, t->val, max, true, wasMax);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
27
ZigZagConversion.cpp
Normal file
27
ZigZagConversion.cpp
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
#include <string>
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
string convert(string s, int numRows) {
|
||||||
|
if (numRows == 1) return s;
|
||||||
|
string* zigzag{ new string[numRows]{ } };
|
||||||
|
int n{ };
|
||||||
|
bool down{ };
|
||||||
|
for (int i = 0; i < s.length(); i++)
|
||||||
|
{
|
||||||
|
zigzag[n] += s[i];
|
||||||
|
if (n == numRows - 1|| n == 0) {
|
||||||
|
down = !down;
|
||||||
|
}
|
||||||
|
n += down ? 1 : -1;
|
||||||
|
}
|
||||||
|
string result{ };
|
||||||
|
for (int i = 0; i < numRows; i++)
|
||||||
|
{
|
||||||
|
result += zigzag[i];
|
||||||
|
}
|
||||||
|
delete[] zigzag;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
};
|
||||||
Loading…
x
Reference in New Issue
Block a user