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/ 把自己刷过的所有题目做一个整理,并且用简洁的语言概括了一下思路,汇总成了一个表格. 题目 ...
随机推荐
- Java中Date时区的转换
1.Date中保存的是什么? 在java中,只要我们执行 Date date = new Date(); 就可以得到当前时间.如: Date date = new Date(); System.ou ...
- PKUSC2019题解
$D1T1$:$n$个村庄,第$i$个村庄的人要去第$p_i$个村庄(保证$p_i$为排列),每次可以将相邻两个村庄的人位置交换直到所有人都到达目的地.再给定一个长为$n-1$的排列$a$,表示第$i ...
- 使用springboot实现一个简单的restful crud——02、dao层单元测试,测试从数据库取数据
接着上一篇,上一篇我们创建了项目.创建了实体类,以及创建了数据库数据.这一篇就写一下Dao层,以及对Dao层进行单元测试,看下能否成功操作数据库数据. Dao EmpDao package com.j ...
- java web编程 servlet3
- js 删除 数组中某个元素(转载)
来源:https://www.jb51.net/article/134312.htm js删除数组中某一项或几项的几种方法 https://www.jb51.net/article/154737.ht ...
- Redis主从同步之主库挂死解决方案
Redis实现了主从同步,但是主库挂死了,如何处理 方案:切换主库的身份 # 连接从库 [root@localhost redis-]# redis-cli -p # 取消从库身份 > slav ...
- Linux磁盘管理——BIOS和UEFI
参考:BIOS and UEFI - CompTIA A+ 220-901 - 1.1 BIOS and UEFI As Fast As Possible 严格上来说BIOS和UEFI除了在搜索boo ...
- 16.centos7基础学习与积累-002
1.从头开始积累centos7系统运用 大牛博客:https://blog.51cto.com/yangrong/p5 互联网公司服务器品牌: dell 服务器品牌: 1U=4.45CM 2010年以 ...
- dt二次开发之-url伪静态的自定义
dt内核的方便性在于代码内核完全开源,都可以根据自身需要进行优化整改,个人在这段时间的深入研究,发现这套内核的方便性,今天继续给大家分享下DT的url伪静态如何自定义函数. url自定义文件是在api ...
- Oracle的instr()函数和substr()函数
INSTR()函数 可以使用instr函数对某个字符串进行判断,判断其是否含有指定的字符. 在一个字符串中查找指定的字符,返回被查找到的指定的字符的位置. 语法: instr(sourceString ...