914. Flip Game

https://www.cnblogs.com/grandyang/p/5224896.html

从前到后遍历,遇到连续两个'+',就将两个加号变成'-'组成新的字符串加入到结果中。

class Solution {
public:
vector<string> generatePossibleNextMoves(string &s) {
// write your code here
vector<string> result;
for(int i = ;i < s.size();i++){
if(s[i] == '+' && s[i-] == '+')
result.push_back(s.substr(,i-) + "--" + s.substr(i+,s.size() - i - ));
}
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 = ; i < s.size(); ++i) {
if (s[i] == '+' && s[i - ] == '+' && !canWin(s.substr(, i - ) + "--" + s.substr(i + ))) {
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 here
for(int i = ;i < s.size();i++){
if(s[i] == '+' && s[i-] == '+'){
string tmp = s.substr(,i-) + "--" + s.substr(i+);
if(!canWin(tmp))
return true;
}
}
return false;
}
};

leetcode 293.Flip Game(lintcode 914) 、294.Flip Game II(lintcode 913)的更多相关文章

  1. leetcode 198. House Robber 、 213. House Robber II 、337. House Robber III 、256. Paint House(lintcode 515) 、265. Paint House II(lintcode 516) 、276. Paint Fence(lintcode 514)

    House Robber:不能相邻,求能获得的最大值 House Robber II:不能相邻且第一个和最后一个不能同时取,求能获得的最大值 House Robber III:二叉树下的不能相邻,求能 ...

  2. [LeetCode] 293. Flip Game 翻转游戏

    You are playing the following Flip Game with your friend: Given a string that contains only these tw ...

  3. [LeetCode] 294. Flip Game II 翻转游戏 II

    You are playing the following Flip Game with your friend: Given a string that contains only these tw ...

  4. leetcode 79. Word Search 、212. Word Search II

    https://www.cnblogs.com/grandyang/p/4332313.html 在一个矩阵中能不能找到string的一条路径 这个题使用的是dfs.但这个题与number of is ...

  5. Leetcode之动态规划(DP)专题-63. 不同路径 II(Unique Paths II)

    Leetcode之动态规划(DP)专题-63. 不同路径 II(Unique Paths II) 初级题目:Leetcode之动态规划(DP)专题-62. 不同路径(Unique Paths) 一个机 ...

  6. Leetcode之动态规划(DP)专题-264. 丑数 II(Ugly Number II)

    Leetcode之动态规划(DP)专题-264. 丑数 II(Ugly Number II) 编写一个程序,找出第 n 个丑数. 丑数就是只包含质因数 2, 3, 5 的正整数. 示例: 输入: n ...

  7. 记录我的 python 学习历程-Day13 匿名函数、内置函数 II、闭包

    一.匿名函数 以后面试或者工作中经常用匿名函数 lambda,也叫一句话函数. 课上练习: # 正常函数: def func(a, b): return a + b print(func(4, 6)) ...

  8. 【LeetCode】117. Populating Next Right Pointers in Each Node II 解题报告(Python)

    [LeetCode]117. Populating Next Right Pointers in Each Node II 解题报告(Python) 标签: LeetCode 题目地址:https:/ ...

  9. 【算法训练营day4】LeetCode24. 两两交换链表中的结点 LeetCode19. 删除链表的倒数第N个结点 LeetCode面试题 02.07. 链表相交 LeetCode142. 环形链表II

    [算法训练营day4]LeetCode24. 两两交换链表中的结点 LeetCode19. 删除链表的倒数第N个结点 LeetCode面试题 02.07. 链表相交 LeetCode142. 环形链表 ...

随机推荐

  1. ET·ci — 全自动软件测试调度(持续集成)平台

            ET·ci 提供了编译-测试-发布解决方案,包括:自动提取配置库代码进行自动构建, 自动调度静态测试工具(如QAC)进行静态测试,自动调度单元测试工具(如Tessy)开展动态测试,自动 ...

  2. 0030redis主从复制以及哨兵模式的搭建

    ------------------------------redis主从备份以及哨兵模式------------------------------------------------------- ...

  3. 《你又怎么了我错了行了吧》【Beta】Scrum meeting 3

    第三天 日期:2019/6/26 前言: 第3次会议在女生宿舍召开(前一天晚上开的) 项目全部基本测试完成,解决了多处bug,明天终于可以拿去演示了.... 1.1 今日完成任务情况.成员贡献时间及工 ...

  4. sitemap怎么制作才适合蜘蛛抓取?

    网站sitemap制作格式与要求 1.sitemap格式说明 <?xml version="1.0" encoding="utf-8"?> < ...

  5. 关于axios请求携带cookie以及封装

    axios跨域携带cookie需要配置 axios跨域发送请求的时候默认不会带上cookie的 + withCredentials的情况下,后端要设置Access-Control-Allow-Orig ...

  6. 关于List集合中元素排序问题

    问题描述: 有一个list集合,其中元素是Student对象,根据student的age排序. Student对象 /** * description * * @author 70KG * @date ...

  7. 谈谈javascript中的prototype与继承

    谈谈javascript中的prototype与继承 今天想谈谈javascript中的prototype. 通常来说,javascript中的对象就是一个指向prototype的指针和一个自身的属性 ...

  8. 题解 UVa10780

    题目大意 多组数据,每组数据给定两个整数 \(m,n\),输出使 \(n\%m^k=0\) 的最大的 \(k\).如果 \(k=0\) 则输出Impossible to divide. 分析 计数水题 ...

  9. [GCP] Goolge compute Engine

    Which of the following is a PAAS option for hosting web apps on GCP? App Engine standard or flexible ...

  10. CF827D Best Edge Weight 题解

    题意: 给定一个点数为 n,边数为 m,权值不超过 \(10^9\) 的带权连通图,没有自环与重边. 现在要求对于每一条边求出,这条边的边权最大为多少时,它还能出现在所有可能的最小生成树上,如果对于任 ...