Skip to content

Commit 113771c

Browse files
Merge pull request #110 from AkashSingh19/master
Added Solution of N queen in cpp
2 parents db53a05 + 3f99b2e commit 113771c

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed

51.N Queen.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
class Solution {
2+
public:
3+
bool isSafe1(int row, int col, vector < string > board, int n) {
4+
// check upper element
5+
int duprow = row;
6+
int dupcol = col;
7+
8+
//check upper left diagonal
9+
while (row >= 0 && col >= 0) {
10+
if (board[row][col] == 'Q')
11+
return false;
12+
row--;
13+
col--;
14+
}
15+
16+
col = dupcol;
17+
row = duprow;
18+
//check left col on the same row
19+
while (col >= 0) {
20+
if (board[row][col] == 'Q')
21+
return false;
22+
col--;
23+
}
24+
25+
row = duprow;
26+
col = dupcol;
27+
//check lower left diagonal
28+
while (row < n && col >= 0) {
29+
if (board[row][col] == 'Q')
30+
return false;
31+
row++;
32+
col--;
33+
}
34+
return true;
35+
}
36+
37+
public:
38+
void solve(int col, vector < string > & board, vector < vector < string >> & ans, int n) {
39+
if (col == n) {
40+
ans.push_back(board);
41+
return;
42+
}
43+
for (int row = 0; row < n; row++) {
44+
if (isSafe1(row, col, board, n)) {
45+
board[row][col] = 'Q';
46+
solve(col + 1, board, ans, n);
47+
board[row][col] = '.';
48+
}
49+
}
50+
}
51+
52+
public:
53+
vector < vector < string >> solveNQueens(int n) {
54+
vector < vector < string >> ans;
55+
vector < string > board(n, string(n,'.'));
56+
solve(0, board, ans, n);
57+
return ans;
58+
}
59+
};

71.Simplify Path.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution {
2+
public:
3+
string simplifyPath(string path) {
4+
string res, tmp;
5+
vector<string> stk;
6+
stringstream ss(path);
7+
while(getline(ss,tmp,'/')) {
8+
if (tmp == "" or tmp == ".") continue;
9+
if (tmp == ".." and !stk.empty()) stk.pop_back();
10+
else if (tmp != "..") stk.push_back(tmp);
11+
}
12+
for(auto str : stk) res += "/"+str;
13+
return res.empty() ? "/" : res;
14+
}
15+
};
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
public boolean search(int[] nums, int target) {
2+
int start = 0, end = nums.length - 1, mid = -1;
3+
while(start <= end) {
4+
mid = (start + end) / 2;
5+
if (nums[mid] == target) {
6+
return true;
7+
}
8+
//If we know for sure right side is sorted or left side is unsorted
9+
if (nums[mid] < nums[end] || nums[mid] < nums[start]) {
10+
if (target > nums[mid] && target <= nums[end]) {
11+
start = mid + 1;
12+
} else {
13+
end = mid - 1;
14+
}
15+
//If we know for sure left side is sorted or right side is unsorted
16+
} else if (nums[mid] > nums[start] || nums[mid] > nums[end]) {
17+
if (target < nums[mid] && target >= nums[start]) {
18+
end = mid - 1;
19+
} else {
20+
start = mid + 1;
21+
}
22+
//If we get here, that means nums[start] == nums[mid] == nums[end], then shifting out
23+
//any of the two sides won't change the result but can help remove duplicate from
24+
//consideration, here we just use end-- but left++ works too
25+
} else {
26+
end--;
27+
}
28+
}
29+
30+
return false;
31+
}

0 commit comments

Comments
 (0)