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

最新文章

  1. ASP.NET Aries JSAPI 文档说明:AR.Utility
  2. Andrew N.G的机器学习公开课学习笔记(一):机器学习的动机与应用
  3. 2016春招Android开发实习生(网易传媒)笔试
  4. MATLAB实现频数直方图——hist的使用
  5. js 检查是否为手机端
  6. maven出错The folder is already a source folder
  7. (转).NET代码混淆实践
  8. NET权限系统开源项目
  9. Android 子线程中进行UI操作遇到的小问题
  10. Android下使用busybox的ifconfig
  11. centos7 部署 ELK 日志系统
  12. Codeforces 279D The Minimum Number of Variables 状压dp
  13. 阿里中间件——消息中间件Notify和MetaQ
  14. SharePoint Server 2016 - Configure Office Online Server
  15. 线程---local数据隔离
  16. 使用WireShark分析使用RedisTemplate取不到值的问题
  17. 初学Direct X(7) ——位图的旋转,缩放以及平移
  18. MySQL会创建临时表的几种情况
  19. 网页调起App之应用实践
  20. 用Python实现的数据结构与算法:基本搜索

热门文章

  1. hdu 1427 速算24点【暴力枚举】
  2. appium 方法整理
  3. Kibana访问报错
  4. 用es6 封装的对数组便捷操作的算法
  5. C# vb .net图像合成-合成星形
  6. java中四种权限修饰符区别
  7. 原油petrolaeum石油 Archaic spelling of petroleum
  8. 深入理解JVM-hotspot虚拟机对象探秘
  9. day 03 预科
  10. nginx使用过程中遇到的问题及基本使用总结