我发现我非常不擅长解决这种 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. C# vb .net实现灰度化特效滤镜

    在.net中,如何简单快捷地实现Photoshop滤镜组中的灰度化呢?答案是调用SharpImage!专业图像特效滤镜和合成类库.下面开始演示关键代码,您也可以在文末下载全部源码: 设置授权 第一步: ...

  2. 网页包抓取工具Fiddler工具简单设置

    当下载好fiddler软件后首先通过以下简单设置,或者有时候fiddler抓取不了浏览器资源了.可以通过以下设置. 设置完成后重启软件.打开网络看看有没有抓取到包.

  3. 关闭 OSX 10.11 SIP (System Integrity Protection) 功能

    关闭 OSX 10.11 SIP (System Integrity Protection) 功能 来源 https://cms.35g.tw/coding/%E9%97%9C%E9%96%89-os ...

  4. datagridview控件 索引-1没有值

    很多WINFORM的开发人员在DataGridView的开发当中,都会出现“索引-1没有值”这个烦人的问题,其实较早之前,我已经大概知道问题的所在,也找到了解决方法,不过一直没有时间去深入研究一下,今 ...

  5. stage1----航空票务系统需求分析报告

    航空票务管理系统需求分析报告 题    目    航空票务管理系统需求分析报告 学    院       信息科学与工程学院 专    业        计算机科学与技术 组    员         ...

  6. title 有背景边框自适应 mobile

    固定宽度,固定高度,来写背景的高度.这样就能居中.

  7. Web应用的生命周期(客户端)

    典型的一个Web应用的生命周期从用户在浏览器输入一串URL,或者单击一个链接开始(就是访问一个页面).而这个生命周期的结束就是我们关闭这个页面. 响应流程: 用户输入一个 URL(生命周期开始) 客户 ...

  8. WPF如何设置启动窗口

    在做系统时,我们想在启动时显示自己想显示的界面,和Winform不同的是它有两种方法 1.在App.xaml中 <Application x:Class="WpfApp1.App&qu ...

  9. Linux命令——taskset

    参考:Linux taskset Command Tutorial for Beginners (with Examples) 简介 taskset命令用于设置进程(或 线程)的处理器亲和性(Proc ...

  10. Xshell 5连接上suse

    # 关闭防火墙 systemctl stop SuSEfirewall2.service systemctl stop SuSEfirewall2_init.service systemctl dis ...