Skip to content

Commit c297d3f

Browse files
adam.linadam.lin
adam.lin
authored and
adam.lin
committed
Update previous work
1 parent 0069f02 commit c297d3f

6 files changed

+113
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
.vscode/
2+
*.pdb
3+
Backup/

121_maxProfit.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
int maxProfit(vector<int>& prices) {
4+
int left = 0;
5+
int right = 1;
6+
int maxProfit = 0;
7+
int profit = 0;
8+
9+
while(right < prices.size()){
10+
if(prices[left] < prices[right]){
11+
profit = prices[right] - prices[left];
12+
maxProfit = max(profit, maxProfit);
13+
}
14+
else{
15+
left = right;
16+
}
17+
18+
right++;
19+
}
20+
21+
return maxProfit;
22+
}
23+
};

141_hasCycle.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* struct ListNode {
4+
* int val;
5+
* ListNode *next;
6+
* ListNode(int x) : val(x), next(NULL) {}
7+
* };
8+
*/
9+
class Solution {
10+
public:
11+
bool hasCycle(ListNode *head) {
12+
ListNode *slowPtr = head;
13+
ListNode *fastPtr = head;
14+
15+
while (fastPtr != nullptr && fastPtr->next != nullptr){
16+
slowPtr = slowPtr->next;
17+
fastPtr = fastPtr->next->next;
18+
19+
if (slowPtr == fastPtr){
20+
return true;
21+
}
22+
}
23+
24+
return false;
25+
}
26+
};

28_strStr.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
int strStr(string haystack, string needle) {
4+
5+
if(needle.size() > haystack.size())
6+
return -1;
7+
8+
for(int i = 0; i <= haystack.size() - needle.size(); i++){
9+
if(haystack.substr(i, needle.size()) == needle){
10+
return i;
11+
}
12+
}
13+
14+
return -1;
15+
}
16+
};

454_fourSumCount.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Solution {
2+
public:
3+
int fourSumCount(vector<int>& A, vector<int>& B, vector<int>& C, vector<int>& D) {
4+
unordered_map<int, int> ABSumCounter, CDSumCounter;
5+
6+
for(auto a : A){
7+
for(auto b : B){
8+
ABSumCounter[a+b] += 1;
9+
}
10+
}
11+
12+
for(auto c : C){
13+
for(auto d : D){
14+
CDSumCounter[c+d] += 1;
15+
}
16+
}
17+
18+
int ans = 0;
19+
for(auto abit = ABSumCounter.begin(); abit != ABSumCounter.end(); abit++){
20+
auto cdit = CDSumCounter.find(-abit->first);
21+
if(cdit != CDSumCounter.end()){
22+
//we are finding +`++combination, so use product
23+
ans += (abit->second * cdit->second);
24+
}
25+
}
26+
27+
return ans;
28+
}
29+
};

53_maxSubArray.cpp

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
int maxSubArray(vector<int>& nums) {
4+
int sum = 0;
5+
int maxSum = nums[0];
6+
7+
for (int i = 0; i < nums.size(); i++){
8+
if (sum < 0){
9+
sum = 0;
10+
}
11+
sum += nums[i];
12+
maxSum = max(sum, maxSum);
13+
}
14+
15+
return maxSum;
16+
}
17+
};

0 commit comments

Comments
 (0)