"""
问题:
两个不重复的数组1和数组2,其中数组1是数组2的子集,再数组2中相应位置找到
数组1所有元素的下一个更大数字
数组1中的数字x的下一个更大数字是数组2中的x右边第一个更大数字,如果不存在,则返回
数字为-1,数组1和数组2中的数字都是唯一的,数组1和2长度不超过1000
"""
out =[]classPython006:defNextBig(self, list1, list2):global out
for i in list1:for j in list2:if i == j:
c = list2.index(j)for k in list2[c:]:if k > j:
out.append(k)breakelse:
out.append(-1)breakelse:
out.append(-1)
ssc = Python006()
ssc.NextBig([1,5,6,7],[2,1,5,9,8,6,7])print(out)
字符串中的单词数
"""
问题:计算字符串中的单词数,其中一个单词定义为不含空格的连续字符串。
描述:输入“hello my name is john”
"""classPython007:defWorldNum(self,a):
b =0for i inrange(len(a)-1):if(a[i]!=' ')&(a[i+1]==' '):
b +=1print(b)
ssc = Python007()
ssc.WorldNum(' a b c ad sa adk a s ')
勒索信
"""
问题:
给定一个表示勒索信内容的字符串和另外一个表示杂志内容的字符串,
写一个方法判断能否通过减下杂志内容构造出这封勒索信,若可以,返回True,否则返回False
注:杂志字符串中的每一个字符仅能再勒索心中使用一次
"""classPython008:defExtort(self, extort, magazine):
a =[]
b =[]
a.extend(extort)print(a)
b.extend(magazine)print(b)for i in a[0:]:for j in b[0:]:if j == i:
b.remove(j)
a.remove(i)breakif a ==[]:print("True")else:print("False")
ssc = Python008()
ssc.Extort('aa','aba')
不重复的两个数
"""
问题:给定一个数组,其中除了2个数,其他均出现两次,请找到不重复两次的2个数并返回
"""
c =[]classPython009:defNoReplace(self, a):for i in a:
b = a.count(i)if b !=2:
c.append(i)print(c)
ssc = Python009()
ssc.NoReplace([1,3,2,2,5,5,6])
双胞胎字符串
"""
问题:
给定俩个字符串s和t,每次可以任意交换位置s的奇数位和偶数位上的字符,即奇数位
上的字符能与其他奇数位的字符交换,偶数位上的字符也能与其他偶数位上的字符交换,问
能否经过若干次减缓,使s变成t
"""classPython010:defTwoStr(self, a, b):
c =[]
d =[]for i inrange(len(a)):if i %2==0:
c.append(a[i])
c.append(b[i])else:
d.append(a[i])
d.append(b[i])for i inrange(len(c)):if c.count(c[i])!=2:print('No')breakelse:for i inrange(len(d)):if d.count(d[i])!=2:print('No')breakelse:print("Yes")
ssc = Python010()
ssc.TwoStr('abcdefg','afedgcb')