This is the 17th day of my participation in the August Challenge

1957. Deleting characters makes strings better

Thought analysis

If the question asks us to return an int, the number of deleted strings, then we need to delete a character if the consecutive character exceeds 3.

 if(s[i] == s[i - 1]){
    count ++;
    if (count == 3){ count--; }}Copy the code

But since the problem asks us to return a string, we can store the answer in another string, a simple method that assigns a character to the end of the string

string tmp = "A";
tmp[0] = b[i];
r += tmp;
Copy the code

1958. Check that the operation is legal

Thought analysis

If the return relationship between a good line segment and a bad line segment is either negative or negative, then the problem is somewhat difficult (in fact, it is not big). Now we only need to find the “good line segment” that meets the condition of a good line segment from the eight directions. So how to judge whether it is a good line segment? It only needs to enumerate 8 directions and verify whether there is a good line segment starting from this point for each direction. If all the lattice points (at least one) between the point and the next lattice point of the same color in the corresponding direction are of another color, they form a good line segment.

If the starting point is determined, if the surrounding points are of different colors, then you can follow the direction. Before you encounter a blank cell or boundary, if you encounter a point of the same color as the starting point, then you can say that this is a good line segment and return true