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:

  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:

 1 class Solution {
 2     public String pushDominoes(String dominoes) {
 3         if(dominoes == null || dominoes.length() == 0){
 4             return dominoes;
 5         }
 6         
 7         StringBuilder sb = new StringBuilder();
 8         String d = "L"+dominoes+"R";
 9         int i = 0;
10         int j = 1;
11         while(j < d.length()){
12             if(d.charAt(j) == '.'){
13                 j++;
14                 continue;
15             }
16             
17             int middleCount = j-i-1;
18             if(i>0){
19                 sb.append(d.charAt(i));
20             }
21             
22             // Both sides, cloest point values are the same,
23             // so append all middle part with the same value
24             if(d.charAt(i) == d.charAt(j)){
25                 for(int k = 0; k<middleCount; k++){
26                     sb.append(d.charAt(j));
27                 }
28             }
29             
30             // Cloest left side is L, cloest right side is R, 
31             // then middle part should still be .
32             if(d.charAt(i)=='L' && d.charAt(j)=='R'){
33                 for(int k = 0; k<middleCount; k++){
34                     sb.append('.');
35                 }
36             }
37             
38             // Cloest left side is R, cloest right side is L,
39             // then both sides push to middle, half and half
40             if(d.charAt(i)=='R' && d.charAt(j)=='L'){
41                 for(int k = 0; k<middleCount/2; k++){
42                     sb.append('R');
43                 }
44                 
45                 if(middleCount%2 == 1){
46                     sb.append('.');
47                 }
48                 
49                 for(int k = 0; k<middleCount/2; k++){
50                     sb.append('L');
51                 }
52             }
53             
54             i=j;
55             j++;
56         }
57         
58         return sb.toString();
59     }
60 }

类似Shortest Distance to a Character.

转载于:https://www.cnblogs.com/Dylan-Java-NYC/p/11404838.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值