当前位置: 首页 > 后端技术 > Python

Golang语言【6】递增三元子序列-傻阶乘-矩阵搜索-直方图的水量|Go主题月

时间:2023-03-25 21:51:09 Python

递增三元子序列给你一个整数数组nums,判断这个数组中是否有一个递增子序列3.如果存在这样的三重下标(i,j,k)使得i2.更新最小值和次小值
2.否则满足条件
//引入数学库import("math")funcincreasingTriplet(nums[]int)bool{//记录最小值和次小值m1,m2:=math.MaxInt32,math.MaxInt32for_,v:=rangenums{//找到子序列的第一个元素并不断更新ifm1>=v{m1=v}elseifm2>=v{//找到子序列的第二个元素m2=v}else{//找到第三个,满足条件返回true}}returnfalse}Javascript/***@param{number[]}nums*@return{boolean}*/varincreasingTriplet=function(nums){letmin=nums[0],temp=Number.MAX_VALUE//最小值,中值for(leti=1;imin){temp=nums[i]}//找到第三个值if(temp0{iftarget==matrix[i][n-1]{returntrue}elseiftarget=0&&ytarget){x--}else{y++}}returnfalse};javascript二分法/***@param{number[][]}matrix*@param{number}target*@return{boolean}*/varsearchMatrix=function(matrix,target){letm=matrix.length,n=matrix[0].lengthletlow=0,high=复制代码m*n-1while(low<=high){letmid=Math.floor((high-low)/2)+low//中位letx=matrix[Math.floor(mid/n)][mid%n]//所处的价值if(xtarget){high=mid-1}else{returntrue}}returnfalse};Typescript以上两种也可以改为tsfunctionsearchMatrix(matrix:number[][],target:number):boolean{letx:number=matrix.length-1,y:number=0while(x>=0&&ytarget){x--}else{y++}}返回false};python暴力破解类Solution(object):defsearchMatrix(self,matrix,target):foriinrange(len(matrix)):forjinrange(len(matrix[0])):ifmatrix[i][j]==target:returnTruereturnFalsepythonanyfunctionany()函数用来判断给定的可迭代参数iterable是否全部为False,则返回False,如果有一个为True,则返回除0以外的True元素,empty和FALSE都被认为是TRUE。语法defany(iterable):forelementiniterable:ifelement:returnTruereturnFalseSolutionclassSolution(object):defsearchMatrix(self,matrix,target):returnany(targetinrowforrowinmatrix)通常愚蠢的阶乘,正整数n的阶乘是所有小于或等于n的正整数的乘积。例如,阶乘(10)=1098765432*1。相反,我们设计了一个笨拙笨拙的阶乘:在整数递减序列中,我们将原来的乘法运算符替换为固定顺序的运算符序列:乘法(*)、除法(/)、加法(+)和减法(-)).例如,clumsy(10)=109/8+7-65/4+3-2*1。但是,这些运算仍然使用通常的算术运算顺序:我们在任何加法和运算之前执行所有乘法和除法步骤减法步骤,乘法和除法步骤从左到右处理。另外,我们使用的除法是底除法,所以10*9/8等于11。这保证了结果是一个整数。实现上面定义的哑函数:给定一个整数N,它返回N的哑阶乘。示例1:输入:4输出:7解释:7=4*3/2+1示例2:输入:10输出:12解释:12=10*9/8+7-6*5/4+3-2*1提示:1<=N<=10000-2^31<=answer<=2^31-1(答案有保证符合32位整数。GOLangfuncclumsy(Nint)int{ifN==1{return1}elseifN==2{return2}elseifN==3{return6}elseifN==4{return7}ifN%4==0{returnN+1}elseifN%4<=2{returnN+2}else{returnN-1}}javascript/***@param{number}N*@return{number}*/varclumsy=function(N){if(N===1){return1}elseif(N===2){return2}elseif(N===3){return6}elseif(N===4){return7}if(N%4===0){returnN+1}elseif(N%4<=2){returnN+2}else{returnN-1}};pythonclassSolution(object):defclumsy(self,N):""":typeN:int:rtype:int"""ifN==1:return1elifN==2:return2elifN==3:返回6elifN==4:返回7ifN%4==0:returnN+1elifN%4<=2:returnN+2else:returnN-1typescriptfunctionclumsy(N:number):number{if(N===1){return1}elseif(N===2){return2}elseif(N===3){return6}elseif(N===4){return7}if(N%4===0){返回N+1}elseif(N%4<=2){returnN+2}else{returnN-1}};17.21.直方图水量难点:【难点】给定一个直方图(也叫柱状图),假设有人从上面连续倒水,这个直方图到底能存多少水?直方图的宽度为1,上面由数组组成[0,1,0,2,1,0,1,3,2,1,2,1]表示直方图,本例为6个单位可以连接水(蓝色部分代表水)。感谢Marcos提供此图。例子:输入:[0,1,0,2,1,0,1,3,2,1,2,1]输出:6[思路]动态规划1.记录每个元素的高度,从左到右扫描并记录右边的最大高度;
2.记录每个元素的高度,从右向左扫描,记录右边的最大高度;
3.比较左右位置元素,取最小的元素,减去数组当前元素的高度。
从左往右扫描,记录右边最大高度从右往左扫描,记录右边最大高度取最小高度gofunctrap(height[]int)int{n:=len(height)ifn==0{return0}//记录左边每个元素的最大高度leftMax:=make([]int,n)leftMax[0]=height[0]fori:=1;我=0;i--{rightMax[i]=max(rightMax[i+1],height[i])}fmt.Println(leftMax,rightMax)ret:=0对于j:=0;j0{returna}returnb}funcmin(a,bint)int{ifa-b>0{returnb}returna}Javascriptvartrap=function(height){letlen=height.lengthif(len===0)return0//记录左边每个矩形的最大高度letleft=Array(len).fill(0)left[0]=height[0]for(leti=1;i=0;--i){right[i]=Math.max(right[i+1],height[i])}//记录结果letret=0for(leti=0;i=0;--i){right[i]=Math.max(right[i+1],height[i]);}//记录结果varret=0;对于(vari=0;i