作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址: https://leetcode.com/problems/special-binary-string/description/

题目描述:

Special binary strings are binary strings with the following two properties:

  • The number of 0’s is equal to the number of 1’s.
  • Every prefix of the binary string has at least as many 1’s as 0’s.

Given a special string S, a move consists of choosing two consecutive, non-empty, special substrings of S, and swapping them. (Two strings are consecutive if the last character of the first string is exactly one index before the first character of the second string.)

At the end of any number of moves, what is the lexicographically largest resulting string possible?

Example 1:

Input: S = "11011000"
Output: "11100100"
Explanation:
The strings "10" [occuring at S[1]] and "1100" [at S[3]] are swapped.
This is the lexicographically largest string possible after some number of swaps.

Note:

  1. S has length at most 50.
  2. S is guaranteed to be a special binary string as defined above.

题目大意

一个特殊的二进制字符串满足以下两个属性:

  1. 字符串中0的个数等于1的个数
  2. 这个字符串中任何前缀中1的个数不少于0的个数

给了一个满足要求的特殊字符串,每次移动可以选择两个两个连续的非空的特殊二进制子串进行交换。求在一系列移动之后,能得到的字母顺序的字符串最大结果。

解题方法

这道题的原型是括号匹配问题。求括号匹配的经典的方法是使用cnt记数的方法,如果是左括号,那么cnt+1,如果是右括号,那么cnt-1,如果cnt等于0了,说明这已经是个匹配了的括号了。注意,一个括号匹配问题中可能存在多个匹配的括号,这个题也是,比如1010就是满足题意的二进制字符串,相等于两个满足题意的二进制串组成。

如果要想在一系列移动之后,得到最大的字符串,那么可以看出要求1尽量在前面,同时0尽量在后面。我们使用list保存那些符合要求的字符串,最后排序即可。一个符合要求的字符串其开头必须是1,末尾必须是0。同时我们意识到,符合要求的字符串的内部也要进行排序。比如子串1010,要给他排序成1100这个样子,注意中间的字符串01并不符合题目要求,给他重新排成10样式,使用递归结构。使用i保存上一次判断完成的字符串的结尾,用j遍历字符串。每次判断结束一个符合要求的子串之后,要令 i = j + 1。

最坏情况下的时间复杂度是O(N!),最优情况下的时间复杂度是O(1),空间复杂度是O(N)。

class Solution(object):
def makeLargestSpecial(self, S):
"""
:type S: str
:rtype: str
"""
cnt = 0
res = list()
i= 0
for j, v in enumerate(S):
cnt += 1 if v == "1" else -1
if cnt == 0:
res.append("1" + self.makeLargestSpecial(S[i + 1:j]) + "0")
i = j + 1
return "".join(sorted(res, reverse=True))

参考资料:

http://www.cnblogs.com/grandyang/p/8606024.html
https://blog.csdn.net/u014688145/article/details/78996824
https://blog.csdn.net/BambooYH/article/details/80686074
http://www.cnblogs.com/zzuli2sjy/p/8260854.html

日期

2018 年 10 月 2 日 —— 小蓝单车莫名其妙收了我1块钱,明明每个月免费骑10次的啊!

【LeetCode】761. Special Binary String 解题报告(Python)的更多相关文章

  1. leetcode 761. Special Binary String

    761. Special Binary String 题意: 一个符合以下两个要求的二进制串: \(1.串中包含的1和0的个数是相等的.\) \(2.二进制串的所有前缀中1的个数不少于0的个数\) 被 ...

  2. 【LeetCode】481. Magical String 解题报告(Python)

    [LeetCode]481. Magical String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:/ ...

  3. 761. Special Binary String

    Special binary strings are binary strings with the following two properties: The number of 0's is eq ...

  4. 【LeetCode】401. Binary Watch 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 java解法 Python解法 日期 [LeetCo ...

  5. 【LeetCode】87. Scramble String 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 动态规划 日期 题目地址:https://le ...

  6. 【LeetCode】654. Maximum Binary Tree 解题报告 (Python&C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 日期 题目地址:https://leetcode ...

  7. 【LeetCode】796. Rotate String 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

  8. 【LeetCode】767. Reorganize String 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.me/ 题目地址:https://leetcode.com/problems/reorganiz ...

  9. 【LeetCode】344. Reverse String 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 新构建字符串 原地翻转 日期 题目地址:https://lee ...

随机推荐

  1. Docker 外部访问容器Pp、数据管理volume、网络network 介绍

    Docker 外部访问容器Pp.数据管理volume.网络network 介绍 外部访问容器 容器中可以运行一些网络应用,要让外部也可以访问这些应用,可以通过 -P 或 -p 参数来 指定端口映射. ...

  2. python20判断变量是否存在

    python中检测某个变量是否有定义 第一种方法使用内置函数locals(): locals():获取已定义对象字典 'testvar' in locals().keys() 第二种方法使用内置函数d ...

  3. python3安装,支持openssl,支持采集https

    python3安装,支持openssl,支持采集https 坑好多,特别是安装的时候,各种不匹配,服务器默认配置是python2,升级3后,采集的时候用到openssl,花了两天也没搞定各种错误,也许 ...

  4. Python os模块与sys模块

    1.os模块简单使用及说明 # -*- coding:utf-8 -*- """ os模块主要用于系统,处理程序与系统交互问题 大部分如属性等功能在linux系统中会使用 ...

  5. 08 eclipse配置JDK

    eclipse配置JDK1.8 一.打开eclipse:Window>>Preferences: 二.搜索:"jdk",并点击右侧的"Add": 三 ...

  6. Hadoop入门 集群崩溃的处理方法

    目录 集群崩溃的处理方法 搞崩集群 错误示范 正确处理方法 1 回到hadoop的家目录 2 杀死进程 3 删除每个集群的data和logs 4 格式化 5 启动集群 总结 原因分析 集群崩溃的处理方 ...

  7. webservice--cxf和spring结合

    服务端: 实体: package entity; import java.util.Date; /*** 实体 */ public class Pojo { //温度 private String d ...

  8. Android获取通知栏的高度

    1 public static int getStatusBarHeight(Context context){ 2         Class<?> c = null; 3        ...

  9. Dubbo服务调用超时

    服务降级的发生,其实是由于消费者调用服务超时引起的,即从发出调用请求到获取到提供者的响应结果这个时间超出了设定的时限.默认服务调用超时时限为1秒.可以在消费者端与提供者端设置超时时限. 一.创建提供者 ...

  10. 【Spring Framework】Spring IOC详解及Bean生命周期详细过程

    Spring IOC 首先,在此之前,我们就必须先知道什么是ioc,ioc叫做控制反转,也可以称为依赖注入(DI),实际上依赖注入是ioc的另一种说法, 1.谁控制谁?: 在以前,对象的创建和销毁都是 ...