python删除三个以上相邻重复字符

双指针法:
用一个左指针和一个右指针,遍历字符串,先固定左指针,
当右指针指向的值等于左指针的值时,就继续移动右指针,然后判断右指针与左指针之间的长度是否大于等于3,如果是的话,就删除掉s中的这一段,并且左指针回退2位,如果否,就移动左指针;

def elimination(s):
    l = 0
    while l < len(s):
        r = l  # 把右指针更新为当前位置
        while r < len(s) and s[r] == s[l]:
            r += 1
        if r - l >= 3:  # 如果找到三个以上重复的元素
            s = s[:l] + s[r:]  # 在s中删除
            l -= 2  # l回退2个位置,比如CCDDD, 回退两个位置能使l指向第一个C
            if l < 0:
                l = 0
        else:
            l = r
            # l += 1  # 这里也可以是l+=1,但是会存在一定的重复运算 比如CCB,l先指向第一个C,然后又要指向第二个C,但r-l都不满足>=3
    return s

s = 'AAAABBBCCCBBCA'
ans = elimination(s)
print(ans)

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的三维路径规划算法的Python实现,使用了A*算法和欧几里得距离作为启发式函数: ```python import heapq import math # 定义三维空间中的节点类 class Node: def __init__(self, x, y, z): self.x = x self.y = y self.z = z self.g = 0 # 节点的实际代价 self.h = 0 # 节点的启发式代价 self.parent = None # 定义节点之间的距离计算方法 def distance_to(self, other): dx = self.x - other.x dy = self.y - other.y dz = self.z - other.z return math.sqrt(dx*dx + dy*dy + dz*dz) # 定义节点之间的比较方法 def __lt__(self, other): return self.g + self.h < other.g + other.h # 定义三维空间中的网格类 class Grid: def __init__(self, width, height, depth): self.width = width self.height = height self.depth = depth self.grid = [[[' ' for z in range(depth)] for y in range(height)] for x in range(width)] # 定义网格中的障碍物和自由空间 def set_obstacle(self, x, y, z): self.grid[x][y][z] = '#' def set_free(self, x, y, z): self.grid[x][y][z] = ' ' def is_free(self, x, y, z): return self.grid[x][y][z] == ' ' # 定义三维空间中的路径规划方法 def find_path(self, start, goal): # 初始化起始节点和目标节点 start.g = 0 start.h = start.distance_to(goal) open_list = [start] closed_list = [] # 开始搜索路径 while open_list: current = heapq.heappop(open_list) # 弹出开放列表中最小代价的节点 if current == goal: path = [] while current: path.append(current) current = current.parent return path[::-1] # 返回反转的路径 closed_list.append(current) # 将当前节点加入闭合列表 # 遍历当前节点的所有相邻节点 for dx in range(-1, 2): for dy in range(-1, 2): for dz in range(-1, 2): if dx == dy == dz == 0: continue x, y, z = current.x + dx, current.y + dy, current.z + dz if x < 0 or x >= self.width or y < 0 or y >= self.height or z < 0 or z >= self.depth: continue if self.grid[x][y][z] == '#': continue neighbor = Node(x, y, z) neighbor.g = current.g + current.distance_to(neighbor) neighbor.h = neighbor.distance_to(goal) neighbor.parent = current # 如果相邻节点已经在闭合列表中,则跳过 if neighbor in closed_list: continue # 如果相邻节点已经在开放列表中,则更新其代价和父节点 if neighbor in open_list: index = open_list.index(neighbor) if open_list[index].g > neighbor.g: open_list[index].g = neighbor.g open_list[index].parent = current else: # 否则,将相邻节点加入开放列表 heapq.heappush(open_list, neighbor) # 如果没有找到路径,则返回空列表 return [] # 测试路径规划算法 if __name__ == '__main__': grid = Grid(5, 5, 5) grid.set_obstacle(1, 1, 0) grid.set_obstacle(1, 2, 0) grid.set_obstacle(2, 1, 0) grid.set_obstacle(2, 2, 0) start = Node(0, 0, 0) goal = Node(4, 4, 4) path = grid.find_path(start, goal) if path: for node in path: print(node.x, node.y, node.z) else: print('No path found') ``` 上面的代码实现了一个简单的三维空间中的路径规划算法,其中障碍物用字符“#”表示,自由空间用空格表示。你可以根据需要修改障碍物和自由空间的表示方式,以及启发式函数的计算方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值