Leetcode 5274. 停在原地的方案数
纪念第一次正式参加,听说这次题目很水,感觉确实不是很难,一般前两题都很简单,这次,到第三题的时候,都还可以做,emm......
实际代码记录:
#include <iostream>
#include <vector>
#include <math.h>
#include <string>
#include <algorithm>
using namespace std; /*
* 功能:初始化m行n列的二维vector
* 参数:m,n,vector
*/
void init(int m, int n, vector<vector<int> > &vc) {
//初始化一个数组m行 n列
vector<int> level;
level.resize(n);
vc.resize(m, level);
for (int i = ; i < m;i++)
{
for (int j = ; j < n; j++)
{
cin >> vc[i][j];
}
}
} /*
* 功能:输出vector
* 参数:vector
*/
void vecPrint(const vector<vector<int> > vc)
{
//输出
cout << "The vector vc is as follow:" << endl;
int m = vc.size();
int n = vc[].size();
for (int i = ; i < m; i++)
{
for (int j = ; j < n; j++)
{
cout << vc[i][j] << "\t";
}
cout << endl;
}
} int minTimeToVisitAllPoints(vector<vector<int> >& points) {
int m = points.size();
int n = ; //只存在横纵坐标,两个
int cost_time = ; //记录总时间
int x1, x2, y1, y2;
for (int i = ; i < m; i++)
{
//记录两个点
x1 = points[i - ][];
y1 = points[i - ][];
x2 = points[i][];
y2 = points[i][]; //计算两个点之间的最小时间
if (x1 == x2) {
//位于同一条直线上
cost_time += abs(y1-y2);
}
else if (y1 == y2) {
cost_time += abs(x1 - x2);
}
else if (x1 < x2 && y1 < y2) {
//位于右上方
while (x1 < x2 && y1 < y2)
{
x1++;
y1++;
cost_time++;
}
//底下至少有一个为0
cost_time += abs(x1 - x2);
cost_time += abs(y1 - y2);
}
else if (x1 > x2 && y1 < y2) {
//位于左上方
while (x1 > x2 && y1 < y2)
{
x1--;
y1++;
cost_time++;
}
//底下至少有一个为0
cost_time += abs(x1 - x2);
cost_time += abs(y1 - y2);
}
else if (x1 > x2 && y1 > y2) {
//位于左下方
while (x1 > x2 && y1 > y2)
{
x1--;
y1--;
cost_time++;
}
//底下至少有一个为0
cost_time += abs(x1 - x2);
cost_time += abs(y1 - y2);
}
else if (x1 < x2 && y1 > y2) {
//位于右下方
while (x1 < x2 && y1 > y2)
{
x1++;
y1--;
cost_time++;
}
//底下至少有一个为0
cost_time += abs(x1 - x2);
cost_time += abs(y1 - y2);
}
}
return cost_time;
} int countServers(vector<vector<int>>& grid) {
//对每行没列进行标记
int result = ;
int i, j, k;
for (i = ; i < grid.size(); i++)
{
for (j = ; j < grid[].size(); j++)
{
if (grid[i][j] == ) {
//看i行或j列是否存在i
int flag = ;
for (k = ; k < grid.size(); k++)
{
if (k != i && grid[k][j] == )
{
result += ;
flag = ; //表示行找到了
break;
}
}
if (flag == ) { //如果行没有找到,需要找列
for (k = ; k < grid[].size(); k++)
{
if (k != j && grid[i][k] == )
{
result += ;
break;
}
}
}
}
}
}
return result;
} /*
int main()
{ int m, n;
cin >> m >> n;
vector<vector<int> > vc;
init(m,n,vc);
vecPrint(vc);
//cout << minTimeToVisitAllPoints(vc) << endl;
cout << countServers(vc) << endl; return 0;
}
*/ vector<vector<string> > suggestedProducts(vector<string>& products, string searchWord) {
vector<vector<string> > result;
//vector<string> level = products; //处理level
//sort排序试试
sort(products.begin(), products.end());
//输出查看效果
int i;
/*
for (i = 0; i < products.size(); i++) {
cout << products[i] << endl;
}
*/
//排序结束,然后进行查找
int len = searchWord.length();
int k=;
while (len--)
{
int count = ;
string condistr; //候选字符串
condistr = searchWord.substr(, k);
//cout << "候选模式串为:" << condistr << endl;
vector<string> level;
for (i = ; i < products.size(); i++)
{
if (products[i].substr(, k) == condistr)
{
if (count < )
{
level.push_back(products[i]);
count++;
cout << products[i] << " ";
} }
}
cout << endl;
result.push_back(level);
level.clear();
k++;
}
return result;
} /*
int main()
{
vector<string> result;
string str;
int k;
cin >> k;
while (k--)
{
cin >> str;
result.push_back(str);
}
suggestedProducts(result,"mouse");
return 0;
}
*/ /*
5
mobile
mouse
moneypot
monitor
mousepad
*/
题目:(前三题比较简单,只总结最后一题)
有一个长度为 arrLen 的数组,开始有一个指针在索引 0 处。
每一步操作中,你可以将指针向左或向右移动 1 步,或者停在原地(指针不能被移动到数组范围外)。
给你两个整数 steps 和 arrLen ,请你计算并返回:在恰好执行 steps 次操作以后,指针仍然指向索引 0 处的方案数。
由于答案可能会很大,请返回方案数 模 10^9 + 7 后的结果。
示例 1:
输入:steps = 3, arrLen = 2
输出:4
解释:3 步后,总共有 4 种不同的方法可以停在索引 0 处。
向右,向左,不动
不动,向右,向左
向右,不动,向左
不动,不动,不动
理解:
方案一、(自己的想法)
拿到这道题的第一想法是,枚举解答树,只有三种走法,左走,不动,右走,那么我可以申请一个数组d[3],初始化该数组为d[3]={-1,0,1},即可以将当前的索引位置想象成数轴上的原点坐标,当前位置为pos,往左走一步,位置变为pos-1,不动的情况位置仍为pos,往右走一步,位置变为pos+1。利用一个解答树,判断走了steps步以后,是否为原点0,即可求得最终解;
以示例1为例,我们可以画一棵树,如下所示:
注意:1.当位于原点0时,向左走到了-1的位置,超出了最左限制,当位于数组末尾时,向右走到了2的位置,超出了最右限制;
2.当遍历到最后一步,即最后一层时,我们如果总和sum等于0,说明经过上三步之后,仍位于原点位置,否则直接return。
因此,我们总结出如下代码:(不考虑模100000007的结果)
代码如下:
class Solution {
public:
int d[] = { ,-, }; //不动,左,右
int count = ;
int sum = ;
int numWays(int steps, int arrLen) {
dfs(,steps,arrLen,sum);
return count;
} void dfs(int depth,int step,int arrLen,int sum) //step
{
if (depth >= step)
{
if (sum == )
count++;
return;
}
if (sum < || sum >= arrLen)
return;
for (int i = ; i < ; i++)
{
dfs(depth + ,step,arrLen,sum+d[i]);
}
}
};
很遗憾,这样的递归遍历方式,在27步的时候,就超时了,但是贴到这儿是为了学习,哈哈!
方案二、(学习别人的动态规划思想)
学会再来补充啦~
Leetcode 5274. 停在原地的方案数的更多相关文章
- 【js】Leetcode每日一题-停在原地的方案数
[js]Leetcode每日一题-停在原地的方案数 [题目描述] 有一个长度为 arrLen 的数组,开始有一个指针在索引 0 处. 每一步操作中,你可以将指针向左或向右移动 1 步,或者停在原地(指 ...
- leetcode-164周赛-1269-停在原地的方案数
题目描述: 自己的提交: class Solution: def numWays(self, steps: int, arrLen: int) -> int: l = min(steps,arr ...
- LeetCode 5274. Number of Ways to Stay in the Same Place After Some Steps - Java - DP
题目链接:5274. 停在原地的方案数 You have a pointer at index 0 in an array of size arrLen. At each step, you can ...
- 洛谷P1108 低价购买[DP | LIS方案数]
题目描述 “低价购买”这条建议是在奶牛股票市场取得成功的一半规则.要想被认为是伟大的投资者,你必须遵循以下的问题建议:“低价购买:再低价购买”.每次你购买一支股票,你必须用低于你上次购买它的价格购买它 ...
- Codeforces 461B. Appleman and Tree[树形DP 方案数]
B. Appleman and Tree time limit per test 2 seconds memory limit per test 256 megabytes input standar ...
- NOIP2012pj摆花[DP 多重背包方案数]
题目描述 小明的花店新开张,为了吸引顾客,他想在花店的门口摆上一排花,共m盆.通过调查顾客的喜好,小明列出了顾客最喜欢的n种花,从1到n标号.为了在门口展出更多种花,规定第i种花不能超过ai盆,摆花时 ...
- UVa 11137 (完全背包方案数) Ingenuous Cubrency
题意:用13.23……k3这些数加起来组成n,输出总方案数 d(i, j)表示前i个数构成j的方案数则有 d(i, j) = d(i-1, j) + d(i, j - i3) 可以像01背包那样用滚动 ...
- 删数方案数(regex)
[题目描述] 给出一个正整数序列 a,长度为 n,cyb 不喜欢完美,他要删掉一些数(也可以不删,即删掉0个),但是他不会乱删,他希望删去以后,能将 a 分成 2 个集合,使得两个非空集合的数的和相同 ...
- poj2975 Nim 胜利的方案数
Nim Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5545 Accepted: 2597 Description N ...
随机推荐
- Centos7/Ubuntu 初始化硬盘分区、挂载
刚刚在腾讯云买了一台服务器,刚买的服务器的数据盘都是需要自己来分区的,下面就记录一下操作. 通过命令fdisk-l查看硬盘信息 可以看到有两块硬盘/dev/vda和/dev/vdb,启动vda是系统盘 ...
- ServiceStack.Redis简单封装
首先创建RedisConfig配置类 #region 单例模式 //定义单例实体 private static RedisConfig _redisConfig = null; /// <sum ...
- oracle 获取表\视图的列名
select COLUMN_NAME FROM user_col_comments WHERE TABLE_NAME='视图名' select COLUMN_NAME from all_tab_c ...
- MySQL 8.0.13安装修改密码的一个问题,记录一下。
https://blog.csdn.net/qq_37350706/article/details/81707862 关于安装MySQL 8.0.13,本人就不多说了,上面这个链接讲的非常详细 请参考 ...
- istio流量管理
目录 1 准备工作 1.1 在k8s部署istio 1.2 istio自动注入 1.3 应用部署要求 2 负载均衡 3 流量迁移:金丝雀发布 3.1 发布应用 3.2 创建目标规则:Destinati ...
- Linux操作:使用grep排除搜索的目录
使用grep时,当一个目录下有一个包含很多文件的目录,但也不想搜索它,怎么办? 使用 --exclude-dir 选项. 单个目录示例: grep -rni 'http' --exclude-dir= ...
- xss学习
1.了解xss的定义 2.理解xss的原理:反射型和存储型 3.理解xss的攻击方式 4.掌握xss的防御措施
- 剑指:链表中倒数第k个节点
题目描述 输入一个链表,输出该链表中倒数第k个结点. 解法 pre 指针走 k-1 步.之后 cur 指针指向 phead,然后两个指针同时走,直至 pre 指针到达尾结点. 即cur与pre始终相距 ...
- 【转】解决Oracle 11g在用EXP导出时,空表不能导出
一.问题原因: 11G中有个新特性,当表无数据时,不分配segment,以节省空间 .insert一行,再rollback就产生segment了. 该方法是在在空表中插入数据,再删除,则产生segme ...
- js 数组 添加或删除 元素 splice 创建一个新的数组,新数组中的元素是通过检查指定数组中符合条件的所有元素 filter
里面可以用 箭头函数 splice 删除 增加 数组 中元素 操作数组 filter 创建新数组 检查指定数组中符合条件的所有元素