题目如下:

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. Oracle11g新建用户及用户表空间

    /* 建立数据表空间 */CREATE TABLESPACE SP_TAB DATAFILE '/u01/app/oracle/oradata/orcl/tab1_1.dbf' size 1024M ...

  2. Git项目实战篇

    版本控制器:Git # 达到多人协同开发的目的 安装 """1.下载对应版本:https://git-scm.com/download2.安装git:在选取安装路径的下一 ...

  3. JS中Object的一些关于原型的方法

    1.Object.getPrototypeOf(obj) 该方法返回 obj 对象的原型对象,等同于 obj.__proto__.获取对象的原型对象推荐使用该方法而不是 obj.__proto__方法 ...

  4. HDU 6058 Kanade's sum —— 2017 Multi-University Training 3

    Kanade's sum Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Tota ...

  5. delphi 控件背景透明代码

    procedure DrawParentBackground(Control: TControl; DC: HDC; R: PRect = nil; bDrawErasebkgnd: Boolean ...

  6. vue数据渲染、条件判断及列表循环

    1.数据渲染  {{msg}} <template> <div id="app"> {{msg}} </div> </template&g ...

  7. linux从head.s到start_kernelstart_kernel之---内核重定位后分析

    参考: https://biscuitos.github.io/blog/ARM-BOOT/ zImage 重定位之后实践 zImage 重定位之后,ARM 将 pc 指针指向了重定位 zImage ...

  8. mysql 查昨天前天上个月数据

    主要使用到DATE_SUB()函数作用:从DATE或DATETIME值中减去时间值(或间隔). 语法:DATE_SUB(start_date, INTERVAL expr unit); 参数详解: s ...

  9. zabbix配置-模板

    1.配置=>模板=>创建模板=>输入模板名称和群组 2.配置=>模板=>找到刚刚创建的模板=>点击应用集(applications)=>创建应用集=>输 ...

  10. hive sql基础了解

    会有些不一样 1 例如使用SQL 之前,要了解用了那个库,use jz_daojia 2 使用GET_JSON_OBJECT 函数等,以及参数 匹配 $.childBrithDay 挺有意思的.新玩意 ...