-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
kaiforme
authored and
kaiforme
committed
Oct 25, 2024
1 parent
f1e32aa
commit 75f6c03
Showing
193 changed files
with
15,763 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
#include <iostream> | ||
#include <vector> | ||
#include <unordered_set> | ||
#include <algorithm> | ||
#include <unordered_map> | ||
|
||
using namespace std; | ||
|
||
class Solution | ||
{ | ||
public: | ||
vector<int> twoSum(vector<int>& nums, int target) | ||
{ | ||
unordered_set<int> uset; | ||
sort(nums.begin(), nums.end()); | ||
int i = 0, j = nums.size() - 1; | ||
while (i < j) | ||
{ | ||
if (nums[i] + nums[j] == target) | ||
{ | ||
uset.insert(i); | ||
uset.insert(j); | ||
} | ||
else if (nums[i] + nums[j] > target) | ||
j--; | ||
else | ||
i++; | ||
} | ||
return vector<int>(uset.begin(), uset.end()); | ||
} | ||
vector<int> twoSum(vector<int>& nums, int target) | ||
{ | ||
int len = nums.size(); | ||
unordered_set<int> uset; | ||
for (int i = 0; i < len; i++) | ||
{ | ||
for (int j = i + 1; j < len; j++) | ||
{ | ||
if (nums[i] + nums[j] == target) | ||
{ | ||
uset.insert(i); | ||
uset.insert(j); | ||
} | ||
} | ||
} | ||
return vector<int>(uset.begin(), uset.end()); | ||
} | ||
// 只要找两个数就行了 | ||
vector<int> twoSum(vector<int>& nums, int target) | ||
{ | ||
unordered_map<int, int> umap; | ||
for (int i = 0; i < nums.size(); i++) | ||
{ | ||
auto iter = umap.find(target - nums[i]); | ||
// 找到 | ||
if (iter != umap.end()) | ||
return { iter->second, i }; | ||
// 未找到 | ||
else | ||
umap.insert(pair<int, int>(nums[i], i)); | ||
} | ||
return {}; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
#include <iostream> | ||
#include <vector> | ||
#include <string> | ||
using namespace std; | ||
|
||
struct ListNode | ||
{ | ||
int val; | ||
ListNode *next; | ||
ListNode() : val(0), next(nullptr) {} | ||
ListNode(int x) : val(x), next(nullptr) {} | ||
ListNode(int x, ListNode *next) : val(x), next(next) {} | ||
}; | ||
|
||
class Solution | ||
{ | ||
public: | ||
ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) | ||
{ | ||
ListNode* head = NULL, * tail = NULL; | ||
int carry = 0; | ||
while (l1 || l2) | ||
{ | ||
int n1 = l1 ? l1->val : 0; //Èôl1Ϊ¿ÕÔòÌî²¹0 | ||
int n2 = l2 ? l2->val : 0; | ||
int sum = n1 + n2 + carry; | ||
if (!head) | ||
head = tail = new ListNode(sum % 10); | ||
else | ||
{ | ||
//Í·²å·¨ | ||
tail->next = new ListNode(sum % 10); | ||
tail = tail->next; | ||
} | ||
carry = sum / 10; | ||
if (l1) | ||
l1 = l1->next; | ||
if (l2) | ||
l2 = l2->next; | ||
} | ||
if (carry > 0) | ||
{ | ||
tail->next = new ListNode(carry); | ||
} | ||
return head; | ||
} | ||
}; | ||
|
||
int main() | ||
{ | ||
ListNode* l1 = new ListNode, * l2 = new ListNode; | ||
vector<int> num1 = { 2, 4, 3 }; | ||
vector<int> num2 = { 5, 6, 4 }; | ||
ListNode* templ1 = l1; | ||
templ1->next = NULL; | ||
templ1->val = num1[0]; | ||
for (int i = 1; i < 3; i++) | ||
{ | ||
ListNode* node = new ListNode; | ||
node->next = NULL; | ||
templ1->next = node; | ||
templ1 = node; | ||
templ1->val = num1[i]; | ||
} | ||
ListNode* templ2 = l2; | ||
templ2->next = NULL; | ||
templ2->val = num2[0]; | ||
for (int i = 1; i < 3; i++) | ||
{ | ||
ListNode* node = new ListNode; | ||
node->next = NULL; | ||
templ2->next = node; | ||
templ2 = node; | ||
templ2->val = num2[i]; | ||
} | ||
Solution S; | ||
ListNode* ans = S.addTwoNumbers(l1, l2); | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
#include <iostream> | ||
#include <string> | ||
#include <unordered_map> | ||
#include <unordered_set> | ||
using namespace std; | ||
/* | ||
class Solution | ||
{ | ||
public: | ||
int lengthOfLongestSubstring(string s) | ||
{ | ||
if (s == "") | ||
return 0; | ||
int len = s.size(); | ||
int max = 1; | ||
for (int i = 0; i < len; i++) | ||
{ | ||
unordered_map<char, int> hashtable; | ||
hashtable[s[i]] = i; //将字符存储到哈希表中 | ||
int sublen = 1; | ||
for (int j = i + 1; j < len; j++) | ||
{ | ||
if (hashtable.find(s[j]) == hashtable.end()) //判断没有重复 | ||
{ | ||
sublen++; | ||
hashtable[s[j]] = j; | ||
} | ||
else | ||
break; | ||
} | ||
if (sublen > max) | ||
max = sublen; | ||
} | ||
return max; | ||
} | ||
}; | ||
*/ | ||
class Solution | ||
{ | ||
public: | ||
int lengthOfLongestSubstring(string s) | ||
{ | ||
unordered_set<char> hashtable; | ||
int r = -1, max = 0; | ||
for (int i = 0; i < s.size(); i++) | ||
{ | ||
if (i > 0) | ||
hashtable.erase(s[i - 1]); | ||
while (r + 1 < s.size() && hashtable.count(s[r + 1]) == 0) //判断是否重复 | ||
{ | ||
hashtable.insert(s[r + 1]); | ||
r++; | ||
} | ||
if (r - i + 1 > max) | ||
max = r - i + 1; | ||
} | ||
return max; | ||
} | ||
}; | ||
int main() | ||
{ | ||
string s = "aaaaa"; | ||
Solution S; | ||
cout << S.lengthOfLongestSubstring(s) << endl; | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
#include <iostream> | ||
#include <vector> | ||
#include <unordered_set> | ||
#include <algorithm> | ||
|
||
using namespace std; | ||
|
||
class Solution | ||
{ | ||
public: | ||
vector<vector<int>> threeSum1(vector<int>& nums) | ||
{ | ||
vector<vector<int>> ans; | ||
int n = nums.size(); | ||
sort(nums.begin(), nums.end()); | ||
for (int i = 0; i < nums.size(); i++) | ||
{ | ||
if (nums[i] > 0) | ||
break; | ||
if (i > 0 && nums[i] == nums[i - 1]) | ||
continue; | ||
unordered_set<int> uset; | ||
for (int j = i + 1; j < nums.size(); j++) | ||
{ | ||
// 说明当前元素 nums[j] 与其前两个元素都相同 | ||
// 这意味着我们已经使用 nums[j - 1] 和 nums[j - 2] 与 nums[i] 组成过三元组 | ||
// 因此 nums[j] 再与 nums[i] 组合会产生重复的三元组 | ||
if (j > i + 2 && nums[j] == nums[j - 1] && nums[j - 1] == nums[j - 2]) | ||
continue; | ||
int c = 0 - (nums[i] + nums[j]); | ||
if (uset.find(c) != uset.end()) | ||
{ | ||
ans.push_back({ nums[i], nums[j], c }); | ||
// 删除元素 | ||
uset.erase(c); | ||
} | ||
else | ||
uset.insert(nums[j]); | ||
} | ||
} | ||
return ans; | ||
} | ||
vector<vector<int>> threeSum2(vector<int>& nums) | ||
{ | ||
vector<vector<int>> ans; | ||
int low, high; | ||
int n = nums.size(); | ||
int target; | ||
sort(nums.begin(), nums.end()); | ||
|
||
for (int i = 0; i < n; i++) | ||
{ | ||
if (nums[i] > 0) | ||
return ans; | ||
if (i > 0 && nums[i] == nums[i - 1]) | ||
continue; | ||
target = 0 - nums[i]; | ||
low = i + 1, high = n - 1; | ||
while (low < high) | ||
{ | ||
if (nums[low] + nums[high] == target) | ||
{ | ||
ans.push_back({ nums[i], nums[low], nums[high] }); | ||
while (low < high && nums[low] == nums[low + 1]) | ||
low++; | ||
while (low < high && nums[high] == nums[high - 1]) | ||
high--; | ||
low++; | ||
high--; | ||
} | ||
else if (nums[low] + nums[high] > target) | ||
high--; | ||
else if (nums[low] + nums[high] < target) | ||
low++; | ||
} | ||
} | ||
return ans; | ||
} | ||
// 哈希表 | ||
vector<vector<int>> threeSum3(vector<int>& nums) | ||
{ | ||
vector<vector<int>> result; | ||
sort(nums.begin(), nums.end()); | ||
// 找出 a + b + c = 0 | ||
// a = nums[i], b = nums[j], c = -(a + b) | ||
// a 和 b 和 c 可以相等 | ||
for (int i = 0; i < nums.size(); i++) | ||
{ | ||
// 第一个元素大于0,不可能等于0 | ||
if (nums[i] > 0) | ||
break; | ||
// 元素a去重 | ||
if (i > 0 && nums[i] == nums[i - 1]) | ||
continue; | ||
unordered_set<int> uset; | ||
for (int j = i + 1; j < nums.size(); j++) | ||
{ | ||
// 元素b去重 | ||
if (j > i + 2 && nums[j] == nums[j - 1] && nums[j - 1] == nums[j - 2]) | ||
continue; | ||
int c = 0 - (nums[i] + nums[j]); | ||
if (uset.find(c) != uset.end()) | ||
{ | ||
// 找到了一个解 | ||
result.push_back({ nums[i], nums[j], c }); | ||
// 元素c去重 | ||
uset.erase(c); | ||
} | ||
else | ||
uset.insert(nums[j]); | ||
} | ||
} | ||
return result; | ||
} | ||
// 双指针 | ||
vector<vector<int>> threeSum(vector<int>& nums) | ||
{ | ||
int n = nums.size(); | ||
sort(nums.begin(), nums.end()); | ||
vector<vector<int>> result; | ||
for (int i = 0; i < n; i++) | ||
{ | ||
if (nums[i] > 0) | ||
break; | ||
int a = nums[i]; | ||
if (i > 0 && nums[i] == nums[i - 1]) | ||
continue; | ||
int left = i + 1, right = n - 1; | ||
while (left < right) | ||
{ | ||
if (left > i + 1 && nums[left] == nums[left - 1]) | ||
{ | ||
left++; | ||
continue; | ||
} | ||
if (right < n - 1 && nums[right] == nums[right + 1]) | ||
{ | ||
right--; | ||
continue; | ||
} | ||
if (a + nums[left] + nums[right] == 0) | ||
{ | ||
result.push_back({ a, nums[left], nums[right] }); | ||
left++; | ||
right--; | ||
} | ||
else if (a + nums[left] + nums[right] > 0) | ||
right--; | ||
else if (a + nums[left] + nums[right] < 0) | ||
left++; | ||
} | ||
} | ||
return result; | ||
} | ||
}; | ||
|
||
int main() | ||
{ | ||
vector<int> nums = { -2,0,0,2,2 }; | ||
|
||
Solution S; | ||
S.threeSum(nums); | ||
|
||
return 0; | ||
} |
Oops, something went wrong.