Skip to content

Commit 3c4bcc2

Browse files
committed
update leetcode
1 parent 1ac177a commit 3c4bcc2

10 files changed

+421
-0
lines changed

leetcode/1BitAnd2BitCharacters.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
bool isOneBitCharacter(vector<int>& bits) {
4+
bool res = true;
5+
int ptr = 0;
6+
while (ptr < bits.size()) {
7+
if (bits[ptr] == 0) {
8+
res = true;
9+
++ptr;
10+
} else {
11+
res = false;
12+
ptr += 2;
13+
}
14+
}
15+
return res;
16+
}
17+
};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
int maxProfit(vector<int>& prices, int fee) {
4+
int sz = prices.size();
5+
vector<int> dp_has(sz + 1, 0);
6+
vector<int> dp_no(sz + 1, 0);
7+
dp_has[0] = -100000;
8+
for (int i = 1; i <= prices.size(); ++i) {
9+
dp_has[i] = max(dp_has[i - 1], dp_no[i - 1] - prices[i - 1] - fee);
10+
dp_no[i] = max(dp_no[i - 1], dp_has[i - 1] + prices[i - 1]);
11+
}
12+
13+
return max(dp_has[sz], dp_no[sz]);
14+
}
15+
};

leetcode/BinarySearch.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Solution {
2+
public:
3+
int search(vector<int>& nums, int target) {
4+
int l = 0;
5+
int r = (int)nums.size() - 1;
6+
while (l < r) {
7+
int mid = (l + r) / 2;
8+
if (nums[mid] < target) {
9+
l = mid + 1;
10+
} else if (nums[mid] > target) {
11+
r = mid - 1;
12+
} else {
13+
return mid;
14+
}
15+
}
16+
if (l > r || nums[l] != target) return -1;
17+
return l;
18+
}
19+
};

leetcode/DesignHashMap.cpp

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
struct Node {
5+
int key;
6+
int val;
7+
Node* next;
8+
Node(int k, int v): key(k), val(v), next(NULL) {}
9+
};
10+
11+
class MyHashMap {
12+
private:
13+
vector<Node*> table;
14+
int get_hash(int key) {
15+
int ret = key % table.size();
16+
if (ret < 0) {
17+
return ret + table.size();
18+
} else {
19+
return ret;
20+
}
21+
}
22+
public:
23+
/** Initialize your data structure here. */
24+
MyHashMap() {
25+
table.resize(9997);
26+
}
27+
28+
/** value will always be non-negative. */
29+
void put(int key, int value) {
30+
int h = get_hash(key);
31+
Node* ptr = table[h];
32+
if (ptr == NULL) {
33+
table[h] = new Node(key, value);
34+
return;
35+
}
36+
Node* last = NULL;
37+
while (ptr != NULL) {
38+
if (ptr->key == key) {
39+
ptr->val = value;
40+
return;
41+
}
42+
last = ptr;
43+
ptr = ptr->next;
44+
}
45+
last->next = new Node(key, value);
46+
}
47+
48+
/** Returns the value to which the specified key is mapped, or -1 if this map contains no mapping for the key */
49+
int get(int key) {
50+
int h = get_hash(key);
51+
Node* ptr = table[h];
52+
while (ptr != NULL) {
53+
if (ptr->key == key) return ptr->val;
54+
ptr = ptr->next;
55+
}
56+
return -1;
57+
}
58+
59+
/** Removes the mapping of the specified value key if this map contains a mapping for the key */
60+
void remove(int key) {
61+
if (get(key) == -1) return;
62+
63+
int h = get_hash(key);
64+
Node* ptr = table[h];
65+
if (ptr->key == key) {
66+
table[h] = ptr->next;
67+
return;
68+
}
69+
Node* last = NULL;
70+
while (ptr != NULL) {
71+
if (ptr->key == key) {
72+
last->next = ptr->next;
73+
return;
74+
}
75+
last = ptr;
76+
ptr = ptr->next;
77+
}
78+
}
79+
};
80+
81+
/**
82+
* Your MyHashMap object will be instantiated and called as such:
83+
* MyHashMap* obj = new MyHashMap();
84+
* obj->put(key,value);
85+
* int param_2 = obj->get(key);
86+
* obj->remove(key);
87+
*/
88+
89+
int main() {
90+
MyHashMap solution;
91+
return 0;
92+
}

