leetcode 838
我发现我非常不擅长解决这种 ummm充满了各种逻辑判断的问题 orz! 因为总是漏少几种情况(很绝望orz)
这道题我是这么判断的
temp为更改后的字符串,dominoes为原字符串
对于原字符串,只需要改变“.”的状态就好了
设置两个指针 low,high
扫描到当前字符“.”时,low从“.”左边往前扫,high从“.”右边往后扫
存在以前几种情况
1. dominoes[high]=='L'&&dominoes[low]!='R'
需要将“.”变为“L”
2.dominoes[low]=='R'&&dominoes[high]!='L'
需要将“.”变为“R”
3. dominoes[low]=='.'&&dominoes[high]=='.'
同时移动low和high
4. dominoes[low]=='L'&&dominoes[high]=='.'
只需要移动high指针,因为可能之后会出现L这种情况
5. dominoes[low]=='.'&&dominoes[high]=='R'
只需要移动low指针,因为可能前面会有R这种情况
6. 其他情况,是不需要改变“.”的状态的
最后对改变后的temp字符串考虑两种情况:
1.第一个字符是“.”
2.末尾字符是“.”
给几个特殊样例:
".L.R...LR..L.."
"..R.."
".R........"
".......L.L"
"RRRRRRL..."
"L.L...L.L.LL.L..L..."
代码如下
class Solution {
public:
string pushDominoes(string dominoes) {
string temp = dominoes;
if(dominoes[]=='.')
{
if(dominoes[]=='L')
temp[] = 'L';
}
for(int i =;i<dominoes.length()-;i++)
{
if(dominoes[i]=='.')
{
int low = i-;
int high = i+;
while(low>=&&high<dominoes.length())
{
if(dominoes[high]=='L'&&dominoes[low]!='R')
{
temp[i] = 'L';
break;
}
else if(dominoes[low]=='R'&&dominoes[high]!='L')
{
temp[i] = 'R';
break;
}
else if(dominoes[low]=='.'&&dominoes[high]=='.')
{
low--;
high++;
}
else if(dominoes[low]=='L'&&dominoes[high]=='.')
{
high++;
}
else if(dominoes[low]=='.'&&dominoes[high]=='R')
{
low--;
}
else
break;
}
}
}
if(dominoes[dominoes.length()-]=='.')
{
int low = dominoes.length()-;
while(low>=)
{
if(temp[low]=='.')
low--;
else
break;
}
if(temp[low]=='R')
{
for(int i =low+;i<dominoes.length();i++)
temp[i] ='R';
}
}
if(dominoes[]=='.')
{
int high = ;
while(high<dominoes.length())
{
if(temp[high]=='.')
high++;
else
break;
}
if(temp[high]=='L')
{
for(int i =high-;i>=;i--)
temp[i] ='L';
}
}
return temp;
}
};
如果大家对我的代算法思路有什么改进,欢迎评论区留言!
leetcode 838的更多相关文章
- LeetCode 838. Push Dominoes
原题链接在这里:https://leetcode.com/problems/push-dominoes/ 题目: There are N dominoes in a line, and we plac ...
- Java实现 LeetCode 838 推多米诺(暴力模拟)
838. 推多米诺 一行中有 N 张多米诺骨牌,我们将每张多米诺骨牌垂直竖立. 在开始时,我们同时把一些多米诺骨牌向左或向右推. 每过一秒,倒向左边的多米诺骨牌会推动其左侧相邻的多米诺骨牌. 同样地, ...
- 【LeetCode】838. Push Dominoes 解题报告(Python)
[LeetCode]838. Push Dominoes 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:// ...
- 【leetcode】838. Push Dominoes
题目如下: 解题思路:本题题目中有一点要求很关键,“we will consider that a falling domino expends no additional force to a fa ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
- 【LeetCode】动态规划(下篇共39题)
[600] Non-negative Integers without Consecutive Ones [629] K Inverse Pairs Array [638] Shopping Offe ...
- 【LeetCode】双指针 two_pointers(共47题)
[3]Longest Substring Without Repeating Characters [11]Container With Most Water [15]3Sum (2019年2月26日 ...
- LeetCode解题报告汇总! All in One!
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 把自己刷过的所有题目做一个整理,并且用简洁的语言概括了一下思路,汇总成了一个表格. 题目 ...
随机推荐
- screen命令下,自启动设置
建立autostart.sh文件 #!/bin/bash screen_name=$"coffeetest:9100" screen -dmS $screen_name cmd=$ ...
- 【开发工具】- Xshell工具的下载和安装
下载地址:https://www.netsarang.com/zh/free-for-home-school/ Xshell 是一个强大的安全终端模拟软件,它支持SSH1, SSH2, 以及Micro ...
- ElementUI 源码定制防坑指南
背景 我司OA系统公文管理模块Office在线编辑使用的是金格IWebOffice中间件[PPAPI插件,通过<object>标签加载],IWebOffice在chrome中设置div盒子 ...
- element-ui 穿梭框使用axios数据查询
//class="input"样式自写,用来覆盖穿梭框自带的搜索,它自带的搜索框不能搜索外部数据,只能查询在穿梭框内的数据 <div style="text-ali ...
- Appscan漏洞之跨站点请求伪造(CSRF)
公司前段时间使用了Fortify扫描项目代码,在修复完这些Fortify漏洞后,最近又启用了Appscan对项目代码进行漏洞扫描,同样也是安排了本人对这些漏洞进行修复.现在,针对修复过的Appscan ...
- python实战项目
没有一个完整的项目开发过程,是不会对整个开发流程以及理论知识有牢固的认知的,对于怎样将所学的理论知识应用到实际开发中更是不得而知了! 以上就是我们在学习过程中必须要有项目实战开发经验的原因,其实无论项 ...
- springboot学习入门简易版九---springboot2.0整合多数据源mybatis mysql8+(22)
一个项目中配置多个数据源(链接不同库jdbc),无限大,具体多少根据内存大小 项目中多数据源如何划分:分包名(业务)或注解方式.分包名方式类似多个不同的jar,同业务需求放一个包中. 分包方式配置多数 ...
- Mysql8.0.17安装(windows10)
1.因为系统重装 又双叒叕开始了装mysql数据库 下载安装包 https://dev.mysql.com/downloads/mysql/ 2.解压到你想安装的地方 3.解压完是没有图红色框中的文 ...
- Python学习日记(十三) 递归函数和二分查找算法
什么是递归函数? 简单来说就是在一个函数中重复的调用自己本身的函数 递归函数在调用的时候会不断的开内存的空间直到程序结束或递归到一个次数时会报错 计算可递归次数: i = 0 def func(): ...
- php基本数据类型
trim()函数,用于去除字符串首尾空格和特殊字符返回的是去掉的空格和特殊字符后的字符串 string trim(string str [,string charlist]); str 要操作的字符串 ...