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
  • 相关阅读:
    黑马程序员_java基础笔记(13)...类加载器和代理
    nyoj-411-Friends number
    nyoj-38-布线问题
    nyoj-233-Sort it
    nyoj-115-城市平乱
    nyoj-608-畅通工程
    nyoj-36-最长公共子序列
    nyoj-150-Train Problem I
    nyoj-494-Dancing With the Googlers
    nyoj-214-单调递增子序列(二)
  • 原文地址:https://www.cnblogs.com/immiao0319/p/8649777.html
Copyright © 2011-2022 走看看