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
dominoes
contains 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 ...
随机推荐
- ajax中如何使用全局变量?
在ajax中一般都是采取默认的异步请求,但是有时候参数是需要做到全局通用,这时候发起同步请求. 如下: $.ajax({ type:"post", url:"url路径& ...
- 【PYQT5快速开发】重定义边框、QSS美化皮肤主题
在用qt designer的基础上重定义边框 前言 作为一名技术工作者,偶有使用.开发工具的需求.制作工具时,既不想在界面上花太懂功夫,又想要工具模样与众不同,结果找半天找不到一键换装的功能/拍砖. ...
- 《Effective Objective-C》概念篇
1.运行时 OC 语言由 Smalltalk(20世纪70年代出现的一种面向对象的语言) 演化而来,后者是消息型语言的鼻祖. OC 使用动态绑定的消息结构,在运行时检查对象类型. 使用消息结构的语言, ...
- 刨根究底字符编码之十六——Windows记事本的诡异怪事:微软为什么跟联通有仇?(没有BOM,所以被误判为UTF8。“联通”两个汉字的GB内码,其第一第二个字节的起始部分分别是“110”和“10”,,第三第四个字节也分别是“110”和“10”)
1. 当用一个软件(比如Windows记事本或Notepad++)打开一个文本文件时,它要做的第一件事是确定这个文本文件究竟是使用哪种编码方式保存的,以便于该软件对其正确解码,否则将显示为乱码. 一般 ...
- DevExpress中GridColumnCollection实现父子表数据绑定
绑定数据: 父表: DataTable _parent = _dvFlt.ToTable().Copy(); 子表: DataTable _child = _dvLog.ToTable().Copy( ...
- Visual Studio 2019 安装
目录 写在前面 官网下载 安装 等待安装 启动 写在前面 目前工作的开发环境还是旧版本的Visual Studio 2013版.个人感觉还是有点跟不上时代更新迭代的节奏了.毕竟,技术在进步.如果我们也 ...
- for循环优化
转自:https://blog.csdn.net/lfc18606951877/article/details/78592823 1:多个for循环时,遵循外小内大(从外至里,循环对象size要从小到 ...
- python错误日志记录工具,解决项目排错问题
我们写项目的时候难免会遇到代码报错的问题,遇到这样的问题了如何快速的定位问题并解决问题呢? 我今天来整理了利用python只带的工具来解决这个问题,我能需要使用的库有: logging os 这些都是 ...
- Joomla漏洞复现
漏洞环境及利用 Joomla 3.4.6 : https://downloads.joomla.org/it/cms/joomla3/3-4-6 PHP 版本: 5.5.38 Joomla 3.4 之 ...
- 基于Text-CNN模型的中文文本分类实战
Text-CNN 1.文本分类 转眼学生生涯就结束了,在家待就业期间正好有一段空闲期,可以对曾经感兴趣的一些知识点进行总结. 本文介绍NLP中文本分类任务中核心流程进行了系统的介绍,文末给出一个基于T ...