题目如下:

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的位置一定能放当前能放的元素中最大的那个。所以遍历S,如果是I,放入当前的最小值,同时最小值加一;如果是D,放入当前的最大值,同时最大值减一。

代码如下:

class Solution(object):
def diStringMatch(self, S):
"""
:type S: str
:rtype: List[int]
"""
low = 0
high = len(S) res = []
for i in S:
if i == 'I':
res.append(low)
low += 1
else:
res.append(high)
high -= 1
res.append(low)
return res

【leetcode】942. DI String Match的更多相关文章

  1. 【LeetCode】942. DI String Match 解题报告(Python)

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

  2. 【Leetcode_easy】942. DI String Match

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

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

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

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

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

  5. 【LeetCode】880. Decoded String at Index 解题报告(Python)

    [LeetCode]880. Decoded String at Index 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...

  6. 【LeetCode】#344 Reverse String

    [Question] Write a function that takes a string as input and returns the string reversed. Example: G ...

  7. 【Leetcode_easy】686. Repeated String Match

    problem 686. Repeated String Match solution1: 使用string类的find函数: class Solution { public: int repeate ...

  8. #Leetcode# 942. DI String Match

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

  9. LeetCode 942 DI String Match 解题报告

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

随机推荐

  1. eclipse 启动项目 报错 java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderLis(亲测)

    [原因] 重新 clean  和  install 了maven项目后就启动报错了.解决如下: 右键项目: 属性properties 删除掉引用的其他jar 选择 Deployment Assembl ...

  2. python每日练习10题

    161.求1000以内的所有的素数以及闰年的数之和 第一步:求1000以内的素数,素数:只能被1和本身整除的数叫素数 import math def is_prime(num): if num ==1 ...

  3. 英语单词substitution

    substitution 来源——shell字符串切片 [root@centos73 ~]# echo ${$alpha:3:4} -bash: ${$alpha:3:4}: bad substitu ...

  4. tab切换中的滚动条下拉分页带来的问题

    相信做过tab切换中滚动条下拉分页的童鞋都知道,我们在用scroll方法来做滚动条下拉分页的时候,都是有bug,切换中间的内容会互相影响,为了解决这个问题,我总结了2种方法: 1.方法一: <! ...

  5. BUUCTF | CODE REVIEW 1 (反序列化,md5绕过)

    <?php /** * Created by PhpStorm. * User: jinzhao * Date: 2019/10/6 * Time: 8:04 PM */ highlight_f ...

  6. element table 通过selection-change选中的索引删除

    <el-table :row-class-name="tableRowClassName" @selection-change="handleSelectionCh ...

  7. [CSP-S模拟测试]:Emotional Flutter(贪心)

    题目传送门(内部题51) 输入格式 第一行一个整数$t$表示数据组数.每组数据的第一行有三个整数$s,k,n$.第二行有$n$个整数$A_1,A_2,...,A_n$,依次表示黑白条的长度. 输出格式 ...

  8. win7NVIDIA显卡驱动升级时卡住

    可以先装上.NET framework,再更新就不会卡了

  9. Javascript对checkbox勾选判断,错误提示和按钮变色操作

    同意相关条款未打钩时,登录按钮为灰色且无法提交,点击灰色的登录按钮提示同意相关条款,打钩后变成亮色且可以提交信息. 勾选框及文字: <div class="check-rule&quo ...

  10. 大数据学习笔记之Hadoop(三):MapReduce&YARN

    文章目录 一 MapReduce概念 1.1 为什么要MapReduce 1.2 MapReduce核心思想 1.3 MapReduce进程 1.4 MapReduce编程规范(八股文) 1.5 Ma ...