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/ 把自己刷过的所有题目做一个整理,并且用简洁的语言概括了一下思路,汇总成了一个表格. 题目 ...
随机推荐
- 启动mysql服务器
介绍了启动服务器的两种方式,以及可能遇到的问题 第一种:系统服务 1)可以通过右击方式一步步找到服务 右击计算机->选择管理->找到服务,然后双击打开,找到mysql,我安装的是mysql ...
- SpringBoot 常用配置 静态资源访问配置/内置tomcat虚拟文件映射路径
Springboot 再模板引擎中引入Js等文件,出现服务器拒绝访问的错误,需要配置过滤器 静态资源访问配置 @Configuration @EnableWebMvc public class Sta ...
- iOS webrtc资料总结
1. webrtc远端图像尺寸改变时,如何调整webrtc ios view的大小 https://www.jianshu.com/p/5e1a8f5bbcf7 2. webRTC实现音频通话听筒和扬 ...
- python 爬虫 user-agent 生成
有些网站做了反爬技术,如:比较初级的通过判断请求头部中的user-agent字段来检测是否通过浏览器访问的. 在爬这类网站时需要模拟user-agent import random import re ...
- 【方法】list<?> 两个list集合 查找不同元素,求差值
//方法1 //自己声明list//思路,从list1中删除list2中相同的元素//使用循环遍历对比的方式删除//list1包含list2,list1多与list2//结束得出list1为不相同元素 ...
- UCOSII消息队列
主结构体 typedef struct os_q { /* QUEUE CONTROL BLOCK */ struct os_q *OSQPtr; /* Link to next queue cont ...
- mongodump
mongodump工具是MongoDB提供的用来导出数据的工具,具体的用法参考官方文档:https://docs.mongodb.com/manual/reference/program/mongod ...
- linux删除命令的简单查找使用--临时找来用的
---恢复内容开始--- linux删除某个文件:rm -f filename; mysql清空数据库,并且主键回到1:TRUNCATE TABLE tablename: drop tab ...
- jquey动画效果
jquery的事件没有on,js的有. 1.show() 显示 由小变大缓慢显示 <html lang="en"> <head> <meta ch ...
- c实现二叉树
C实现二叉树 简单说明 实现了先序遍历.中序遍历.后序遍历.搜索 本来想着和平衡二叉树一起放上来的,但是花了一个下午也只是把平衡二叉树原理弄懂和左右旋代码实现,最难的平衡左/右旋还没弄,就不显摆了,就 ...