Skip to content

Commit 1912b11

Browse files
committed
More solutions for leetcode
1 parent 0b97b8d commit 1912b11

File tree

4 files changed

+293
-0
lines changed

4 files changed

+293
-0
lines changed

N-Queens.cpp

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
class Solution {
2+
public:
3+
vector<vector<string> > solveNQueens(int n) {
4+
5+
vector<vector<string>> solution;
6+
vector<vector<bool>> board(n, vector<bool>(n, false));
7+
8+
solve(solution, board, 0);
9+
10+
return solution;
11+
}
12+
private:
13+
void solve(vector<vector<string>> &solution, vector<vector<bool>> &board, int col){
14+
if(col >= board.size()){
15+
solution.push_back(board_str(board));
16+
return;
17+
}
18+
19+
for(int row = 0; row < board.size(); row++){
20+
if(is_safe(board, row, col)){
21+
place_queen(board, row, col);
22+
solve(solution, board, col+1);
23+
remove_queen(board, row, col);
24+
}
25+
}
26+
27+
}
28+
29+
void place_queen(vector<vector<bool>> &board, int row, int col){
30+
board[row][col] = true;
31+
}
32+
33+
void remove_queen(vector<vector<bool>> &board, int row, int col){
34+
board[row][col] = false;
35+
}
36+
37+
bool is_safe(vector<vector<bool>> &board, int row, int col){
38+
for(int j = 0; j < board.size(); j++){
39+
if(board[row][j]) return false;
40+
if(board[j][col]) return false;
41+
}
42+
43+
int ind = 1;
44+
while(row-ind>=0 && col-ind>=0){
45+
if(board[row-ind][col-ind]) return false;
46+
ind++;
47+
}
48+
49+
ind = 1;
50+
while(row+ind < board.size() && col-ind>=0){
51+
if(board[row+ind][col-ind]) return false;
52+
ind++;
53+
}
54+
return true;
55+
}
56+
57+
vector<string> board_str(vector<vector<bool>> &board){
58+
string layer;
59+
vector<string> result;
60+
61+
for(int i = 0; i < board.size(); i++){
62+
layer = "";
63+
for(int j = 0; j < board[0].size(); j++){
64+
if(board[i][j]) layer += 'Q';
65+
else layer += '.';
66+
}
67+
result.push_back(layer);
68+
}
69+
return result;
70+
}
71+
};

N-Queens_2.cpp

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
class Solution {
2+
public:
3+
int totalNQueens(int n) {
4+
5+
int num_solutions = 0;
6+
7+
vector<vector<bool>> board(n, vector<bool>(n, false));
8+
solve(num_solutions, board, 0);
9+
10+
return num_solutions;
11+
}
12+
private:
13+
void solve(int &num_solutions, vector<vector<bool>> &board, int col){
14+
if(col >= board.size()){
15+
num_solutions++;
16+
return;
17+
}
18+
19+
for(int row = 0; row < board.size(); row++){
20+
if(is_safe(board, row, col)){
21+
place_queen(board, row, col);
22+
solve(num_solutions, board, col+1);
23+
remove_queen(board, row, col);
24+
}
25+
}
26+
27+
}
28+
29+
void place_queen(vector<vector<bool>> &board, int row, int col){
30+
board[row][col] = true;
31+
}
32+
33+
void remove_queen(vector<vector<bool>> &board, int row, int col){
34+
board[row][col] = false;
35+
}
36+
37+
bool is_safe(vector<vector<bool>> &board, int row, int col){
38+
for(int j = 0; j < board.size(); j++){
39+
if(board[row][j]) return false;
40+
if(board[j][col]) return false;
41+
}
42+
43+
int ind = 1;
44+
while(row-ind>=0 && col-ind>=0){
45+
if(board[row-ind][col-ind]) return false;
46+
ind++;
47+
}
48+
49+
ind = 1;
50+
while(row+ind < board.size() && col-ind>=0){
51+
if(board[row+ind][col-ind]) return false;
52+
ind++;
53+
}
54+
return true;
55+
}
56+
};

