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 ...
随机推荐
- day47——css介绍、语法结构、选择器、css权重
day47 今日内容 css介绍 CSS(Cascading Style Sheet,层叠样式表)定义如何显示HTML元素,给HTML设置样式,让它更加美观. 语法结构 div{ color:gree ...
- GoLang基础数据类型---字典
Map 是 Go 中的内置类型,它将键与值绑定到一起.可以通过键获取相应的值. 如何创建 map? 可以通过将键和值的类型传递给内置函数 make 来创建一个 map.语法为:make(map[Key ...
- 打印从1到n位数的最大值
题目: 输入数字n,按顺序打印从1到最大的n位十进制数,如输入3,则打印从1.2.3一直到最大的3位数999 参考大数运算的方法.考虑到位数会很大,所以采用字符串的形式解决.对输入的n,创建一个长度为 ...
- 女性对DeepNude脱衣技术的防护
写在前面的话 本文不提供下载方式,开源部分只是社区逆向后公开的部分源码 这篇文章有些人看了可能会比较极端,但不从技术角度分析又谈何防护?攻与防一直存在,不管是安全还是AI都是一样 你极端不极端,它就在 ...
- Gulp 给所有静态文件引用加版本号
在juqery和easyui 盛行的年代许多项目采用纯静态页面去构建前端框架从而实现前后端分离的目的.项目开发周期内往往会频繁修改更新某个文件,当你将文件更新到服务器后客户端由于缓存问题而出现显示异常 ...
- 反射之关于MethodInfo的使用
1.MethodInfo类是在System.Reflection命名空间底下,既然是在Reflection空间底下.故名思议关于反射相关的操作,其中比较重要的方法是Invoke()方法,它是加载相同程 ...
- JVM性能优化--Java的垃圾回收机制
一.Java内存结构 1.Java堆(Java Heap) java堆是java虚拟机所管理的内存中最大的一块,是被所有线程共享的一块内存区域,在虚拟机启动时创建.此内存区域的唯一目的就是存放对象实例 ...
- loj#10012\poj2018 Best Cow Fences(二分)
题目 #10012 「一本通 1.2 例 2」Best Cow Fences 解析 有序列\(\{a_i\}\),设\([l,r]\)上的平均值为\(\bar{x}\),有\(\sum_{i=l}^r ...
- 使pre的内容自动换行(转)小知识
<pre> 元素可定义预格式化的文本.被包围在 pre 元素中的文本通常会保留空格和换行符.而文本也会呈现为等宽字体. <pre> 标签的一个常见应用就是用来表示计算机的源代码 ...
- JavaWeb 之 JSTL 标签
JSTL 标签库 一.概述 1.概念 JSTL : JavaServer Pages Tag Library JSP标准标签库. 是由 Apache 组织提供的开源的免费的 jsp 标签. 2.作用 ...