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/ 把自己刷过的所有题目做一个整理,并且用简洁的语言概括了一下思路,汇总成了一个表格. 题目 ...
随机推荐
- 关于base64的一个小细节
Base64出现\r\n的问题 前段时间遇到这么一个小问题: 后台接口返回一个图片的base64串,同事拿着这个字符串,找了一个在线图片和Base64字符串互转的工具网站,想将字符串转成图片,死活转不 ...
- java并发编程之原子操作
先来看一段简单的代码,稍微有点并发知识的都可以知道打印出结果必然是一个小于20000的值 package com.example.test.cas; import java.io.IOExceptio ...
- VisualStudio 连接 MySql 实现增删查改
首先创建数据库,建立一个用户登录表 2.visualStudio默认是不支持MySql的,要想通过Ado.Net 操作MySql 需要在管理NeGet包添加对MySql.Data 和 MySql.D ...
- DevExtreme学习笔记(一) DataGrid中注意事项
1.阻止cell编辑 config.onEditorPreparing = function (e) { if (e.dataField === 'xx' && e.row.data. ...
- C# 使用代理实现方法过滤
一.为什么要进行方法过滤 一些情况下我们需要再方法调用前记录方法的调用时间和使用的参数,再调用后需要记录方法的结束时间和返回结果,当方法出现异常的时候,需要记录异常的堆栈和原因,这些都是与业务无关的代 ...
- 4.linux下配置Golang的环境变量
装好linux后优先在linux上配置Golang开发环境. 1.到Go语言中文网下载Linux安装包 https://studygolang.com/dl 2.到下载的目录下解压,下载的文件一般在“ ...
- 社交类app开发( 仿陌陌 客户端+服务器端)
一.开发所需要的技术 手机端需要Android/iOS开发人员,服务器端需要php/数据库开发人员, 如果再做网页版的话,WEB开发也是要的. 即时通讯 GPS地图 群聊 差不多 对 http so ...
- Xen们和Open Stack们
1.虚拟化技术:XEN.KVM.ESXI 2.虚拟化管理:Eucalyptus, OpenNebula, OpenStack, OpenQRM, XenServer, Oracle VM, Cloud ...
- js基础知识4
原文链接:https://book.apeland.cn/details/361/#3.getElementsByClassName()方法 DOM介绍 1.文档:DOM中的”D” DOM是”Do ...
- Codes: MODERN ROBOTICS Ch.3_Expo. Coods.基础代码实现
%%1 Transform omega to so3 matrix % W for skew-symmetirc matirx % w for omega, angular velocity func ...