作者: 负雪明烛
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. MybatisPlus入门程序

    参考资料:MybatisPlus官网 环境搭建 创建数据库 CREATE DATABASE `mybatisplus` ​ USE `mybatisplus` ​ CREATE TABLE `user ...

  2. sed 修改文件

    总结 正确的修改进文件命令(替换文件内容):sed -i "s#machangwei#mcw#g" mcw.txt 正确的修改追加进文件命令(追加文件内容):sed -i &quo ...

  3. ES6必知,变量的结构赋值。

    对象和数组时 Javascript 中最常用的两种数据结构,由于 JSON 数据格式的普及,二者已经成为 Javascript 语言中特别重要的一部分. 在编码过程中,我们经常定义许多对象和数组,然后 ...

  4. 【leetcode】834. Sum of Distances in Tree(图算法)

    There is an undirected connected tree with n nodes labeled from 0 to n - 1 and n - 1 edges. You are ...

  5. 【STM8】SPI通讯

    这篇内容有点长,如果有人想透过我的博客学习STM8的SPI,那是我的荣幸 首先我要先说大纲,这样大家心里比较有底,可以把精力都用在SPI理解上 [SPI初步介绍]:介绍SPI如何接线.名称解释.通讯注 ...

  6. SqlSession与SqlSessionFactory到底是什么关系?

    1. SqlSession和SqlSessionFactory的接口定义 SqlSession: public interface SqlSession extends Closeable {     ...

  7. 集合类——集合输出、栈和队列及Collections集合

    1.集合输出 在之前我们利用了toString()及get()方法对集合进行了输出,其实那都不是集合的标准输出,集合输出有四种方式:Iterator.ListIterator.Enumeration. ...

  8. proxysql+MHA+半同步复制

    先配置成主从同步 先在各节点安装服务 [root@inotify ~]# yum install mariadb-server -y 编辑主节点的配置文件,并启动 [root@centos7 ~]# ...

  9. GO类型转换

    golang []byte转string golang中,字符切片[]byte转换成string最简单的方式是 package main import ( "fmt" _ &quo ...

  10. spring下春注解的声明式事务控制

    package com.hope.test;import com.hope.domain.Account;import com.hope.service.IAccountService;import ...