原题链接在这里: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:

  1. 0 <= N <= 10^5
  2. 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的更多相关文章

  1. 【LeetCode】838. Push Dominoes 解题报告(Python)

    [LeetCode]838. Push Dominoes 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:// ...

  2. 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 ...

  3. 【leetcode】838. Push Dominoes

    题目如下: 解题思路:本题题目中有一点要求很关键,“we will consider that a falling domino expends no additional force to a fa ...

  4. 838. Push Dominoes

    There are N dominoes in a line, and we place each domino vertically upright. In the beginning, we si ...

  5. [LeetCode] Push Dominoes 推多米诺骨牌

    There are N dominoes in a line, and we place each domino vertically upright. In the beginning, we si ...

  6. [Swift]LeetCode838. 推多米诺 | Push Dominoes

    There are N dominoes in a line, and we place each domino vertically upright. In the beginning, we si ...

  7. Java实现 LeetCode 838 推多米诺(暴力模拟)

    838. 推多米诺 一行中有 N 张多米诺骨牌,我们将每张多米诺骨牌垂直竖立. 在开始时,我们同时把一些多米诺骨牌向左或向右推. 每过一秒,倒向左边的多米诺骨牌会推动其左侧相邻的多米诺骨牌. 同样地, ...

  8. leetcode 838

    我发现我非常不擅长解决这种 ummm充满了各种逻辑判断的问题 orz! 因为总是漏少几种情况(很绝望orz) 这道题我是这么判断的 temp为更改后的字符串,dominoes为原字符串 对于原字符串, ...

  9. All LeetCode Questions List 题目汇总

    All LeetCode Questions List(Part of Answers, still updating) 题目汇总及部分答案(持续更新中) Leetcode problems clas ...

随机推荐

  1. Go语言【数据结构】字符串

    字符串 简介 一个字符串是一个不可改变的字节序列,字符串通常是用来包含人类可读的文本数据.和数组不同的是,字符串的元素不可修改,是一个只读的字节数组.每个字符串的长度虽然也是固定的,但是字符串的长度并 ...

  2. python使用自带模块httplib进行http请求

    #-*- encoding:utf-8 -*- import httplib, time class httpRequest(): def __init__(self, headers, reques ...

  3. python ---升级所有安装过的package

    # -*- coding:utf8 -*- import pip from subprocess import call from pip._internal.utils.misc import ge ...

  4. mongodb常规操作语句

    db.c_user.insertOne({ name: "ljm", pwd: "123456" }); //插入一个 db.c_user.insertMany ...

  5. java之mybatis整合spring

    这篇讲解spring+mybatis的整合. 目录结构: 一. 整合spring的第一种方法 1. 新建 java 项目 : spring_mybatis 2.导入jar 包-----spring和m ...

  6. 3. ABP .NETCore 添加企业微信第三方登录

    1.企业微信登录步骤 1.获取企业微信Token 官方文档:https://work.weixin.qq.com/api/doc#90000/90135/91039 2.通过Token 与前端传的Co ...

  7. int转换为String,常用的四种方法。

    int i = 100; 方法一:String s1 = i + " "; 方法二:String s2 = String.valueof(i); 方法三(先转换为Integer类型 ...

  8. Java自学-控制流程 continue

    Java的 continue语句 继续下一次循环 示例 1 : continue 如果是双数,后面的代码不执行,直接进行下一次循环 public class HelloWorld { public s ...

  9. OO第三单元作业总结

    OO第三单元作业总结--JML 第三单元的主题是JML规格的学习,其中的三次作业也是围绕JML规格的实现所展开的(虽然感觉作业中最难的还是如何正确适用数据结构以及如何正确地对于时间复杂度进行优化). ...

  10. 启动OpenOffice服务

    下载安装 安装OpenOffice 4.1.6:下载路径:http://www.openoffice.org/zh-cn/download/ 启动 用以下命令启动OpenOffice服务,注意ip,如 ...