# python(牛客)试题解析1 - 简单

– – – – – – – – – – 分-割-线 – – – – – – – – – – –

```class Solution:
def solve(self , str: str) -> str:
# write code here
list1 = []
for i in str:
list1.append(i)
list1.reverse()
s =""
for i in list1:
s = s+i
return s```

```class Solution:
def solve(self , str: str) -> str:
str1 = str[::-1]
return str1```

```class Solution:
def judge(self , str: str) -> bool:
str1 = str[::-1]
if str1 == str:
return True
else:
return False```

解析2：使用三母表达式简化输出

```class Solution:
def judge(self , str: str) -> bool:
return True if str[::-1]==str[:] else False```

```class Solution:
def gcd(self , a: int, b: int) -> int:
#a = 30
#b = 40
res1 = []
res2 = []
res3 = []
# 因式分解
while a > 1:
for i in range(a - 1):
k = i + 2
if a % k == 0:
res1.append(k)
a = int(a / k)
break
#print(res1)
while b > 1:
for i in range(2, b + 1):
if b % i == 0:
res2.append(i)
b = int(b / i)
break
#print(res2)
for i in range(0, len(res1)):
if res1[i] in res2:
res3.append(res1[i])
res2.remove(res1[i])
res = 1
for i in res3:
res = res * i
#print(res)
return res```

```class Solution:
def gcd(self , a: int, b: int) -> int:
t=0
m=0
n=0
# 辗转相减减法
if a == b:
t = a
else:
m = max(a, b)
n = min(a, b)
t = m - n
while n != t:
m, n = max(n, t), min(n, t)
t = m - n
return t```

```class Solution:
def Fibonacci(self , n: int) -> int:
if n == 1 or n == 2:
return 1
elif n == 3:
return 2
else:
return self.Fibonacci(n-1) + self.Fibonacci(n-2)```

```class Solution:
def Fibonacci(self , n: int) -> int:
a, b = 1, 1
if n <= 1:
return 1
else:
for i in range(2, n):
tmp = a + b
a = b
b = tmp
return b```

k如果大于字符串长度，则输出最大值的怎么所在字符串的位置索引，

AbCdeFG
3

5

```l1 = input()
k = int(input())
if k > len(l1):
k = len(l1)
l2 =sorted(l1)
letter = l2[k-1]
print(l1.index(letter))```

`给定两个整数数组，arr1、arr2，数组元素按升序排列；假设从arr1、arr2中分别取出一个元素，可构成一对元素；现在需要取出k对元素，并对取出的所有元素求和，计算和的最小值；注意：两对元素对应arr1、arr2的下标是相同的，视为同一对元素。描述：输入两行数组arr1、arr2每行首个数字为数组大小size， 0 < size <= 100arr1，arr2中的每个元素e， 0< e <1000接下来一行，正整数k 0 < k <= arr1.size * arr2.size输出描述满足要求的最小值示例：输入3 1 1 23 1 2 32输出4解析：推导式进行求和后从小到大排序，并取得第k小的和值`
`a = list(map(int,input().split()))[1:] b = list(map(int,input().split()))[1:] k = int(input()) sum_ = [x+y for x in a for y in b] sum2 = sorted(sum_) print(sum(sum2[:k]))`

xyxyXX

x:2;y:2;X:2;

abababb

b:4;a:3;

```s = input() d = {} a = [] for i in s: if i in d.keys(): d[i] += 1
else: d[i] = 1 l = sorted(d.items(),key=lambda x:x[1],reverse=True) for i in l: a.append(f"{i[0]}:{i[1]}") print(";".join(a)+";")```

原文作者：Mrwhite86
原文地址: https://www.cnblogs.com/mrwhite2020/p/16905635.html
本文转自网络文章，转载此文章仅为分享知识，如有侵权，请联系博主进行删除。