Skip to content

Commit 448d289

Browse files
committed
some easy problems to get started again
1 parent cd0a484 commit 448d289

6 files changed

+252
-0
lines changed

21. Merge Two Sorted Lists.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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+
ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
12+
13+
ListNode *result = new ListNode (-1);
14+
ListNode *cur = result;
15+
16+
while(l1 && l2) {
17+
18+
if (l1->val < l2->val) {
19+
cur->next = l1;
20+
l1 = l1->next;
21+
}
22+
else {
23+
cur->next = l2;
24+
l2 = l2->next;
25+
}
26+
27+
cur = cur->next;
28+
29+
}
30+
31+
while(l1) {
32+
cur->next = l1;
33+
cur = cur->next;
34+
l1 = l1->next;
35+
}
36+
37+
while(l2) {
38+
cur->next = l2;
39+
cur = cur->next;
40+
l2 = l2->next;
41+
}
42+
43+
return result->next;
44+
45+
}
46+
};

23. Merge k Sorted Lists.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
11+
struct mycomp {
12+
bool operator() (ListNode* &l1, ListNode* &l2) {
13+
return l1->val > l2->val;
14+
}
15+
};
16+
public:
17+
ListNode* mergeKLists(vector<ListNode*>& lists) {
18+
19+
ListNode *head = new ListNode(-1);
20+
21+
if (lists.empty())
22+
return head->next;
23+
24+
priority_queue<ListNode *, vector<ListNode *> , mycomp > mh;
25+
26+
for(auto i : lists) {
27+
if (i)
28+
mh.push(i);
29+
}
30+
31+
32+
ListNode *cur = head;
33+
34+
while (!mh.empty()) {
35+
ListNode *f = mh.top();
36+
cur->next = f;
37+
cur = cur->next;
38+
mh.pop();
39+
if (f->next) {
40+
f = f->next;
41+
mh.push(f);
42+
}
43+
44+
}
45+
46+
return head->next;
47+
48+
49+
}
50+
};

263. Ugly Number.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public:
3+
bool isUgly(int num) {
4+
5+
if(!num)
6+
return false;
7+
8+
while(num % 2 == 0) {
9+
num = num/2;
10+
}
11+
12+
while(num % 3 == 0) {
13+
num = num/3;
14+
}
15+
16+
while(num % 5 == 0) {
17+
num = num/5;
18+
}
19+
20+
return num == 1;
21+
22+
}
23+
};

400. Nth Digit.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class Solution {
2+
public:
3+
int findNthDigit(int n) {
4+
5+
if (n <= 9)
6+
return n;
7+
8+
int nine = 9;
9+
long i = 0;
10+
long copy = n;
11+
long sum = 0;
12+
13+
while (copy > 0) {
14+
++i;
15+
sum = nine*pow(10,i-1);
16+
sum = sum * i;
17+
copy = copy - sum;
18+
}
19+
20+
copy = copy + sum;
21+
22+
int q = copy / i;
23+
int r = copy % i;
24+
25+
long num = 1*pow(10,i-1) + q;
26+
int ans = 0;
27+
28+
// cout << num << endl;
29+
30+
if (r == 0) {
31+
r = i;
32+
--num;
33+
}
34+
// start from index. r = index and strip away all the remaining digits and then extract the required digit.
35+
for(int j = r;j<i; ++j) {
36+
num = num/10;
37+
}
38+
ans = num % 10;
39+
return ans;
40+
41+
}
42+
};
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
class Solution {
2+
public:
3+
vector<int> findAnagrams(string s, string p) {
4+
5+
vector<int> result;
6+
vector<int> pat(256,0);
7+
vector<int> str(256,0);
8+
9+
for(int i = 0;i<p.length();++i) {
10+
++pat[p[i]];
11+
++str[s[i]];
12+
}
13+
14+
if(pat == str)
15+
result.push_back(0);
16+
17+
for(int i = p.length();i<s.length();++i) {
18+
++str[s[i]];
19+
--str[s[i-p.length()]];
20+
21+
if (pat == str)
22+
result.push_back(i-p.length()+1);
23+
24+
}
25+
26+
return result;
27+
}
28+
};

450. Delete Node in a BST.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
8+
* };
9+
*/
10+
class Solution {
11+
public:
12+
TreeNode* deleteNode(TreeNode* root, int key) {
13+
14+
if(!root)
15+
return root;
16+
17+
if(key > root->val)
18+
root->right = deleteNode(root->right,key);
19+
20+
else if (key < root->val)
21+
root->left = deleteNode(root->left, key);
22+
23+
else {
24+
25+
if(root->left == nullptr && root->right == nullptr) {
26+
delete root;
27+
return nullptr;
28+
}
29+
30+
else if (root->left == nullptr && root->right) {
31+
32+
TreeNode *cur = root->right;
33+
delete root;
34+
return cur;
35+
36+
}
37+
38+
else if (root->right == nullptr && root->left) {
39+
TreeNode *cur = root->left;
40+
delete root;
41+
return cur;
42+
}
43+
44+
else {
45+
TreeNode *cur = root->right;
46+
while(cur->left) {
47+
cur = cur->left;
48+
}
49+
50+
51+
cout << cur->val << endl;
52+
53+
root->val = cur->val;
54+
root->right = deleteNode(root->right, cur->val);
55+
56+
}
57+
}
58+
59+
return root;
60+
61+
62+
}
63+
};

0 commit comments

Comments
 (0)