作者: 负雪明烛
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", then A[i] < A[i+1]
  • If S[i] == "D", then A[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. 1 <= S.length <= 10000
  2. 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)的更多相关文章

  1. LeetCode 942 DI String Match 解题报告

    题目要求 Given a string S that only contains "I" (increase) or "D" (decrease), let N ...

  2. #Leetcode# 942. DI String Match

    https://leetcode.com/problems/di-string-match/ Given a string S that only contains "I" (in ...

  3. 【Leetcode_easy】942. DI String Match

    problem 942. DI String Match 参考 1. Leetcode_easy_942. DI String Match; 完

  4. 【LeetCode】686. Repeated String Match 解题报告(Python & C++)

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

  5. 【leetcode】942. DI String Match

    题目如下: Given a string S that only contains "I" (increase) or "D" (decrease), let  ...

  6. 【LeetCode】443. String Compression 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 使用额外空间 不使用额外空间 日期 题目地址:htt ...

  7. 【LeetCode】62. Unique Paths 解题报告(Python & C++)

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

  8. LeetCode 942. 增减字符串匹配(DI String Match) 49

    942. 增减字符串匹配 942. DI String Match 题目描述 每日一算法2019/6/21Day 49LeetCode942. DI String Match Java 实现 and ...

  9. 【LeetCode】649. Dota2 Senate 解题报告(Python)

    [LeetCode]649. Dota2 Senate 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地 ...

随机推荐

  1. dlang 安装

    刷论坛看到TIOBE排行榜,排名靠前的基本是C.C++.java.python之类的语言,常用的R语言近几年排名一路走高,前20基本变化不大. 后面发现第二十九位居然有个叫做D的语言,看了下和C语法很 ...

  2. 37-Invert Binary Tree

    Invert Binary Tree My Submissions QuestionEditorial Solution Total Accepted: 87818 Total Submissions ...

  3. vector去重--unique

    具体实现见中间源码 function template <algorithm> std::unique equality (1) template <class ForwardIte ...

  4. 8种Vue中数据更新了但页面没有更新的情况

    目录 1.Vue 无法检测实例被创建时不存在于 data 中的 属性 2. Vue 无法检测'对象属性'的添加或移除 3.Vue 不能检测利用数组索引直接修改一个数组项 4.Vue 不能监测直接修改数 ...

  5. 大数据学习day32-----spark12-----1. sparkstreaming(1.1简介,1.2 sparkstreaming入门程序(统计单词个数,updateStageByKey的用法,1.3 SparkStreaming整合Kafka,1.4 SparkStreaming获取KafkaRDD的偏移量,并将偏移量写入kafka中)

    1. Spark Streaming 1.1 简介(来源:spark官网介绍) Spark Streaming是Spark Core API的扩展,其是支持可伸缩.高吞吐量.容错的实时数据流处理.Sp ...

  6. 零基础学习java------day2------关键字、标志符、常量、进制键的转换、java中的数据类型、强制类型转换的格式

    今日内容要求: 1. 了解关键字的概念及特点,了解保留字 2. 熟练掌握标识符的含义,特点,可使用字符及注意事项 3. 了解常量的概念,进制,进制之间相互转换,了解有符号标识法的运算方式 4. 掌握变 ...

  7. Node.js 概述

    JavaScript 标准参考教程(alpha) 草稿二:Node.js Node.js 概述 GitHub TOP Node.js 概述 来自<JavaScript 标准参考教程(alpha) ...

  8. 一起手写吧!promise.all

    Promise.all 接收一个 promise 对象的数组作为参数,当这个数组里的所有 promise 对象全部变为resolve或 有 reject 状态出现的时候,它才会去调用 .then 方法 ...

  9. DP-Burst Balloons

    leetcode312: https://leetcode.com/problems/burst-balloons/#/description Given n balloons, indexed fr ...

  10. 基于Annotation(注解)的装配

    一.常用注解 1.@Component 是一种通用注解,可用于任何Bean 2.@Repository 通常用于注解DAO层类,即持久层 3.@Service 通常用于注解Service类,即服务层 ...