• Welcome to the world's largest Chinese hacker forum

    Welcome to the world's largest Chinese hacker forum, our forum registration is open! You can now register for technical communication with us, this is a free and open to the world of the BBS, we founded the purpose for the study of network security, please don't release business of black/grey, or on the BBS posts, to seek help hacker if violations, we will permanently frozen your IP and account, thank you for your cooperation. Hacker attack and defense cracking or network Security

    business please click here: Creation Security  From CNHACKTEAM

Recommended Posts

T1 6047. 移除指定数字得到的最大结果

给你一个表示某个正整数的字符串数字和一个字符数字5 .

从数字中恰好移除一个等于手指的字符后,找出并返回按十进制表示最大的结果字符串。生成的测试用例满足手指在数字中出现至少一次。

思路

将字符串转化列表去除指定的元素再转换

直接截取字符串的子串s substr(0,I)s substr(I)(C)

使用max_element()返回的是序列最大值的地址,需要解引用* 获取值;

代码

计算机编程语言

类别解决方案:

def removeDigit(self,number: str,digit: str) - str:

# 移除指定元素

n=len(数字)

max=-1000000000

对于范围(n):内的我

s=' '

如果数字==digit:

李=列表(数字)

李。波普(一)

因为我在li:

s=" .加入(一)

如果整数最大值:

max=int(s)

打印(最大)

返回字符串(最大值)

C

类别解决方案{

公共:

string removeDigit(字符串数字,字符数字){

int n=数字。size();

向量串s;

for(int I=0;在;我)

{

如果(数字[我]==数字)

{

string a=number.substr(0,I)number。substr(i1);

s。push _ back(a);

}

}

int l=s . size();

for(int I=0;il;我)

{

couts'-';

}

return *max_element(s.begin()、s . end());//返回的是地址*解引用

}

};

T2

暴力不过

类别解决方案:

def minimumCardPickup(self,cards: List[int]) - int:

min_=1000000000000

适用于范围内的我(信用卡):

对于范围内的j(I ^ 1,len(卡)):

如果牌[我]==牌:

项目=(j-11)

if temmin_:

min_=tem

if min _==1000000000000:

min_=-1

返回最小值_

超时

类别解决方案:

def minimumCardPickup(self,cards: List[int]) - int:

n=len(卡片)

min_=100000000

rec=[]

对于范围(n):内的我

idx=[x for x,y in enumerate(cards)if y==cards]

如果len(idx)=2:

建议追加(idx)

对于rec:中的x

x.sort()

对于范围(len(x)-1,0,-1):内的j

tem=x[j]-x[j-1] 1

if temmin_:

min_=tem

if min _==100000000:

min_=-1

返回最小值_

使用无序字典

类别解决方案{

公共:

int minimumCardPickup(矢量卡){

int res=1e6

int len=卡片。size();

unordered_mapint,int a;

for(int I=0;终场我)

{

if (a.count(cards)0)

{

res=min(res,I-a[cards]1);

}

a[cards]=I;

}

for(int I=0;内务部。size();我)

couta”;

if (reslen)返回-1;

返回表示留数

}

};

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now