求助玄关
  • 板块灌水区
  • 楼主ChenZQ
  • 当前回复1
  • 已保存回复1
  • 发布时间2024/8/7 10:39
  • 上次更新2024/8/7 11:26:16
查看原帖
求助玄关
745358
ChenZQ楼主2024/8/7 10:39

这个代码有人能改成c++吗,我看不懂...

def min_operations_to_convert(A, B):
    if sorted(A) != sorted(B) or len(A) != len(B):
        return -1

    len_A = len(A)
    j = len_B - 1
    operations = 0

    for i in range(len_A):
        if A[i] == B[j]:
            j -= 1
            if j < 0:
                return operations
        else:
            operations += 1

    return -1 if j != -1 else operations

# 示例
A = "abcde"
B = "edcba"
print(min_operations_to_convert(A, B)) 
2024/8/7 10:39
加载中...