Skip to content

Commit cc8c55d

Browse files
committed
practised lists
1 parent d744369 commit cc8c55d

File tree

3 files changed

+188
-0
lines changed

3 files changed

+188
-0
lines changed

240. Search a 2D Matrix II.cpp

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
class Solution {
2+
public:
3+
bool searchMatrix(vector<vector<int>>& matrix, int target) {
4+
5+
if(matrix.empty() || matrix[0].empty())
6+
return false;
7+
8+
9+
int row = 0;
10+
int col = matrix[0].size()-1;
11+
12+
13+
while(col >=0 && row < matrix.size()) {
14+
15+
if(matrix[row][col] == target)
16+
return true;
17+
18+
else if (matrix[row][col] > target)
19+
col--;
20+
21+
else
22+
row++;
23+
24+
}
25+
return false;
26+
27+
}
28+
};
29+
30+
-------
31+
32+
33+
// Divide & conquer
34+
35+
class Solution {
36+
37+
bool helper(vector<vector<int>>& matrix, int target, int rlow, int rhigh, int clow, int chigh) {
38+
if ((rlow > rhigh) || (clow > chigh))
39+
return false;
40+
41+
int rmid = rlow + (rhigh-rlow)/2;
42+
43+
int cmid = clow + (chigh-clow)/2;
44+
45+
if (matrix[rmid][cmid] == target)
46+
return true;
47+
48+
else if (matrix[rmid][cmid] > target)
49+
return helper(matrix, target, rlow, rhigh, clow, cmid - 1)
50+
|| helper(matrix, target, rlow, rmid-1, cmid, chigh);
51+
52+
else
53+
return helper(matrix, target, rmid+1, rhigh, clow, chigh)
54+
|| helper(matrix, target, rlow, rmid, cmid+1, chigh);
55+
}
56+
57+
58+
public:
59+
bool searchMatrix(vector<vector<int>>& matrix, int target) {
60+
61+
62+
int rlow = 0;
63+
int clow = 0;
64+
int chigh = matrix[0].size()-1;
65+
int rhigh = matrix.size()-1;
66+
67+
return helper(matrix, target, rlow, rhigh, clow, chigh);
68+
69+
70+
}
71+
};

61. Rotate List.cpp

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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* rotateRight(ListNode* head, int k) {
12+
13+
if(!k)
14+
return head;
15+
16+
int n = 0;
17+
18+
ListNode *onebeforek = head;
19+
20+
ListNode *cur = head;
21+
22+
int kcopy = k;
23+
24+
while(cur && cur->next && k--) {
25+
cur = cur->next;
26+
++n;
27+
}
28+
29+
while(cur && cur->next) {
30+
cur = cur->next;
31+
onebeforek = onebeforek->next;
32+
++n;
33+
}
34+
35+
n++;
36+
37+
if (kcopy > n) {
38+
k = kcopy % n;
39+
k = n - k;
40+
k--;
41+
onebeforek = head;
42+
while(k--) {
43+
onebeforek = onebeforek->next;
44+
}
45+
}
46+
47+
if(kcopy == n)
48+
return head;
49+
50+
if(!cur)
51+
return head;
52+
53+
cur->next = head;
54+
head = onebeforek->next;
55+
onebeforek->next = nullptr;
56+
57+
return head;
58+
}
59+
};

92. Reverse Linked List II.cpp

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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* reverseBetween(ListNode* head, int m, int n) {
12+
13+
if(!head)
14+
return nullptr;
15+
16+
if(m == n)
17+
return head;
18+
19+
ListNode *cur = head;
20+
ListNode *prev = nullptr;
21+
ListNode *next = nullptr;
22+
ListNode *onebeforem = nullptr;
23+
ListNode *mth = nullptr;
24+
int mcopy = m;
25+
26+
m--;
27+
n--;
28+
29+
while(cur->next && m-- && n--) {
30+
prev = cur;
31+
cur = cur->next;
32+
}
33+
34+
onebeforem = prev;
35+
mth = cur;
36+
37+
while(cur && n--) {
38+
next = cur->next;
39+
cur->next = prev;
40+
prev = cur;
41+
cur = next;
42+
}
43+
44+
if(onebeforem)
45+
onebeforem->next = cur;
46+
47+
if(cur)
48+
mth->next = cur->next;
49+
50+
cur->next = prev;
51+
52+
if (mcopy == 1)
53+
head = cur;
54+
55+
return head;
56+
57+
}
58+
};

0 commit comments

Comments
 (0)