[Leetcode] Flip Game Flipping games

Source: Internet
Author: User

You is playing the following Flip Game with your Friend:given a string this contains only these both characters: an + D - , you and your friend take turns to the flip twoconsecutive into "++" "--" . The game ends when a person can no longer make a move and therefore the other person would be the the winner.

Write a function to compute all possible states of the string after one valid move.

For example, given s = "++++" , after one move, it may become one of the following states:

[  "--++",  "+--+",  "++--"]

If There is no valid move, return an empty list [] .

This question let us turn the adjacent two + + to become--, is really not a problem, we start from the second letter to traverse, each time to determine whether the current letter is +, and whether the previous letter is +, if all add, then the inverted string will be stored in the result can be, see the code is as follows:

classSolution { Public: Vector<string> generatepossiblenextmoves (strings) {vector<string>Res;  for(inti =1; I < s.size (); ++i) {if(S[i] = ='+'&& S[i-1] =='+') {res.push_back (S.substr (0I1) +"--"+ S.SUBSTR (i +1)); }        }        returnRes; }};

Similar topics:

Leetcode all in one topic summary (continuous update ...)

[Leetcode] Flip Game Flipping games

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.