AC:0/3,score=0.8+0+0.9=1.7

卧槽这青铜场把我炸了,愣是没做出来,只做了1,3,第二题都没看,炸了。

第一题:

题目:

QQ图片20200806224236.png

答案:

  1. class Solution:
  2. def solve(self , n , k ):
  3. # write code here
  4. k=k%n
  5. if n==1:return 0
  6. if n==2:
  7. if k==1 : return 1
  8. if k==0 : return 0
  9. if k==0: return 0
  10. if k==1 or k==n-1 or k==2 or k==n-2:return 2
  11. else: return 3

思路:

漏解了,漏了k==2这里,万万没有想到移动两格可以这么翻转。
这类题,只要好好的去拿实例做一下,都能有思路。

第二题:

题目:

QQ图片20200806224508.png

答案:

  1. class Solution:
  2. def solve(self , n , m , a ):
  3. # write code here
  4. l,r,num=0,0,0
  5. ans=0
  6. while r<n:
  7. num+=1-a[r]
  8. while num>m:
  9. num-=1-a[l]
  10. l+=1
  11. ans=max(ans,r-l+1)
  12. r+=1
  13. return ans

思路:

其实就是最简单的滑动窗口,这题不难,可惜我把它跳过了,草。

第三题:

题目:

QQ图片20200806224508.png

答案:

这题所有的python 都只能过91,
(2020.08.08更新)后来有大佬指出了python默认深度搜索不够,所以要加个system,太坑了。

  1. import sys
  2. sys.setrecursionlimit(100005)
  3. class Solution:
  4. def solve(self , n , pre , suf ):
  5. # write code here
  6. def dfs(pre,suf):
  7. if not pre:return []
  8. if len(pre)==1:return [pre[0]]
  9. root=[pre[0]]
  10. index=suf.index(pre[1])
  11. left=dfs(pre[1:2+index],suf[:index+1])
  12. right=dfs(pre[2+index:],suf[index+1:-1])
  13. return left+root+right
  14. ans= dfs(pre,suf)
  15. return ans

贴一个C++的能过的,备用。

class Solution {
public:
    /**
     * 返回所求中序序列
     * @param n int整型 二叉树节点数量
     * @param pre int整型vector 前序序列
     * @param suf int整型vector 后序序列
     * @return int整型vector
     */
    vector<int> ans;
    void deal(int pl, int pr, vector<int>& pre, int sl, int sr , vector<int>& suf){
        if(pl > pr||sl>sr) return ;
        if(pl == pr) {ans.push_back(pre[pl]);return;}
        int pos = -1;
        for(int i=sl;i<=sr;i++){
            if(suf[i] == pre[pl+1]) pos = i;
        }
        deal(pl+1,pos-sl+pl+1,pre,sl,pos,suf);
        ans.push_back(pre[pl]);
        deal(pos-sl+pl+2,pr,pre,pos+1,sr-1,suf);
    }
    vector<int> solve(int n, vector<int>& pre, vector<int>& suf) {
        // write code here
        deal(0,n-1,pre,0,n-1,suf);
        return ans;
    }
};

思路:

其实这题我会,但是不知道为什么被恰了(后来知道了),leetcode上面是能通过的。
注意,前序和后序构造中序,答案是不唯一的,但是题目中限制了条件,就是唯一的了,难道是这里出错了,也不像。就这样吧,难。