51. N-Queens
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other.
Given an integer n, return all distinct solutions to the n-queens puzzle.
Each solution contains a distinct board configuration of the n-queens' placement, where 'Q'
and '.'
both indicate a queen and an empty space respectively.
For example,
There exist two distinct solutions to the 4-queens puzzle:
[ [".Q..", // Solution 1 "...Q", "Q...", "..Q."], ["..Q.", // Solution 2 "Q...", "...Q", ".Q.."] ]
//dfs下,记得判断所有不合法的情况
class Solution {
public:
void dfs(int cur, int n, vector<string> &mp, int vis[], vector<vector<string>> &ans)
{
if(cur >= n)
{
ans.push_back(mp);
return ;
}
for(int i = 0; i < n; i++)
{
if(vis[i]) continue;
int j = 1;
while(cur - j >= 0 && i - j >= 0 && mp[cur-j][i-j] == '.') j++;
if(cur - j >= 0 && i - j >= 0 && mp[cur-j][i-j] == 'Q') continue;
j = 1;
while(cur-j>=0&&i+j<n&&mp[cur-j][i+j]=='.') j++;
if(cur-j>=0&&i+j<n&&mp[cur-j][i+j]=='Q') continue;
vis[i] = 1;
mp[cur][i] = 'Q';
dfs(cur+1, n, mp, vis, ans);
vis[i] = 0;
mp[cur][i] = '.';
}
}
vector<vector<string>> solveNQueens(int n) {
int vis[n];
vector<string> mp;
vector<vector<string>> ans;
string s;
for(int i = 0; i < n; i++) s += '.';
for(int i = 0; i < n; i++) mp.push_back(s);
memset(vis, 0, sizeof(vis));
dfs(0, n, mp, vis, ans);
return ans;
}
};
//左上为x-y 右上角 x+y
class Solution {
public:
void dfs(int cur, int n, vector<string> &mp, int vis[][20], vector<vector<string>> &ans)
{
if(cur >= n)
{
ans.push_back(mp);
return ;
}
for(int i = 0; i < n; i++)
{
if(vis[0][i] || vis[1][n+cur-i] || vis[2][cur+i]) continue;
vis[0][i] = vis[1][n+cur-i] = vis[2][cur+i] = 1;
mp[cur][i] = 'Q';
dfs(cur+1, n, mp, vis, ans);
vis[0][i] = vis[1][n+cur-i] = vis[2][cur+i] = 0;
mp[cur][i] = '.';
}
}
vector<vector<string>> solveNQueens(int n) {
int vis[3][20];
vector<string> mp;
vector<vector<string>> ans;
string s;
for(int i = 0; i < n; i++) s += '.';
for(int i = 0; i < n; i++) mp.push_back(s);
memset(vis, 0, sizeof(vis));
dfs(0, n, mp, vis, ans);
return ans;
}
};
52
.
N-Queens II
Follow up for N-Queens problem.
Now, instead outputting board configurations, return the total number of distinct solutions.
//同上一题
class Solution {
public:
void dfs(int cur, int n, vector<string> &mp, int vis[])
{
if(cur >= n)
{
ans++;
return ;
}
for(int i = 0; i < n; i++)
{
if(vis[i]) continue;
int j = 1;
while(cur - j >= 0 && i - j >= 0 && mp[cur-j][i-j] == '.') j++;
if(cur - j >= 0 && i - j >= 0 && mp[cur-j][i-j] == 'Q') continue;
j = 1;
while(cur-j>=0&&i+j<n&&mp[cur-j][i+j]=='.') j++;
if(cur-j>=0&&i+j<n&&mp[cur-j][i+j]=='Q') continue;
vis[i] = 1;
mp[cur][i] = 'Q';
dfs(cur+1, n, mp, vis);
vis[i] = 0;
mp[cur][i] = '.';
}
}
int ans;
int totalNQueens(int n) {
int vis[n];
ans = 0;
vector<string> mp;
string s;
for(int i = 0; i < n; i++) s += '.';
for(int i = 0; i < n; i++) mp.push_back(s);
memset(vis, 0, sizeof(vis));
dfs(0, n, mp, vis);
return ans;
}
};
53
.
Maximum Subarray
Find the contiguous subarray within an array (containing at least one number) which has the largest sum.
For example, given the array [-2,1,-3,4,-1,2,1,-5,4]
,
the contiguous subarray [4,-1,2,1]
has the largest sum = 6
.
If you have figured out the O(n) solution, try coding another solution using the divide and conquer approach, which is more subtle.
//简单dp
class Solution {
public:
int maxSubArray(vector<int>& nums) {
int ans = nums[0];
int dp = 0;
for(int i = 0; i < nums.size(); i++)
{
if(dp + nums[i] < 0) dp = max(nums[i], 0);
else dp += nums[i];
if(dp) ans = max(ans, dp);
else ans = max(ans, nums[i]);
}
return ans;
}
};
54
.
Spiral Matrix
Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral order.
For example,
Given the following matrix:
[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ]
You should return [1,2,3,6,9,8,7,4,5]
.
//模拟下
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
if(matrix.size() == 0) return {};
int l, r, ll, rr;
r = matrix.size()-1;
rr = matrix[0].size()-1;
l = ll = 0;
vector<int> ans;
while(l <= r && ll <= rr)
{
for(int j = ll; j <= rr; j++) ans.push_back(matrix[l][j]);
if(l < r) for(int i = l+1; i <= r; i++) ans.push_back(matrix[i][rr]);
if(l < r) for(int j = rr-1; j >= ll; j--) ans.push_back(matrix[r][j]);
if(ll < rr)for(int i = r-1; i > l; i--) ans.push_back(matrix[i][ll]);
ll++; rr--;
l++; r--;
}
return ans;
}
};
55
.
Jump Game
Given an array of non-negative integers, you are initially positioned at the first index of the array.
Each element in the array represents your maximum jump length at that position.
Determine if you are able to reach the last index.
For example:
A = [2,3,1,1,4]
, return true
.
A = [3,2,1,0,4]
, return false
.
//简单遍历下
class Solution {
public:
bool canJump(vector<int>& nums) {
if(nums.size() <= 1) return true;
int s = 0, n = nums.size()-1;
while(true)
{
int l, r;
l = s; r = s+nums[s];
if(l == r) return false;
if(r >= n) return true;
for(int i = l+1; i <= r; i++) s = s+nums[s] > i+nums[i] ? s : i;
}
}
};