我发现我非常不擅长解决这种 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的更多相关文章

  1. LeetCode 838. Push Dominoes

    原题链接在这里:https://leetcode.com/problems/push-dominoes/ 题目: There are N dominoes in a line, and we plac ...

  2. Java实现 LeetCode 838 推多米诺(暴力模拟)

    838. 推多米诺 一行中有 N 张多米诺骨牌,我们将每张多米诺骨牌垂直竖立. 在开始时,我们同时把一些多米诺骨牌向左或向右推. 每过一秒,倒向左边的多米诺骨牌会推动其左侧相邻的多米诺骨牌. 同样地, ...

  3. 【LeetCode】838. Push Dominoes 解题报告(Python)

    [LeetCode]838. Push Dominoes 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:// ...

  4. 【leetcode】838. Push Dominoes

    题目如下: 解题思路:本题题目中有一点要求很关键,“we will consider that a falling domino expends no additional force to a fa ...

  5. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

  6. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

  7. 【LeetCode】动态规划(下篇共39题)

    [600] Non-negative Integers without Consecutive Ones [629] K Inverse Pairs Array [638] Shopping Offe ...

  8. 【LeetCode】双指针 two_pointers(共47题)

    [3]Longest Substring Without Repeating Characters [11]Container With Most Water [15]3Sum (2019年2月26日 ...

  9. LeetCode解题报告汇总! All in One!

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 把自己刷过的所有题目做一个整理,并且用简洁的语言概括了一下思路,汇总成了一个表格. 题目 ...

随机推荐

  1. MySQL安装后的初始优化

    mysql数据库在安装之后,有一些内置的库(test库).用户(如root@localhost.localdomain)是不需要的,而且在Linux系统上,yum安装的mysql是默认无root密码的 ...

  2. Java调用WebService方法总结(5)--Axis2调用WebService

    Axis2是新一点Axis,基于新的体系结构进行了全新编写,有更强的灵活性并可扩展到新的体系结构.文中demo所使用到的软件版本:Java 1.8.0_191.Axis2 1.7.9. 1.准备 参考 ...

  3. kubernetes第八章--NFS PersistentVolume

  4. Git提交代码解决方案

    最近做项目不再用小乌龟了,开始用git,便做了记录如下,后期可以看看自己是怎么使用的   下载安装就不说了,直接进入使用环节.   1.使用规则 git pull origin master 和 gi ...

  5. Air for ANE:一星期的调试笔记

    来源:http://blog.csdn.net/hero82748274/article/details/8656674 第一次尝试ANE的东西,让我感觉到很折腾人.adobe 出的这个方案虽然可以解 ...

  6. Python绘制拓扑图(无向图)、有向图、多重图。最短路径计算

    前言: 数学中,“图论”研究的是定点和边组成的图形. 计算机中,“网络拓扑”是数学概念中“图”的一个子集.因此,计算机网络拓扑图也可以由节点(即顶点)和链路(即边)来进行定义和绘制. 延伸: 无向图 ...

  7. Android笔记(六十八) Fragment总结

    Fragment的产生: 为了适应各种尺寸的屏幕,谷歌推出Fragment,可以把Fragment成Activity的一个组成部分,它拥有自己的生命周期.可以接收并处理用户的各种事件,还可以动态的增删 ...

  8. pod健康检查(liveness probe存活探针&&readiness probe 可读性探针)

    在Kubernetes集群当中,我们可以通过配置liveness probe(存活探针)和readiness probe(可读性探针)来影响容器的生存周期.参考文档:https://kubernete ...

  9. nginx编译安装之-./configure 参数详解

    参考官方文档 http://nginx.org/en/docs/configure.html --with开头的,默认是禁用的(没启动的,想使用的话需要在编译的时候加上) --without开头的,默 ...

  10. (Linux基础学习)第七章:echo命令

    第1节:简单说明功能:显示字符语法:echo [-neE][字符串]说明:echo会将输入的字符串送往标准输出.输出的字符串之间以空白字符隔开,并在最后加上换行号选项:-E(默认)不支持\解释功能-n ...