标签:perm details 2-2 leetcode def end 否则 sum track
#!/usr/bin/env python # coding: utf-8 code1: # 解析逆波兰式子 # 通过100% def evalRPN(tokens): t,f = tokens.pop(),evalRPN if t in ‘+-*/‘: b,a = f(tokens),f(tokens) t = eval("a" + t + "b") return int(t) if __name__ == "__main__": tokens = input() tokens = list([n for n in tokens.split()]) # tokens = ["2","1","+","3","*"] print(evalRPN(tokens)) code2: # 最高的山峰 # 通过78% def longestMountain(N,A): res = 0 i = 0 while i <N: j = i #找到上升的终点,即山顶 while j+1<len(A) and A[j]<A[j+1]: j += 1 mid = j #找到下降的终点 while j+1<len(A) and A[j]>A[j+1]: j += 1 #判断是否构成山脉 if i <mid<j: res = max(res,j-i+1) #若为平山,i后移,否则下降的终点作为起点 if i == j: i += 1 else: i = j return res if __name__ == "__main__": N = int(input()) A = input() A = list([int(n) for n in A.split()]) # N = 7 # A = [9,2,5,8,4,1,7] print(longestMountain(N,A)) code3 # 谈学姐与投资组合 # f(i) = 0, i<0 # f(i) = f(0), i=0 # f(i) = a*f(i-1)+b*f(i-2)+c*f(i-3)+2*i**2-i+32767 # 输入n,a,b,c,f(0) # 输出:f(n) # 类似斐波那契数列,只通过了10% def moneySum( n,a,b,c,f0): if n<0: return 0 if n == 0: return f0 if n == 1: f1 = a*f0+32767+2-2 return f1 if n == 2: f1 = a*f0+32767+2-2 f2 = a*f1+b*f0+32767+2*n**2-n return f2 return a*moneySum(n-1,a,b,c,f0)+b*moneySum(n-2,a,b,c,f0)+c*moneySum(n-3,a,b,c,f0)+2*n**2-n+32767 if __name__ == "__main__": array = input() array = list([int(n) for n in array.split()]) n,a,b,c,f0 = array[0],array[1],array[2],array[3],array[4] # n,a,b,c,f0 = [10,0,0,0,100] print(moneySum(n,a,b,c,f0)) code4 #完全平方数组的数目:类似LeetCode_996_NumberOfSquarefulArrays #https://blog.csdn.net/qq_17550379/article/details/87687106 #下列代码通过了87% def numSquarefulPerms(N,A): nums = A nums.sort() res = [] def backtrack(nums,tmp): if not nums: res.append(tmp) return None # print("len(nums)",len(nums)) for i in range(len(nums)): #去重 if i and nums[i] == nums[i-1]: continue #剪枝 if not tmp or ((tmp[-1]+nums[i])**0.5)%1 == 0: backtrack(nums[:i]+nums[i+1:],tmp+[nums[i]]) backtrack(nums,[]) return len(res) if __name__ == "__main__": # N = int(input()) # A = input() # A = list([int(n) for n in A.split()]) N = 3 A = [3,10,6] print(numSquarefulPerms(N,A))
标签:perm details 2-2 leetcode def end 否则 sum track
原文地址:https://www.cnblogs.com/ivyharding/p/11380070.html