LeetCode 838. Push Dominoes
原题链接在这里:https://leetcode.com/problems/push-dominoes/
题目:
There are N dominoes in a line, and we place each domino vertically upright.
In the beginning, we simultaneously push some of the dominoes either to the left or to the right.

After each second, each domino that is falling to the left pushes the adjacent domino on the left.
Similarly, the dominoes falling to the right push their adjacent dominoes standing on the right.
When a vertical domino has dominoes falling on it from both sides, it stays still due to the balance of the forces.
For the purposes of this question, we will consider that a falling domino expends no additional force to a falling or already fallen domino.
Given a string "S" representing the initial state. S[i] = 'L', if the i-th domino has been pushed to the left; S[i] = 'R', if the i-th domino has been pushed to the right; S[i] = '.', if the i-th domino has not been pushed.
Return a string representing the final state.
Example 1:
Input: ".L.R...LR..L.."
Output: "LL.RR.LLRRLL.."
Example 2:
Input: "RR.L"
Output: "RR.L"
Explanation: The first domino expends no additional force on the second domino.
Note:
0 <= N <= 10^5- String
dominoescontains only'L','R'and'.'
题解:
Try to find the cloest L or R on both sides.
If left side and right side are the same, then middle part should be filled the same, too.
If left is L and right is R, then middle part '.' should not change.
If left is R and right is L, then both sides push to middle.
Use two pointers i, and j to find out both sides cloest L or R.
If it is '.' , then j move until it is finds L or R.
Add a L at the beginning and R at the end to the original string to handle starting and ending '.'
When got the j, calcualte the middle count. Fill the i character. Then the middle part. Then i = j, j++.
Time Compelxity: O(dominoes.length()).
Space: O(1). regardless res.
AC Java:
class Solution {
public String pushDominoes(String dominoes) {
if(dominoes == null || dominoes.length() == 0){
return dominoes;
}
StringBuilder sb = new StringBuilder();
String d = "L"+dominoes+"R";
int i = 0;
int j = 1;
while(j < d.length()){
if(d.charAt(j) == '.'){
j++;
continue;
}
int middleCount = j-i-1;
if(i>0){
sb.append(d.charAt(i));
}
// Both sides, cloest point values are the same,
// so append all middle part with the same value
if(d.charAt(i) == d.charAt(j)){
for(int k = 0; k<middleCount; k++){
sb.append(d.charAt(j));
}
}
// Cloest left side is L, cloest right side is R,
// then middle part should still be .
if(d.charAt(i)=='L' && d.charAt(j)=='R'){
for(int k = 0; k<middleCount; k++){
sb.append('.');
}
}
// Cloest left side is R, cloest right side is L,
// then both sides push to middle, half and half
if(d.charAt(i)=='R' && d.charAt(j)=='L'){
for(int k = 0; k<middleCount/2; k++){
sb.append('R');
}
if(middleCount%2 == 1){
sb.append('.');
}
for(int k = 0; k<middleCount/2; k++){
sb.append('L');
}
}
i=j;
j++;
}
return sb.toString();
}
}
类似Shortest Distance to a Character.
LeetCode 838. Push Dominoes的更多相关文章
- 【LeetCode】838. Push Dominoes 解题报告(Python)
[LeetCode]838. Push Dominoes 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:// ...
- 838. Push Dominoes —— weekly contest 85
Push Dominoes There are N dominoes in a line, and we place each domino vertically upright. In the be ...
- 【leetcode】838. Push Dominoes
题目如下: 解题思路:本题题目中有一点要求很关键,“we will consider that a falling domino expends no additional force to a fa ...
- 838. Push Dominoes
There are N dominoes in a line, and we place each domino vertically upright. In the beginning, we si ...
- [LeetCode] Push Dominoes 推多米诺骨牌
There are N dominoes in a line, and we place each domino vertically upright. In the beginning, we si ...
- [Swift]LeetCode838. 推多米诺 | Push Dominoes
There are N dominoes in a line, and we place each domino vertically upright. In the beginning, we si ...
- Java实现 LeetCode 838 推多米诺(暴力模拟)
838. 推多米诺 一行中有 N 张多米诺骨牌,我们将每张多米诺骨牌垂直竖立. 在开始时,我们同时把一些多米诺骨牌向左或向右推. 每过一秒,倒向左边的多米诺骨牌会推动其左侧相邻的多米诺骨牌. 同样地, ...
- leetcode 838
我发现我非常不擅长解决这种 ummm充满了各种逻辑判断的问题 orz! 因为总是漏少几种情况(很绝望orz) 这道题我是这么判断的 temp为更改后的字符串,dominoes为原字符串 对于原字符串, ...
- All LeetCode Questions List 题目汇总
All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...
随机推荐
- [CF30E]Tricky and Clever Password(KMP+manacher)
首先枚举回文中心,然后显然中心两边要尽量扩展作为middle,这个用manacher实现. 然后注意到suffix的结尾位置是固定的(串尾),那么预处理出以每个位置结尾的串与原串后缀至多能匹配多长,然 ...
- ComPtr的介绍以及使用
ComPtr是为COM而设计的智能指针.它支持WindowsRT,也支持传统Win32.相比ATL里的CComPtr类,它有了一些提升. ComPtr包含在Windows 8.x SDK and Wi ...
- .Net Core WepApi-JWT认证
JWT 介绍 JWT(Json Web Token)是一种开放标准,已Json对象的方式在各方之间安全地传输信息 JWT登陆状态不在服务器端进行存储,而是通过秘钥生成一个具有有效时间的Token返回给 ...
- ASP.NET SignalR 系列(三)之代码实现
说在前头: 因SignalR默认采用camel的编码规范,故前端调用后端的对象或者方法时,首字母均需要小写 创建集线器 创建完,文件中默认创建了一个不带参数Hello方法的示例,我们修改一下,带个参数 ...
- python ocr中文识别库 tesseract安装及问题处理
这个破东西,折腾了快1个小时,网上的教材太乱了. 我解决的主要是windows的问题 先下载exe.(一看到这个,我就有种预感,不妙) https://digi.bib.uni-mannheim.de ...
- exchange From Middle English eschaunge
exchange From Middle English eschaunge, borrowed from Anglo-Norman eschaunge exchange 1.An act of ex ...
- Xcode 10 Archive 时电脑卡死
Xcode 10 Archive Unity5.x 导出工程时电脑卡死.解决办法:Targets - Build Settings - Debug Information Format 设置成DWAR ...
- 使用Prometheus监控Linux系统各项指标
首先在Linux系统上安装一个探测器node explorer, 下载地址https://prometheus.io/docs/guides/node-exporter/ 这个探测器会定期将linux ...
- unity 实现技能释放
要实现技能释放其实很简单,说白了就是在指定的位置Instantiate一个对应的例子特效.我走的弯路主要在寻找这个指定位置上. 对于指向性技能就不多说了,因为是有确切目标的(当然首先判断下技能能不能对 ...
- EF 批量增删改 EntityFramework.Extensions
EntityFramework.Extensions 1.官方网站 http://entityframework-extensions.net/ 2 破解版 Z.EntityFramework.E ...