【LeetCode】942. DI String Match 解题报告(Python)
作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/
题目地址:https://leetcode.com/problems/di-string-match/description/
题目描述
Given a string S that only contains "I" (increase) or "D" (decrease), let N = S.length.
Return any permutation A of [0, 1, ..., N] such that for all i = 0, ..., N-1:
- If
S[i] == "I", thenA[i] < A[i+1] - If
S[i] == "D", thenA[i] > A[i+1]
Example 1:
Input: "IDID"
Output: [0,4,1,3,2]
Example 2:
Input: "III"
Output: [0,1,2,3]
Example 3:
Input: "DDI"
Output: [3,2,0,1]
Note:
- 1 <= S.length <= 10000
- S only contains characters “I” or “D”.
题目大意
已知一个字符串只包含I和D,求[0~N]的一个排列,使得如果字符串的某个位置是I,那么数组对应的位置和后面的位置是递增的;如果字符串的某个位置是D,那么数组对应的位置和后面的位置是递减的。
解题方法
乍一看很难的题目,但是仔细分析一下,发现很简单的:类似于贪心策略,我们使第一个出现的I的位置对应的是0,第一个D出现的位置对应的是N,那么无论这个位置后面无论出现的是另外的哪个数字,当前的位置都能满足题设条件。我们每次遇见I都比之前的I增加1,每次遇到D都比之前的D减小1.这样会尽可能的给后面的数字让出空间。
最后还要注意的是数组比字符串多一个位置,这个位置其实就是剩下的那个数字,比如我的解法中的ni或者nd都可以。
时间复杂度是O(N),空间复杂度是O(1)。
class Solution:
def diStringMatch(self, S):
"""
:type S: str
:rtype: List[int]
"""
N = len(S)
ni, nd = 0, N
res = []
for s in S:
if s == "I":
res.append(ni)
ni += 1
else:
res.append(nd)
nd -= 1
res.append(ni)
return res
日期
2018 年 11 月 18 日 —— 出去玩了一天,腿都要废了
【LeetCode】942. DI String Match 解题报告(Python)的更多相关文章
- LeetCode 942 DI String Match 解题报告
题目要求 Given a string S that only contains "I" (increase) or "D" (decrease), let N ...
- #Leetcode# 942. DI String Match
https://leetcode.com/problems/di-string-match/ Given a string S that only contains "I" (in ...
- 【Leetcode_easy】942. DI String Match
problem 942. DI String Match 参考 1. Leetcode_easy_942. DI String Match; 完
- 【LeetCode】686. Repeated String Match 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【leetcode】942. DI String Match
题目如下: Given a string S that only contains "I" (increase) or "D" (decrease), let ...
- 【LeetCode】443. String Compression 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用额外空间 不使用额外空间 日期 题目地址:htt ...
- 【LeetCode】62. Unique Paths 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址:https://leetcode.com/problems/unique-pa ...
- LeetCode 942. 增减字符串匹配(DI String Match) 49
942. 增减字符串匹配 942. DI String Match 题目描述 每日一算法2019/6/21Day 49LeetCode942. DI String Match Java 实现 and ...
- 【LeetCode】649. Dota2 Senate 解题报告(Python)
[LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...
随机推荐
- Linux中shell去除空行的几种方法
有时我们在处理和查看文件时,经常会有很多空行,为了美观或是有需要时,就有必要把这些除行去掉了,方法如下: #如需将结果输出加入重定向 > 文件名 1)用tr命令 代码如下: cat ...
- mysql 计算日期为当年第几季度
select T21620.日期 as F21634, QUARTER('98-04-01') as quarter #返回日期是一年的第几个季度 - ...
- Mac下source tree 下的安装
安装时出现了以下错误,解决方法 git -c diff.mnemonicprefix=false -c core.quotepath=false -c credential.helper=source ...
- 基于Kubernetes实现前后端应用的金丝雀发布
基于Kubernetes实现前后端应用的金丝雀发布 公司的研发管理平台实现了Gitlab+Kubernetes的Devops,在ToB和ToC场景中,由于用户量大,且预发布环境和生产环境或多或少存在差 ...
- 这份github上被14万人点赞的Java教程太强了
前几天有个小伙伴加我之后问了下面的这个问题.我看到后是一脸懵逼的状态,jcombobox?实话说,我已经完全忘了在Java中还有这么个东西. 在网上一番搜索后,才发现原来它是 swing 中的下拉列表 ...
- A Child's History of England.28
By such means, and by taxing and oppressing the English people in every possible way, the Red King b ...
- eclipse上点击open Perspective找不到java EE的解决办法
原因:没有安装java ee等插件 Help--->Install New software---->work with中选择All Available Sites----> ...
- CentOS7 搭建maven私服Nexus
下载解压 官网https://www.sonatype.com/download-oss-sonatype 下载页面 https://help.sonatype.com/repomanager2/do ...
- Linux 下使用rtcwake实现定时休眠和唤醒设备
查看是否安装rtcwake whereis rtcwake rtcwake: /usr/sbin/rtcwake /usr/share/man/man8/rtcwake.8.gz 查看rtcwake帮 ...
- jQuery对象进行方法扩展
<!DOCTYPE html><html><head> <meta charset="UTF-8"> <title>01 ...