leetcode/DesignHashset.cpp

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
struct Node {
5+
int val;
6+
Node* next;
7+
Node(int v): val(v), next(NULL) {}
8+
};
9+
10+
class MyHashSet {
11+
private:
12+
vector<Node*> table;
13+
14+
int get_hash(int key) {
15+
int ret = key % table.size();
16+
if (ret < 0) {
17+
return ret + table.size();
18+
} else {
19+
return ret;
20+
}
21+
}
22+
public:
23+
/** Initialize your data structure here. */
24+
MyHashSet() {
25+
table.resize(9997);
26+
}
27+
28+
void add(int key) {
29+
if (contains(key)) {
30+
return;
31+
}
32+
33+
int h = get_hash(key);
34+
Node* ptr = table[h];
35+
if (ptr == NULL) {
36+
table[h] = new Node(key);
37+
return;
38+
}
39+
while (ptr->next != NULL) ptr = ptr->next;
40+
ptr->next = new Node(key);
41+
}
42+
43+
void remove(int key) {
44+
if (!contains(key)) {
45+
return;
46+
}
47+
48+
int h = get_hash(key);
49+
Node* prev = table[h];
50+
if (prev->val == key) {
51+
table[h] = prev->next;
52+
return;
53+
}
54+
Node* ptr = prev->next;
55+
while (ptr->val != key) {
56+
ptr = ptr->next;
57+
prev = prev->next;
58+
}
59+
prev->next = ptr->next;
60+
}
61+
62+
/** Returns true if this set contains the specified element */
63+
bool contains(int key) {
64+
int h = get_hash(key);
65+
Node* ptr = table[h];
66+
while (ptr != NULL) {
67+
if (ptr->val == key) return true;
68+
ptr = ptr->next;
69+
}
70+
return false;
71+
}
72+
};
73+
74+
int main() {
75+
Solution solution;
76+
return 0;
77+
}
78+
79+
/**
80+
* Your MyHashSet object will be instantiated and called as such:
81+
* MyHashSet* obj = new MyHashSet();
82+
* obj->add(key);
83+
* obj->remove(key);
84+
* bool param_3 = obj->contains(key);
85+
*/

