Skip to content

Commit f3a1249

Browse files
committed
8 problems solved.
1 parent 7f5ef72 commit f3a1249

File tree

8 files changed

+163
-0
lines changed

8 files changed

+163
-0
lines changed

src/147.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
ListNode* insertionSortList(ListNode* head) {
2+
if (head == nullptr || head->next == nullptr)
3+
return head;
4+
5+
ListNode* node = head;
6+
ListNode* p = head->next;
7+
node->next = nullptr;
8+
while (p != nullptr)
9+
{
10+
ListNode* p1 = node;
11+
if (p->val <= p1->val)
12+
{
13+
ListNode* tmp = p->next;
14+
p->next = p1;
15+
node = p;
16+
p = tmp;
17+
}
18+
else
19+
{
20+
ListNode* p2 = p1;
21+
while (p1->next != nullptr && p1->val < p->val)
22+
{
23+
p2 = p1;
24+
p1 = p1->next;
25+
}
26+
ListNode* tmp = p->next;
27+
if (p1->next == nullptr)
28+
{
29+
if (p1->val < p->val)
30+
{
31+
p1->next = p;
32+
p->next = nullptr;
33+
}
34+
else
35+
{
36+
p2->next = p;
37+
p->next = p1;
38+
}
39+
}
40+
else
41+
{
42+
p2->next = p;
43+
p->next = p1;
44+
}
45+
p = tmp;
46+
}
47+
}
48+
49+
return node;
50+
}

src/1614.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
int maxDepth(string s) {
2+
int len = s.size();
3+
int maxdepth = 0;
4+
int depth = 0;
5+
int i = 0;
6+
7+
while (i < len)
8+
{
9+
if (s[i] == '(')
10+
++depth;
11+
else if (s[i] == ')')
12+
{
13+
if (depth > maxdepth)
14+
maxdepth = depth;
15+
--depth;
16+
}
17+
++i;
18+
}
19+
20+
return maxdepth;
21+
}

src/1812.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
bool squareIsWhite(string coordinates) {
2+
return ((coordinates[0]-'a')+(coordinates[1]-'1'))%2 == 0 ? false : true;
3+
}

src/1816.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
string truncateSentence(string s, int k) {
2+
int i = 0;
3+
int index = 0;
4+
int pos = 0;
5+
while (i < k)
6+
{
7+
pos = s.find(' ', index);
8+
++i;
9+
index = pos+1;
10+
}
11+
12+
return s.substr(0, pos);
13+
}

src/1822.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
int arraySign(vector<int>& nums) {
2+
int len = nums.size();
3+
int ct = 0;
4+
for (int i = 0; i < len; ++i)
5+
{
6+
if (nums[i] < 0)
7+
++ct;
8+
else if (nums[i] == 0)
9+
return 0;
10+
}
11+
12+
return ct%2 == 0 ? 1 : -1;
13+
}

src/1832.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
bool checkIfPangram(string sentence) {
2+
int len = sentence.size();
3+
if (len < 26)
4+
return false;
5+
int arr[26] = {0};
6+
int ct = 0;
7+
for (int i = 0; i < len; ++i)
8+
{
9+
if (arr[sentence[i]-'a'] == 0)
10+
{
11+
++ct;
12+
arr[sentence[i]-'a'] = 1;
13+
}
14+
}
15+
16+
return ct == 26;
17+
}

src/1844.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
string replaceDigits(string s) {
2+
int len = s.size();
3+
for (int i = 0; i < len-1; i += 2)
4+
{
5+
s[i+1] = s[i] + (s[i+1]-'0');
6+
}
7+
8+
return s;
9+
}

src/1848.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
int getMinDistance(vector<int>& nums, int target, int start) {
2+
int len = nums.size();
3+
if (nums[start] == target)
4+
return 0;
5+
int i = start-1, j = start+1;
6+
int ct = 1;
7+
while (i >= 0 && j < len)
8+
{
9+
if (nums[i] == target || nums[j] == target)
10+
break;
11+
--i;
12+
++j;
13+
++ct;
14+
}
15+
if (i >= 0 && j < len)
16+
return ct;
17+
18+
while (i >= 0)
19+
{
20+
if (nums[i] == target)
21+
break;
22+
--i;
23+
++ct;
24+
}
25+
if (i >= 0)
26+
return ct;
27+
28+
while (j < len)
29+
{
30+
if (nums[j] == target)
31+
return ct;
32+
++j;
33+
++ct;
34+
}
35+
36+
return ct;
37+
}

0 commit comments

Comments
 (0)