Search_a_2D_Matrix.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+
if(matrix.empty()) return false;
5+
6+
int m = matrix.size();
7+
int n = matrix[0].size();
8+
9+
int i = 0;
10+
int j = n-1;
11+
while(i < m && j >= 0){
12+
if(matrix[i][j] == target) return true;
13+
else if(matrix[i][j] < target) i++;
14+
else j--;
15+
}
16+
17+
return false;
18+
}
19+
};
20+
21+
pair<int, int> search_2D_matrix(vector<vector<int>> &A, int sr, int sc, int er, int ec, int target){
22+
if(sr > er || sc > ec) return {-1, -1};
23+
24+
int mr = sr + (er-sr)/2;
25+
int mc = sc + (ec-sc)/2;
26+
27+
if(A[mr][mc] == target) return {mr, mc};
28+
else if(A[mr][mc] > target) return search_2D_matrix(A, sr, sc, mr, mc, target);
29+
else{
30+
pair<int, int> t = search_2D_matrix(A, mr+1, sc, er, mc, target);
31+
if(t.first == -1 && t.second == -1) return search_2D_matrix(A, sr, mc+1, er, ec, target);
32+
else return t;
33+
}
34+
}
35+
36+
class Solution {
37+
public:
38+
bool searchMatrix(vector<vector<int>> &matrix, int target) {
39+
// IMPORTANT: Please reset any member data you declared, as
40+
// the same Solution instance will be reused for each test case.
41+
42+
int sInd, mInd, eInd;
43+
pair<int, int> rc;
44+
int nCol, nRow;
45+
46+
nRow = matrix.size();
47+
nCol = matrix[0].size();
48+
49+
sInd = 0; eInd = nRow*nCol - 1;
50+
51+
while (sInd <= eInd) {
52+
mInd = sInd + (eInd - sInd)/2;
53+
rc = index_rc(mInd, nCol);
54+
55+
if (matrix[rc.first][rc.second] == target) {
56+
return true;
57+
}else if (matrix[rc.first][rc.second] < target) {
58+
sInd = mInd + 1;
59+
}else{
60+
eInd = mInd - 1;
61+
}
62+
}
63+
64+
return false;
65+
}
66+
67+
private:
68+
pair<int, int> index_rc(int index, int sz){
69+
return make_pair(index / sz, index % sz);
70+
}
71+
};

Swap_Nodes_in_Pairs.cpp

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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 *swapPairs(ListNode *head) {
12+
if(head == nullptr) return nullptr;
13+
if(head->next == nullptr) return head;
14+
15+
ListNode *cur = head;
16+
ListNode *next = nullptr;
17+
ListNode dummy(0);
18+
ListNode *d = &dummy;
19+
d->next = head;
20+
21+
while(cur && cur->next){
22+
next = cur->next;
23+
cur->next = next->next;
24+
next->next = cur;
25+
d->next = next;
26+
d = cur;
27+
cur = cur->next;
28+
}
29+
30+
return dummy.next;
31+
}
32+
};
33+
34+
35+
/**
36+
* Solution 2: split the list into two, and merge them back into one.
37+
*
38+
*/
39+
class Solution {
40+
public:
41+
ListNode *swapPairs(ListNode *head) {
42+
if(head == nullptr) return nullptr;
43+
if(head->next == nullptr) return head;
44+
45+
ListNode dummy1(0);
46+
ListNode *d1 = &dummy1;
47+
ListNode dummy2(0);
48+
ListNode *d2 = &dummy2;
49+
50+
bool flag = true;
51+
ListNode *p = head;
52+
ListNode *t = nullptr;
53+
while(p){
54+
t = p->next;
55+
p->next = nullptr;
56+
if(flag){
57+
d1->next = p;
58+
d1 = d1->next;
59+
flag = false;
60+
}else{
61+
d2->next = p;
62+
d2 = d2->next;
63+
flag = true;
64+
}
65+
p = t;
66+
}
67+
68+
return merge_list(dummy1.next, dummy2.next);
69+
}
70+
private:
71+
ListNode *merge_list(ListNode *l1, ListNode *l2){
72+
ListNode dummy(0);
73+
ListNode *d = &dummy;
74+
75+
bool flag = true;
76+
while(l1 && l2){
77+
if(flag){
78+
d->next = l2;
79+
l2 = l2->next;
80+
d = d->next;
81+
flag = false;
82+
}else{
83+
d->next = l1;
84+
l1 = l1->next;
85+
d = d->next;
86+
flag = true;
87+
}
88+
}
89+
90+
if(l1) d->next = l1;
91+
if(l2) d->next = l2;
92+
93+
return dummy.next;
94+
}
95+
};

0 commit comments

Comments
 (0)