leetcode/DesignLinkedList.cpp

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
struct Node {
5+
int val;
6+
Node* next;
7+
Node(int v): val(v), next(NULL) {}
8+
};
9+
10+
class MyLinkedList {
11+
private:
12+
Node* head;
13+
Node* tail;
14+
public:
15+
/** Initialize your data structure here. */
16+
MyLinkedList() {
17+
head = tail = NULL;
18+
}
19+
20+
/** Get the value of the index-th node in the linked list. If the index is invalid, return -1. */
21+
int get(int index) {
22+
if (index < 0) return -1;
23+
24+
Node* ptr = head;
25+
for (int i = 0; i < index && ptr != NULL; ++i) {
26+
ptr = ptr->next;
27+
}
28+
return ptr == NULL ? -1 : ptr->val;
29+
}
30+
31+
/** Add a node of value val before the first element of the linked list. After the insertion, the new node will be the first node of the linked list. */
32+
void addAtHead(int val) {
33+
if (head == NULL) {
34+
head = new Node(val);
35+
tail = head;
36+
} else {
37+
Node* nhead = new Node(val);
38+
nhead->next = head;
39+
head = nhead;
40+
}
41+
}
42+
43+
/** Append a node of value val to the last element of the linked list. */
44+
void addAtTail(int val) {
45+
if (tail == NULL) {
46+
tail = new Node(val);
47+
head = tail;
48+
} else {
49+
tail->next = new Node(val);
50+
tail = tail->next;
51+
}
52+
}
53+
54+
/** Add a node of value val before the index-th node in the linked list. If index equals to the length of linked list, the node will be appended to the end of linked list. If index is greater than the length, the node will not be inserted. */
55+
void addAtIndex(int index, int val) {
56+
if (index == 0) {
57+
addAtHead(val);
58+
return;
59+
}
60+
Node* ptr = head;
61+
for (int i = 0; i < index - 1; ++i) {
62+
if (ptr == NULL || ptr->next == NULL) {
63+
return;
64+
}
65+
ptr = ptr->next;
66+
}
67+
Node* node = new Node(val);
68+
node->next = ptr->next;
69+
ptr->next = node;
70+
if (node->next == NULL) {
71+
tail = node;
72+
}
73+
}
74+
75+
/** Delete the index-th node in the linked list, if the index is valid. */
76+
void deleteAtIndex(int index) {
77+
if (index < 0) return;
78+
if (index == 0 && head != NULL) {
79+
head = head->next;
80+
if (head == NULL) {
81+
tail = NULL;
82+
}
83+
return;
84+
}
85+
86+
Node* ptr = head;
87+
Node* last = NULL;
88+
for (int i = 0; i < index; ++i) {
89+
if (ptr == NULL || ptr->next == NULL) {
90+
return;
91+
}
92+
last = ptr;
93+
ptr = ptr->next;
94+
}
95+
last->next = ptr->next;
96+
if (last->next == NULL) {
97+
tail = last;
98+
}
99+
}
100+
};
101+
102+
/**
103+
* Your MyLinkedList object will be instantiated and called as such:
104+
* MyLinkedList* obj = new MyLinkedList();
105+
* int param_1 = obj->get(index);
106+
* obj->addAtHead(val);
107+
* obj->addAtTail(val);
108+
* obj->addAtIndex(index,val);
109+
* obj->deleteAtIndex(index);
110+
*/
111+
112+
int main() {
113+
int index = 0;
114+
int val = 23;
115+
MyLinkedList* obj = new MyLinkedList();
116+
int param_1 = obj->get(index);
117+
obj->addAtHead(val);
118+
obj->addAtTail(val);
119+
obj->addAtIndex(index,val);
120+
obj->deleteAtIndex(index);
121+
return 0;
122+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class KthLargest {
2+
public:
3+
KthLargest(int kvalue, vector<int>& nums) : k(kvalue) {
4+
for(int i = 0; i < nums.size(); i++) add(nums[i]);
5+
}
6+
7+
int add(int val) {
8+
pq.push(val);
9+
if(pq.size() > k) pq.pop();
10+
return pq.top();
11+
}
12+
private:
13+
priority_queue<int, vector<int>, greater <int>> pq;
14+
int k;
15+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
int minimumDeleteSum(string s1, string s2) {
4+
int sz1 = s1.size(), sz2 = s2.size();
5+
vector<vector<int> > dp(sz1 + 1, vector<int>(sz2 + 1, 0));
6+
7+
for (int i = 1; i <= sz1; ++i) {
8+
for (int j = 1; j <= sz2; ++j) {
9+
if (s1[i - 1] == s2[j - 1]) {
10+
dp[i][j] = dp[i - 1][j - 1] + s1[i - 1];
11+
} else {
12+
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
13+
}
14+
}
15+
}
16+
17+
int res = 0;
18+
for (int i = 0; i < sz1; ++i) res += s1[i];
19+
for (int i = 0; i < sz2; ++i) res += s2[i];
20+
res -= 2 * dp[sz1][sz2];
21+
return res;
22+
}
23+
};

0 commit comments

Comments
 (0)