华为笔试21年4月17日1 .反转括号内字符串stringstr=" ((jhkv ) ) love ) I ) "; 变成ilovejhkv
stringstr=「(u ) s ) love )”ilovesu
从大括号到外括号依次反转连接
把想法递归化
定义:反转inverse(str,l,r ) str中从l到r不包含括号的字符串
如果
package com.company; publicclassfanzuan { publicstaticvoidmain (字符串[ ] args )/stringstr=' () jhkv ) (love ) I ) ); stringstr='(u ) s ) ) love ) I ); system.out.println(inverse(str,0,str.length ) (-1 ) ); }私有状态转换(stringstr,int l,int r ) ) if(r-L=1)返回' ); stringbuilder sb=new stringbuilder (; for(intI=L1; ir; I ) charc=str.charat(I; if(c!=' () sb.append(c ) c; else { int j; for(j=I1; ir; j () if(str.Charat(j )==' ) break; }sb.append(inverse(str,I,j ); i=j; } } return sb.reverse ().toString ); }减速# include iostream # include vector # include algorithm # includefunctionalusingnamespacestd; int main () { int N; cin N; 向量(n; int temp; for(intI=0; i N; I ) { cin temp; ve[i]=temp; }矢量int ans; ans.push_back(ve[0]; int i=1,lastspeed=ve[0],count=1,ABEfalg=0,ABEedge=0; while(in ) if ) lastspeed-ve )=9) { ABEfalg; }else{if(Abefalg=4) intL=max ) Abeedge,i - ABEfalg-4 ); intr=min(n-1,i 3); for(int满意的花生=l; 满意的花生=r; 满意花生(ans.push_back(ve[满意花生] ); ABEedge=i 4; 出局=-3; } ABEfalg=0; }if(count==60Abefalg4) ans.push_back ) ve[I]; 计数=0; } lastspeed=ve[i]; I; 出局; (if ) Abefalg=4) intL=max ) Abeedge,i - ABEfalg - 4 ); intr=min(n-1,i 3); for(int满意的花生=l; 满意的花生=r; 满意花生(ans.push_back(ve[满意花生] ); (} int numcc=ans.size ) (- 1; for(intLuo=0; luonumcc; luo ) cout ans[luo] ','; cout ans[numcc] endl; 返回0; } 3.最低的基站数4
1 3 1 1
第一个基站最多可达1 1=2
第二个基站最多可达2 3=5
最短1-2-4最低2基站1 2
1-2-3-4请给我三个基站
在两个指针中,I指针指向区间范围的最左边,r指针指向区间范围的最右边
当r指向最后一个基站n-1时结束
package com.company; 公共类jizhan { publicstaticvoidmain (字符串[ ] args )/int ) ) a={ 2,5,1,1,1 }; int [ ] a={ 1,3,1,1,2,1,3,1,1 }; 系统. out.println (least count (a ) ); }私有状态转换计数(int [ ] a ) { int ans=1,r=a[0]; int lenp=0; for(intI=0; i a.length; (if ) r=a.Length-1 ) break; for(intj=I1; j=r; j () if ) ja[j]lenpa[lenp] ) lenp=j; (I=r; r=lenp a[lenp]; ans; }返回Ans; }