当前位置:网站首页>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;//到达最后那个点,说明成功到达
}
};
边栏推荐
- GPS from getting started to giving up (XVIII), multipath effect
- PostgreSQL 修改数据库用户的密码
- Huawei has launched attacks in many industries at the same time, and its frightening technology has made European and American enterprises tremble
- GPS从入门到放弃(十七) 、对流层延时
- PostgreSQL 安装gis插件 CREATE EXTENSION postgis_topology
- LeetCode学习记录(从新手村出发之杀不出新手村)----1
- AI 企业多云存储架构实践 | 深势科技分享
- Kohana 数据库
- GPS从入门到放弃(十五)、DCB差分码偏差
- UNI-Admin基础框架怎么关闭创建超级管理员入口?
猜你喜欢
Sparkshuffle process and Mr shuffle process
【10点公开课】:视频质量评价基础与实践
Happy sound 2[sing.2]
2020 Bioinformatics | GraphDTA: predicting drug target binding affinity with graph neural networks
Persistence / caching of RDD in spark
关于char[]数组通过scanf赋值使用上的一些问题。。
GPS從入門到放弃(十三)、接收機自主完好性監測(RAIM)
Reptile practice (V): climbing watercress top250
Xiaoman network model & http1-http2 & browser cache
微信红包封面小程序源码-后台独立版-带测评积分功能源码
随机推荐
Aggregate function with key in spark
UNI-Admin基础框架怎么关闭创建超级管理员入口?
Oracle-控制文件及日志文件的管理
Four data streams of grpc
Save and retrieve strings
The golden age of the U.S. technology industry has ended, and there have been constant lamentations about chip sales and 30000 layoffs
11、 Service introduction and port
GPS从入门到放弃(十八)、多路径效应
PostgreSQL 修改数据库用户的密码
The underlying implementation of string
数字化转型挂帅复产复工,线上线下全融合重建商业逻辑
华为在多个行业同时出击,吓人的技术让欧美企业瑟瑟发抖
Wechat red envelope cover applet source code - background independent version - source code with evaluation points function
[sciter bug] multi line hiding
JS method to stop foreach
保存和检索字符串
Codeforces Round #274 (Div. 2) –A Expression
Kohana 数据库
MariaDb数据库管理系统的学习(一)安装示意图
MongoDB(三)——CRUD