Skip to content

Commit 3b016bf

Browse files
committed
5 problems solved.
1 parent 6e254fa commit 3b016bf

File tree

5 files changed

+130
-0
lines changed

5 files changed

+130
-0
lines changed

src/1021.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
string removeOuterParentheses(string S) {
2+
int len = S.size();
3+
if (len == 0 || len == 2)
4+
return "";
5+
6+
int i = 0, last = 0;
7+
int left = 0, right = 0;
8+
string res("");
9+
while (i < len)
10+
{
11+
if (S[i] == '(')
12+
++left;
13+
else
14+
{
15+
++right;
16+
if (left == right)
17+
{
18+
if (i - last + 1 > 2)
19+
res.insert(res.end(), S.begin()+last+1, S.begin()+i);
20+
last = i+1;
21+
}
22+
}
23+
++i;
24+
}
25+
26+
return res;
27+
}

src/142.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
ListNode *detectCycle(ListNode *head) {
2+
if (head == nullptr || head->next == head)
3+
return head;
4+
ListNode* fast = head;
5+
ListNode* slow = head;
6+
fast = fast->next;
7+
if (fast == nullptr)
8+
return nullptr;
9+
fast = fast->next;
10+
slow = slow->next;
11+
while (fast != nullptr && slow != nullptr)
12+
{
13+
if (fast != slow)
14+
{
15+
fast = fast->next;
16+
slow = slow->next;
17+
if (fast == nullptr)
18+
return nullptr;
19+
else
20+
fast = fast->next;
21+
}
22+
else
23+
break;
24+
}
25+
if (fast == nullptr)
26+
return nullptr;
27+
slow = head;
28+
while (slow != fast)
29+
{
30+
slow = slow->next;
31+
fast = fast->next;
32+
}
33+
return slow;
34+
}

src/17.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
vector<string> letterCombinations(string digits) {
2+
int len = digits.size();
3+
vector<string> res;
4+
if (len == 0)
5+
return res;
6+
string dict[10] = {"","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
7+
int num[10] = {0,0,3,3,3,3,3,4,3,4};
8+
9+
string s;
10+
search(digits, 0, len, dict, num, s, res);
11+
12+
return move(res);
13+
}
14+
15+
void search(const string& digits, int ct, int len, const string* dict, const int* num, string s, vector<string>& res)
16+
{
17+
if (ct < len)
18+
{
19+
int index = digits[ct]-'0';
20+
int n = num[index];
21+
for (int i = 0; i < n; ++i)
22+
search(digits, ct+1, len, dict, num, s+dict[index][i], res);
23+
}
24+
else
25+
res.push_back(s);
26+
}

src/215.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
int findKthLargest(vector<int>& nums, int k) {
2+
sort(nums.begin(), nums.end());
3+
int len = nums.size();
4+
5+
return nums[len-1-(k-1)];
6+
}

src/23.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
ListNode* mergeKLists(vector<ListNode*>& lists) {
2+
ListNode* head = nullptr;
3+
lists.erase(remove(lists.begin(), lists.end(), nullptr), lists.end());
4+
int len = lists.size();
5+
if (len == 0)
6+
return head;
7+
if (len == 1)
8+
return lists[0];
9+
10+
struct cmp
11+
{
12+
bool operator()(ListNode* a, ListNode* b)
13+
{
14+
return a->val > b->val;
15+
}
16+
};
17+
priority_queue<ListNode*, vector<ListNode*>, cmp> pq;
18+
for (int i = 0; i < len; ++i)
19+
pq.push(lists[i]);
20+
head = pq.top();
21+
pq.pop();
22+
if (head->next != nullptr)
23+
pq.push(head->next);
24+
ListNode* node = head;
25+
while (!pq.empty())
26+
{
27+
ListNode* tmp = pq.top();
28+
pq.pop();
29+
if (tmp->next != nullptr)
30+
pq.push(tmp->next);
31+
node->next = tmp;
32+
node = tmp;
33+
}
34+
node->next = nullptr;
35+
36+
return head;
37+
}

0 commit comments

Comments
 (0)