当前位置:网站首页>【leetcode】838. Push domino (Analog)
【leetcode】838. Push domino (Analog)
2022-06-24 18:25:00 【Chinese fir sauce_】
subject :
838. Push domino
n A row of dominoes , Erect each domino vertically . At the beginning , At the same time, push some dominoes to the left or right .
Every second , The dominoes falling to the left will push the dominoes adjacent to the left . similarly , The dominoes on the right will also push the adjacent dominoes on the right .
If a vertically erected dominoes is falling on both sides at the same time , Because of the balance of forces , The dominoes remain the same .
As far as this is concerned , We would assume that a falling domino does not exert additional force on other falling or fallen dominoes .
Give you a string dominoes Indicates the initial state of this row of dominoes , among :
dominoes[i] = ‘L’, It means the first one i A domino is pushed to the left ,
dominoes[i] = ‘R’, It means the first one i A domino is pushed to the right ,
dominoes[i] = ‘.’, It means that the third party has not been promoted i Dominoes .
Returns a string representing the final state .
Example 1:
Input :dominoes = “RR.L”
Output :“RR.L”
explain : The first dominoes didn't put extra force on the second one .
Example 2:
Input :dominoes = “.L.R…LR…L…”
Output :“LL.RR.LLRRLL…”
Tips :
n == dominoes.length
1 <= n <= 105
dominoes[i] by ‘L’、‘R’ or ‘.’
simulation :
We can enumerate all consecutive dominoes that have not been pushed , According to the two sides of this piece of dominoes ( If any ) The direction of pushing down determines the final state of this dominoes :
- If the dominoes on both sides are in the same direction , Then this continuous vertical dominoes will fall in the same direction .
- If the dominoes on both sides are opposite , Then this piece of dominoes will fall to the middle .
- If the dominoes on both sides are opposite , Then the dominoes will remain upright .
Specially , If there is no dominoes pushed down on the left , Then suppose there is a dominoes that fall to the left ; If there are no dominoes pushed down on the right , Then suppose there is a dominoes that fall to the right . This assumption will not destroy the final state of the dominoes , And the boundary case can also fall into the above three cases .
We can use two pointers i and j Not all dominoes are pushed continuously ,left and right Indicates the direction of the dominoes on both sides . Calculate the final state of the dominoes according to the above three cases .
class Solution {
// simulation
public String pushDominoes(String dominoes) {
char[] s = dominoes.toCharArray();
int n = s.length, i = 0;
char left = 'L';
while (i < n) {
int j = i;
while (j < n && s[j] == '.') {
// Find a continuous piece of dominoes that have not been pushed
j++;
}
char right = j < n ? s[j] : 'R';
if (left == right) {
// In the same direction , Then these erected dominoes will fall in the same direction
while (i < j) {
s[i++] = right;
}
} else if (left == 'R' && right == 'L') {
// Relative direction , Then fall from both sides to the middle
int k = j - 1;
while (i < k) {
s[i++] = 'R';
s[k--] = 'L';
}
}
left = right;
i = j + 1;
}
return new String(s);
}
}
边栏推荐
- Application service access configuration parameters
- Nine practical guidelines for improving responsive design testing
- Optimizing bloom filter: challenges, solutions, and comparisons
- Considerations for it project demand analysis
- Nacos cluster starts throwing set of SQL_ SELECT_ LIMIT is not support
- Overall planning and construction method of digital transformation
- EasyCVR国标协议接入的通道,在线通道部分播放异常是什么原因?
- [can you really use es] Introduction to es Basics (I)
- [NLP] 3 papers on how Stanford team builds a better chat AI
- Leetcode topic [array] -46- full arrangement
猜你喜欢

Why are more and more people studying for doctors? Isn't it more and more difficult to graduate a doctor?

Overall planning and construction method of digital transformation

How do yaml files and zmail collide with the spark of the framework, and how can code and data be separated gracefully?

Get max value of a bit column - get max value of a bit column

13 ways to reduce the cost of cloud computing
[quick news] the jeecgboot low code platform was successfully selected into the 2021 scientific innovation China · open source innovation list

Constantly changing the emergency dialing of harmonyos ETS during the new year

Digital transformation informatization data planning and technology planning

Flutter dart regular regexp matches non printing characters \cl\cj\cm\ck

Error reported after NPM I
随机推荐
基于BGP实现纯三层容器网络方案
腾讯云荣获“可信云技术最佳实践-虚拟化”
Number of occurrences of numbers in the array (medium difficulty)
Welcome to the network security threat information sharing program
Easygbs video platform TCP active mode streaming exception repair
Uniapp wechat applet calls mobile map to navigate to the target point
Design topic: MATLAB cellular automata personnel evacuation
Two micro service interviews where small companies suffer losses
Redpacketframe and openmode packages
EasyCVR国标协议接入的通道,在线通道部分播放异常是什么原因?
Seven strategies for successfully integrating digital transformation
How do yaml files and zmail collide with the spark of the framework, and how can code and data be separated gracefully?
You don't know about this inspection platform. It's a big loss!
Flutter dart regular regexp special characters $, () (IV)
Mariana Trench, Facebook's open source code analysis tool
How to select the best test cases for automation?
How to create simple shapes in illustrator 2022
Fragment usage
【你真的会用ES吗】ES基础介绍(一)
腾讯云TCS:面向应用的一站式PaaS 平台