当前位置:网站首页>Deduct daily question 838 of a certain day
Deduct daily question 838 of a certain day
2022-07-26 10:18:00 【Fabulouskkk】
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 .

harm Want to traverse with double pointers Then I thought for a long time and didn't write it out hhh
Refer to the idea of a boss in the comment area :
1、LL All between L;
2、RR All between R;
3、LR unchanged ,RL In the middle ;
4、 There is one on the far left by default L, There is one on the far right by default R, It's easy to handle
This idea is much clearer in an instant
public static String pushDominoes(String dominoes) {
//1、LL All between L;2、RR All between R;3、LR unchanged ,RL In the middle ;5、 There is one on the far left by default L, There is one on the far right by default R, It's easy to handle
dominoes = new StringBuilder("L").append(dominoes).append("R").toString();
StringBuilder ans = new StringBuilder();
// Initialize left and right pointers
int leftIndex = 0, rightIndex = 1;
while (rightIndex < dominoes.length()) {
while (rightIndex < dominoes.length() && dominoes.charAt(rightIndex) == '.') {
rightIndex++;
}
char leftChar = dominoes.charAt(leftIndex);
char rightChar = dominoes.charAt(rightIndex);
if (leftChar == rightChar) {
for (int i = leftIndex+1 ; i < rightIndex; i++) {
ans.append(leftChar);
}
} else if (leftChar == 'R' && rightChar == 'L') {
// here as Calculate how many... Between the two .
int as = rightIndex - leftIndex - 1;
for (int i = 0; i < as / 2 ; i++) {
ans.append('R');
}
// If at present '.' If the number of is odd We also need to add a middle vertical dominoes namely R...L -> RR.LL
if (as % 2 == 1) {
ans.append('.');
}
for (int i = 0; i < as / 2; i++) {
ans.append('L');
}
} else if(leftChar == 'L'&& rightChar == 'R'){
for (int i = leftIndex + 1; i < rightIndex; i++) {
ans.append('.');
}
}
// Take this time rightIndex Point to the char Add to ans in
ans.append(rightChar);
// Update the position of the left and right pointers , Make it the next pair LR/RL/LL/RR Move
leftIndex = rightIndex;
rightIndex++;
}
// Remove the one added on the far right R
return ans.substring(0,ans.length() - 1).toString();
}
See that many bosses in the comment area use bitwise operators , However, I don't seem to have used it … Make up for it 
notes
| The operator | Explain ( Arithmetic shift left and arithmetic shift right are mainly used to multiply signed numbers 、 halve ) |
|---|---|
| number << i | amount to number * 2i , Move arithmetic left AKA Logic shift left , High overflow , Low complement 0 |
| number >> i | amount to number / 2i , Count right , Low overflow , High compensation 0 |
| number >>> i | To move right without a sign , It's also called logical shift right , If the number is positive , Then the high position will be supplemented 0, And if the number is negative , After moving right, the high position will be filled as well 0 |
example 


Picture source
边栏推荐
- Uniapp "no mobile phone or simulator detected, please try again later" and uniapp custom components and communication
- Use of pclint in vs2013
- Learning about tensorflow (I)
- The fourth week of summer vacation
- Learning about opencv (3)
- Netease cloud UI imitation -- & gt; sidebar
- 30 minutes to thoroughly understand the synchronized lock upgrade process
- Common errors when starting projects in uniapp ---appid
- Okaleido ecological core equity Oka, all in fusion mining mode
- Using undertow, Nacos offline logout delay after service stop
猜你喜欢

30 minutes to thoroughly understand the synchronized lock upgrade process

Beginner of flask framework-04-flask blueprint and code separation

About automatic operation on Web pages

Wechat applet learning notes 1

【Halcon视觉】软件编程思路

What will the new Fuzhou Xiamen railway bring to Fujian coastal areas?

Study on the basis of opencv

Meeting OA project (III) -- my meeting (meeting seating and submission for approval)

Principle analysis and source code interpretation of service discovery

Learning about opencv (2)
随机推荐
Wechat applet development
Study on the basis of opencv
Wechat applet learning notes 1
El table implements adding / deleting rows, and a parameter changes accordingly
Study notes of the second week of sophomore year
Some descriptions of DS V2 push down in spark
Production of a-modal drag function in antui
Getting started with SQL - combined tables
Uniapp "no mobile phone or simulator detected, please try again later" and uniapp custom components and communication
Use spiel expressions in custom annotations to dynamically obtain method parameters or execute methods
Leetcode 504. 七进制数
In Net 6.0
Cause: couldn‘t make a guess for 解决方法
Distributed network communication framework: how to publish local services into RPC services
The use of MySQL in nodejs
Android greendao数据库的使用
Necessary for beginners: debug breakpoint debugging skills in idea and common breakpoint skills
【C#语言】LINQ概述
Jpg to EPS
简单化构造函数的继承方法(一)- 组合继承