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. VBS 数组冒泡排序

    '定义变量 Dim arr1 '数组赋值 arr1 = array(, , , , , , ,) To UBound(arr1) To UBound(arr1) k=arr1(m) If arr1(m ...

  2. 样条函数后续(java)--可在hive中执行的函数

    之前写的样条插值算法只能在本地执行,但是我想要的是可在hive中执行的jar包,为了符合我的要求,经过痛苦.气愤.悲伤等一系列过程,终于实现了: 想要实现可在hive中执行的jar包,以下是具体步骤: ...

  3. linux中service模板

    [Unit] Description=描述 After=syslog.target network.target remote-fs.target nss-lookup.target [Service ...

  4. 文件操作2-Day3

    一.文件操作流程 打开文件,得到文件句柄并赋值给一个变量 通过句柄对文件进行操作 关闭文件 二.文件打开模式 1.普通打开模式 r:只读模式(不加参数则默认只读:不能写,不能追加) w:只写模式(只能 ...

  5. c# 自动将string字符串转成实体属性的类型

    Convert.ChangeType() 看到.net webapi中有[FromUri]来接收参数  可以将自动参数转换成字段属性的类型 baidu 了许多文章 都在自己造轮子  突然发下微软提供了 ...

  6. Linux LVM--三种Logic Volume

    本文链接:https://blog.csdn.net/u012299594/article/details/84551722 概述 为了满足在性能和冗余等方面的需求,LVM支持了下面三种Logic V ...

  7. MongoDB bindIp 与 bindIpAll

    Linux服务器上安装MongoDB后,允许其它客户端IP访问MongoDB服务器.阿里云服务器需要设置入站规则,可参见使用外网访问阿里云服务器ZooKeeper 1.允许所有地址访问 NOTE:ne ...

  8. learning java 访问文件和目录

    import java.io.File; import java.io.IOException; public class FileTest { public static void main(Str ...

  9. 案例:3D切割轮播图

    一.3d转换 3D旋转套路:顺着轴的正方向看,顺时针旋转是负角度,逆时针旋转是正角度 二.代码 <!DOCTYPE html> <html lang="en"&g ...

  10. 交互设计算法基础(1) - Binary Search

    int binary_search(int[] list, int item) { int low = 0; int high = list.length-1; while (low <= hi ...