zoukankan      html  css  js  c++  java
  • 293. Flip Game只翻转一步的加减号翻转游戏

    [抄题]:

    You are playing the following Flip Game with your friend: Given a string that contains only these two characters: + and -, you and your friend take turns to flip two consecutive "++" into "--". The game ends when a person can no longer make a move and therefore the other person will be 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 [].

     [暴力解法]:

    时间分析:

    空间分析:

     [优化后]:

    时间分析:

    空间分析:

    [奇葩输出条件]:

    [奇葩corner case]:

    [思维问题]:

    [一句话思路]:

    不要用数组来做,字符串就该用函数

    [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

    [画图]:

    不要用数组来做,字符串就该用函数

    [一刷]:

    1. (i = s.indexOf("++", i+1))) >= 0 表示角标存在性, 括号不坏菜 特别是赋值的时候
      s.substring(i+2)表示切到尾部为止

    [二刷]:

    [三刷]:

    [四刷]:

    [五刷]:

      [五分钟肉眼debug的结果]:

    [总结]:

    string题一般都用函数

    [复杂度]:Time complexity: O(n) Space complexity: O(n)

    [英文数据结构或算法,为什么不用别的数据结构或算法]:

    substring函数:

    [关键模板化代码]:

    [其他解法]:

    [Follow Up]:

    [LC给出的题目变变变]:

    294. Flip Game II 排列组合,用回溯法

     [代码风格] :

    class Solution {
        public List<String> generatePossibleNextMoves(String s) {
            //ini
            List<String> res = new ArrayList<>();
            //cc 
            if (s.length() == 0) {
                return res;
            }
            
            //for loop 
            for (int i = -1; (i = s.indexOf("++", i + 1)) >= 0;) {
                res.add(s.substring(0, i) + "--" + s.substring(i + 2));
            }
            
            //return
            return res;
        }
    }
    View Code
  • 相关阅读:
    Impala 加载Hive的UDF
    在编译器中调试spark程序处理
    转:Kafka 客户端TimeoutException问题之坑
    Apache Kafka系列(五) Kafka Connect及FileConnector示例
    Apache Kafka系列(四) 多线程Consumer方案
    Apache Kafka系列(三) Java API使用
    Apache Kafka系列(二) 命令行工具(CLI)
    ajax异步刷新
    Mybaties下的分页功能的实现
    读取配置文件的方法
  • 原文地址:https://www.cnblogs.com/immiao0319/p/8649777.html
Copyright © 2011-2022 走看看