AC:0/3,score=0.8+0+0.9=1.7
卧槽这青铜场把我炸了,愣是没做出来,只做了1,3,第二题都没看,炸了。
第一题:
题目:
答案:
class Solution:
def solve(self , n , k ):
# write code here
k=k%n
if n==1:return 0
if n==2:
if k==1 : return 1
if k==0 : return 0
if k==0: return 0
if k==1 or k==n-1 or k==2 or k==n-2:return 2
else: return 3
思路:
漏解了,漏了k==2这里,万万没有想到移动两格可以这么翻转。
这类题,只要好好的去拿实例做一下,都能有思路。
第二题:
题目:
答案:
class Solution:
def solve(self , n , m , a ):
# write code here
l,r,num=0,0,0
ans=0
while r<n:
num+=1-a[r]
while num>m:
num-=1-a[l]
l+=1
ans=max(ans,r-l+1)
r+=1
return ans
思路:
第三题:
题目:
答案:
这题所有的python 都只能过91,
(2020.08.08更新)后来有大佬指出了python默认深度搜索不够,所以要加个system,太坑了。
import sys
sys.setrecursionlimit(100005)
class Solution:
def solve(self , n , pre , suf ):
# write code here
def dfs(pre,suf):
if not pre:return []
if len(pre)==1:return [pre[0]]
root=[pre[0]]
index=suf.index(pre[1])
left=dfs(pre[1:2+index],suf[:index+1])
right=dfs(pre[2+index:],suf[index+1:-1])
return left+root+right
ans= dfs(pre,suf)
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上面是能通过的。
注意,前序和后序构造中序,答案是不唯一的,但是题目中限制了条件,就是唯一的了,难道是这里出错了,也不像。就这样吧,难。