leetcode 293.Flip Game(lintcode 914) 、294.Flip Game II(lintcode 913)
生活随笔
收集整理的這篇文章主要介紹了
leetcode 293.Flip Game(lintcode 914) 、294.Flip Game II(lintcode 913)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
914. Flip Game
https://www.cnblogs.com/grandyang/p/5224896.html
從前到后遍歷,遇到連續兩個'+',就將兩個加號變成'-'組成新的字符串加入到結果中。
class Solution { public:vector<string> generatePossibleNextMoves(string &s) {// write your code herevector<string> result; for(int i = 1;i < s.size();i++){if(s[i] == '+' && s[i-1] == '+')result.push_back(s.substr(0,i-1) + "--" + s.substr(i+1,s.size() - i - 1));}return result;} };?
?913.?Flip Game II
這個題是看先手變換的是否會贏。
方法與Flip Game類似,遍歷字符串,然后遞歸找下一個是否是能修改,將修改后的字符串傳入到遞歸的結果中。
https://www.cnblogs.com/grandyang/p/5226206.html
下面這個應該是leetcode上的格式,沒有使用引用,這個代碼直接貼到lintcode上會報錯。
class Solution { public:bool canWin(string s) {for (int i = 1; i < s.size(); ++i) {if (s[i] == '+' && s[i - 1] == '+' && !canWin(s.substr(0, i - 1) + "--" + s.substr(i + 1))) {return true;}}return false;} };修改后lintcode上的代碼。
class Solution { public:/*** @param s: the given string* @return: if the starting player can guarantee a win*/bool canWin(string &s) {// write your code herefor(int i = 1;i < s.size();i++){if(s[i] == '+' && s[i-1] == '+'){string tmp = s.substr(0,i-1) + "--" + s.substr(i+1);if(!canWin(tmp))return true;}}return false;} };?
轉載于:https://www.cnblogs.com/ymjyqsx/p/10841419.html
總結
以上是生活随笔為你收集整理的leetcode 293.Flip Game(lintcode 914) 、294.Flip Game II(lintcode 913)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: composer 更新版本
- 下一篇: phpstrom php出现404