当前位置:网站首页>LeetCode刷题(十一)——顺序刷题51至55
LeetCode刷题(十一)——顺序刷题51至55
2022-07-06 14:10:00 【CAccept】
51.N 皇后
按照国际象棋的规则,皇后可以攻击与之处在同一行或同一列或同一斜线上的棋子。
n 皇后问题 研究的是如何将 n 个皇后放置在 n×n 的棋盘上,并且使皇后彼此之间不能相互攻击。
给你一个整数 n ,返回所有不同的 n 皇后问题 的解决方案。
每一种解法包含一个不同的 n 皇后问题 的棋子放置方案,该方案中 ‘Q’ 和 ‘.’ 分别代表了皇后和空位。
示例1:
输入:n = 4
输出:[[".Q..","...Q","Q...","..Q."],["..Q.","Q...","...Q",".Q.."]]
解释:如上图所示,4 皇后问题存在两个不同的解法。
分析:既然要保持每行,每列,两条斜线仅仅只能存在一个皇后,可以创建多个标识bool数组,我们可以每行每行地放皇后所以我们只需要创建每列和斜线的标识数组
,对于列标识数组还是很简单的,而斜线的标识数组要如何标识呢?看下图:
代码:
class Solution {
public:
vector<vector<string>>res;
vector<bool>col,dg,udg;
vector<string>path;
int nt;
vector<vector<string>> solveNQueens(int n) {
nt = n;
path = vector<string>(n,string(n,'.'));
col = dg = udg =vector<bool>( n * 2);//有x+y所以最大也不会超过2*n
dfs(0);//第0行开始搜索
return res;
}
void dfs(int u)
{
if(u == nt)
{
res.push_back(path);
return;
}
//枚举每一列
for(int i = 0; i < nt; i++)
{
if(!col[i] && !dg[u - i + nt] && !udg[u + i])
{
col[i] = dg[u - i + nt] = udg[u + i]=true;
path[u][i]='Q';
dfs(u + 1);
//恢复现场
col[i] = dg[u - i + nt] = udg[u + i] = false;
path[u][i]='.';
}
}
return;
}
};
52.N皇后 II
n 皇后问题 研究的是如何将 n 个皇后放置在 n × n 的棋盘上,并且使皇后彼此之间不能相互攻击。给你一个整数 n ,返回 n 皇后问题 不同的解决方案的数量
。
示例1:
输入:n = 4
输出:2
解释:如上图所示,4 皇后问题存在两个不同的解法。
分析:这题的思路是一样的只是要求的东西是方案数量,可以用上题代码返回size就可以,也可以用下面的代码实现。
代码:
class Solution {
public:
int nt;
vector<string>path;
vector<bool>st,udg,dg;
int totalNQueens(int n) {
nt = n;
path = vector<string> (nt,string( n , '.' ));
st = vector<bool> (n);
udg = dg = vector<bool>(n*2);
return dfs(0);
}
int dfs(int u)
{
if(u == nt) return 1;
int res = 0;
for(int i = 0; i < nt; i++)
{
if(!st[i] && !udg[u - i + nt] && !dg[u + i])
{
st[i] = udg[u - i + nt] = dg[u + i] = true;
res += dfs(u + 1);
//恢复现场
st[i] = udg[u - i + nt] = dg[u + i] = false;
}
}
return res;
}
};
53.最大子数组和
给你一个整数数组 nums
,请你找出一个具有最大和的连续子数组(子数组最少包含一个元素),返回其最大和。子数组 是数组中的一个连续部分。
示例 1:
输入:nums = [-2,1,-3,4,-1,2,1,-5,4]
输出:6
解释:连续子数组 [4,-1,2,1] 的和最大,为 6 。
分析:这一题可以使用动态规划来做可以做到时间复杂度为O(n)
、空间复杂度为O(1)
,分析思路如下图:
代码:
class Solution {
public:
int maxSubArray(vector<int>& nums) {
int res = INT_MIN;
for(int i = 0, last = 0; i<nums.size(); i++)
{
last = nums[i] + max(last , 0);
res = max(res , last);
}
return res;
}
};
54.螺旋矩阵
给你一个 m 行 n 列的矩阵 matrix ,请按照顺时针螺旋顺序
,返回矩阵中的所有元素。
示例 1:
输入:matrix = [[1,2,3],[4,5,6],[7,8,9]]
输出:[1,2,3,6,9,8,7,4,5]
分析:当进行顺时针移动时会对当前的(x,y)进行移动,我们可以按照这个来设置两个偏移数组来进行移动,在移动时判断是否越界或者是元素已经遍历过,如下图:
代码:
class Solution {
public:
vector<int> spiralOrder(vector<vector<int>>& matrix) {
int n = matrix.size();
int m = matrix[0].size();
vector<int>res;
if(!matrix.size())return res;
vector<vector<bool>>st(n , vector<bool>(m));//是否已经遍历过状态数组
//偏移数组dx,dy
int dx[] = {
0 , 1 , 0 , -1};
int dy[] = {
1 , 0 , -1 , 0};
int a , b;
for(int i = 0 , x = 0 , y = 0, d = 0; i < n * m; i++)
{
res.push_back(matrix[x][y]);
st[x][y] = true;
a = dx[d] + x ,b = dy[d] + y;
if(a>=n||a<0||b>=m||b<0||st[a][b])
{
d = (d+1)%4;
a = dx[d] + x ,b = dy[d] + y;
}
x = a , y = b;
}
return res;
}
};
55.跳跃游戏
给定一个非负整数数组 nums
,你最初位于数组的 第一个下标 。数组中的每个元素代表你在该位置可以跳跃的最大长度。判断你是否能够到达最后一个下标。
示例1:
输入:nums = [2,3,1,1,4]
输出:true
解释:可以先跳 1 步,从下标 0 到达下标 1, 然后再从下标 1 跳 3 步到达最后一个下标。
分析:通过分析,假如我们可以跳跃到第k个点那么就一定可以跳跃到k-1这个点,以此类推,可以发现如果可以跳到i这个点,那么就可以跳到0—k-1,同样的如果连0—k-1都到不了那么一定到不了第k个点。
代码:
class Solution {
public:
bool canJump(vector<int>& nums) {
for(int i = 0, j = 0; i < nums.size(); i++)
{
if (j < i) return false;//说明到不了第i个点
j = max(j , i + nums[i]);//将j与i+nums[i]进行比较
}
return true;//到达最后那个点,说明成功到达
}
};
边栏推荐
- [MySQL] online DDL details
- [Yu Yue education] higher mathematics of Nanchang University (2) reference materials
- Digital transformation takes the lead to resume production and work, and online and offline full integration rebuilds business logic
- Force deduction question 500, keyboard line, JS implementation
- 基于InsightFace的高精度人脸识别,可直接对标虹软
- The golden age of the U.S. technology industry has ended, and there have been constant lamentations about chip sales and 30000 layoffs
- HDU 4912 paths on the tree (lca+)
- AI enterprise multi cloud storage architecture practice | Shenzhen potential technology sharing
- MariaDb数据库管理系统的学习(一)安装示意图
- Codeforces Round #274 (Div. 2) –A Expression
猜你喜欢
Broadcast variables and accumulators in spark
Shell product written examination related
数字化转型挂帅复产复工,线上线下全融合重建商业逻辑
Vit paper details
Embedded common computing artifact excel, welcome to recommend skills to keep the document constantly updated and provide convenience for others
Why rdd/dataset is needed in spark
GPS from entry to abandonment (XVII), tropospheric delay
[asp.net core] set the format of Web API response data -- formatfilter feature
GPS from getting started to giving up (XV), DCB differential code deviation
2500个常用中文字符 + 130常用中英文字符
随机推荐
11、 Service introduction and port
[10:00 public class]: basis and practice of video quality evaluation
Unity3D学习笔记6——GPU实例化(1)
Uni app app half screen continuous code scanning
一行代码可以做些什么?
【sciter】: 基于 sciter 封装通知栏组件
Persistence / caching of RDD in spark
Powerful domestic API management tool
Sql: stored procedures and triggers - Notes
Kohana database
What is the RDD operator in spark
Reinforcement learning - learning notes 5 | alphago
Xiaoman network model & http1-http2 & browser cache
1D convolution detail
Unity3D学习笔记6——GPU实例化(1)
PostgreSQL 修改数据库用户的密码
Make menuconfig has a recipe for target 'menuconfig' failed error
小满网络模型&http1-http2 &浏览器缓存
GPS从入门到放弃(十四)、电离层延时
OpenCV300 CMake生成project在项目过程中的问题