classSolution: defwiggleMaxLength(self, nums: List[int]) -> int: # c用来记录前一个差值是下降/上升的,默认0 n, c, res = len(nums), 0, 1 if n < 2: return n for i in range(1, n): x = nums[i] - nums[i - 1] # 如果有差值才继续处理 if x: # <0代表有上升下降的交替,=0是初始情况的判断 if x * c <= 0: res += 1 c = x return res
col=['grade','subGrade','employmentTitle','homeOwnership','verificationStatus','purpose','postCode','regionCode', 'initialListStatus','applicationType','policyCode'] for i in data_x.columns: if i in col: data_x[i] = data_x[i].astype('str') for i in testA.columns: if i in col: testA[i] = testA[i].astype('str') model=CatBoostClassifier( loss_function="Logloss", eval_metric="AUC", task_type="CPU", learning_rate=0.1, iterations=500, random_seed=2020, od_type="Iter", depth=7)
计算机处理的输入常常不是几十个或几千个,而是几百万个的时候,时间复杂度为O ( n 2 ) O(n^2)O(n2)和O ( e n ) O(e^n)O(e**n)的算法,所需的运行次数简直是天壤之别,O ( e n ) O(e^n)O(e**n)指数级的可能运行好几天都没法完成任务,所以才要研究一个问题是否存在多项式时间算法。
deffindMaxConsecutiveOnes(self, nums: List[int]) -> int: res=0 count=0 for i in range(len(nums)): if nums[i]: count+=1 #res=max(res,count) else: count=0 res=max(res,count) #return res retuen max(res,count)
classSolution(object): deffindMaxConsecutiveOnes(self, nums): count =0 start = 0 for i in range(len(nums)): if nums[i]==1: start =i break curstart = start maxcount = 0 count =0 for i in range(start, len(nums)): if nums[i]==1: count+=1 if count>maxcount: maxcount = count else: curstart=i+1 count = 0 return maxcount
defsortColors(self, nums: List[int]) -> None: defswap(nums,i,j): temp=nums[i] nums[i]=nums[j] nums[j]=temp r1,r2=-1,-1 for i in range (len(nums)): if nums[i]<2: r2+=1 swap(nums,i,r2) if nums[r2]<1: r1+=1 swap(nums,r1,r2)
思路
1 2
[9,3,15,20,7] [9,15,7,20,3]
1.用L表示左子树,P代表根,R代表右子树
2.中序序列可表示为 LPR, 后序序列可表示为 LRP
3.后序序列的最后一个元素是二叉树的根,如3
4.根据根的值可以把中序序列分为两部分[9]和[3,15,20, 7]
5.中序两部分元素个数分别为1,4
6.根据中序元素个数把后续分为两部分[9]和[15, 7, 20, 3]
7.用中序和后序对应的部分分别创建树的左子树和右子树
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
classSolution: defbuildTree(self, inorder: List[int], postorder: List[int]) -> TreeNode: defhelper(il, ir, pl, pr): if (il == ir or pl == pr): returnNone root = TreeNode(postorder[pr-1]) inRootPos = inorder.index(root.val, il, ir) rightSize = ir - inRootPos root.right = helper(inRootPos + 1, ir, pr - rightSize, pr - 1) root.left = helper(il, inRootPos, pl, pr - rightSize) return root return helper(0, len(inorder), 0, len(postorder))