Skip to content

Commit 944b065

Browse files
adam.linadam.lin
adam.lin
authored and
adam.lin
committed
Update previous work
1 parent 16e7580 commit 944b065

6 files changed

+152
-1
lines changed

.vscode/settings.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,6 @@
5454
"xstring": "cpp",
5555
"xtr1common": "cpp",
5656
"xutility": "cpp"
57-
}
57+
},
58+
"C_Cpp.errorSquiggles": "enabled"
5859
}

152_maxProduct.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#include <vector>
2+
3+
class Solution {
4+
public:
5+
int maxProduct(std::vector<int>& nums) {
6+
int n = nums.size();
7+
int product = 0;
8+
int maxProduct = 0;
9+
10+
for (int i = 0; i < n; ++i) {
11+
for (int j = i; j < n; ++j) {
12+
product = nums[i] * nums[j];
13+
maxProduct = std::max(product, maxProduct);
14+
}
15+
}
16+
17+
return maxProduct;
18+
}
19+
};

1_twoSum.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <vector>
2+
#include <unordered_map>
3+
4+
using namespace std;
5+
6+
class Solution {
7+
public:
8+
vector<int> twoSum(vector<int>& nums, int target) {
9+
//for store seen value
10+
unordered_map<int, int> nonrepeat;
11+
12+
for (int i = 0; i < nums.size(); ++i) {
13+
int answer = target - nums[i];
14+
15+
//unordered_map did not find anything, will return end(), means last position+1.
16+
if (nonrepeat.find(answer) == nonrepeat.end()) {
17+
nonrepeat[nums[i]] = i;
18+
}
19+
else {
20+
return vector<int> {nonrepeat[answer], i};
21+
}
22+
23+
}
24+
return{};
25+
}
26+
};

20_validParentheses.cpp

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#include <stack>
2+
#include <string>
3+
4+
class Solution {
5+
public:
6+
bool isValid(std::string s) {
7+
std::stack<char> container;
8+
for (int i = 0; i < s.length(); ++i)
9+
{
10+
if (s[i] == '(' || s[i] == '{' || s[i] == '[')
11+
{
12+
container.push(s[i]);
13+
}
14+
else if (s[i] == ')')
15+
{
16+
if (i == 0 || container.empty() || container.top() != '(')
17+
{
18+
return false;
19+
}
20+
container.pop();
21+
}
22+
else if (s[i] == '}')
23+
{
24+
if (i == 0 || container.empty() || container.top() != '{')
25+
{
26+
return false;
27+
}
28+
container.pop();
29+
}
30+
else if (s[i] == ']')
31+
{
32+
if (i == 0 || container.empty() || container.top() != '[')
33+
{
34+
return false;
35+
}
36+
container.pop();
37+
}
38+
39+
}
40+
return container.empty();
41+
}
42+
};

21_mergeTwoLists.cpp

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using namespace std;
2+
#include <iostream>
3+
4+
// Definition for singly-linked list.
5+
struct ListNode {
6+
int val;
7+
ListNode *next;
8+
ListNode() : val(0), next(nullptr) {}
9+
ListNode(int x) : val(x), next(nullptr) {}
10+
ListNode(int x, ListNode *next) : val(x), next(next) {}
11+
};
12+
13+
class Solution {
14+
public:
15+
ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
16+
ListNode* head = new ListNode(-1); // create a new ListNode and assign to first
17+
ListNode* dummy = head;
18+
19+
while (list1 != nullptr && list2 != nullptr) {
20+
if (list1->val <= list2->val) {
21+
dummy->next = list1;
22+
list1 = list1->next;
23+
}
24+
else {
25+
dummy->next = list2;
26+
list2 = list2->next;
27+
}
28+
dummy = dummy->next;
29+
}
30+
31+
if (list1 != nullptr) {
32+
dummy->next = list1;
33+
}
34+
else if (list2 != nullptr) {
35+
dummy->next = list2;
36+
}
37+
38+
return head->next;
39+
}
40+
};

575_distributeCandies.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <unordered_set>
4+
using namespace std;
5+
6+
class Solution {
7+
public:
8+
int distributeCandies(vector<int>& candyType) {
9+
unordered_set<int> myunordered_set(candyType.begin(), candyType.end());
10+
int rawN = candyType.size();
11+
int unorderedN = myunordered_set.size();
12+
return min(rawN / 2, unorderedN);
13+
14+
/*
15+
if(rawN / 2 > unorderedN){
16+
return unorderedN;
17+
}
18+
else{
19+
return rawN / 2;
20+
}
21+
*/
22+
}
23+
};

0 commit comments

Comments
 (0)