LeetCode 942 DI String Match 解题报告
题目要求
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]
题目分析及思路
题目给出一个只包含I或D的字符串,要求返回一个序列,序列长度为字符串长度N+1。当字母为I,则序列的对应位置比后面位置的值要大;若字母为D,则相反。我们可以使第一个出现I的位置对应的是0,第一个D出现的位置对应的是N,那么无论这个位置后面出现的是另外的哪个数字,当前的位置都能满足题设条件。我们每次遇见I都比之前的I增加1,每次遇到D都比之前的D减小1。这样会尽可能的给后面的数字让出空间。
python代码
class Solution:
def diStringMatch(self, S):
"""
:type S: str
:rtype: List[int]
"""
N = len(S)
min,max = 0,N
res = list()
for s in S:
if s == 'I':
res.append(min)
min = min + 1
if s == 'D':
res.append(max)
max = max - 1
res.append(min)
return res
LeetCode 942 DI String Match 解题报告的更多相关文章
- 【LeetCode】942. DI String Match 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- #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 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/ 题目地 ...
- 【LeetCode】838. Push Dominoes 解题报告(Python)
[LeetCode]838. Push Dominoes 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:// ...
随机推荐
- 【iCore1S 双核心板_ARM】例程十五:USB_HID实验——双向数据传输
实验方法: 1.USB_HID协议免驱动,此例程不需要驱. 2.将跳线冒跳至USB_OTG,通过Micro USB 线将iCore1S USB-OTG接口与电脑相连. 3.打开上位机软件usb_hid ...
- 【iCore1S 双核心板_FPGA】例程十一:Modelsim仿真实验
实验现象: 通过仿真波形,分析输入与输出的关系,可以清晰的看到所添加信号波形的变化与程序所写的一致. 核心代码: module modelsim( input CLK_12M, output FPGA ...
- mac中安装wxpython
一.简介 wxPython是Python语言的一套优秀的GUI图形库,允许Python程序员很方便的创建完整的.功能键全的GUI用户界面. wxPython是作为优秀的跨平台GUI库wxWidgets ...
- (实用)CentOS 6.3更新内置Python2.6
在安装Kilo版的OpenStack时,我们发现社区已经将Python升到2.7,而CentOS 6.3上仍然在使用2.6版的Python.本文记录将CentOS 6.3内置的Python2.6更新为 ...
- OpenWRT AR9331 mjpg-streamer 网络安装和离线ipk安装
OpenWRT AR9331 固件 我的摄像头ID为: root@Off-1CD0:/# lsusb Bus 001 Device 002: ID 1871:0101 OpenWRT支持的UVV摄像 ...
- linux 斜杠/
inux OS: 使用”/“ 例子:/home/user/XXX 特例:路径中某目录名包含空格,在命令行中使用cd等命令书写路径时,则要在空格前加”\“ 例子: 主目录(/home/student ...
- 安卓开发笔记——关于开源项目SlidingMenu的使用介绍(仿QQ5.0侧滑菜单)
记得去年年末的时候写过这个侧滑效果,当时是利用自定义HorizontalScrollView来实现的,效果如下: 有兴趣的朋友可以看看这篇文件<安卓开发笔记——自定义HorizontalScro ...
- [Python] 02 - String
字符串 string 一.基本性质 不变性 Immutability 要变就 --> list --> string 二.功能函数 功能函数 S = 'Spam" S.find( ...
- [Python] pip 简明指南
安装 1. 使用 apt 安装: $ sudo apt-get install python-pip 截止本文落笔,apt 安装 pip 版本为 1.0,但 pip 最新发行版本为 1.5.6 $ p ...
- centos7 安装zookeeper3.4.8集群
1.下载上传文件到centos中 2.解压文件夹 3.cd conf 文件下,cp zoo_sample.cfg zoo.cfg 4.vim zoo.cfg # The number of mil ...