【leetcode】942. DI String Match
题目如下:
Given a string
Sthat only contains "I" (increase) or "D" (decrease), letN = S.length.Return any permutation
Aof[0, 1, ..., N]such that for alli = 0, ..., N-1:
- If
S[i] == "I", thenA[i] < A[i+1]- If
S[i] == "D", thenA[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 <= S.length <= 10000Sonly 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的更多相关文章
- 【LeetCode】942. DI String Match 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【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】481. Magical String 解题报告(Python)
[LeetCode]481. Magical String 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http:/ ...
- 【LeetCode】880. Decoded String at Index 解题报告(Python)
[LeetCode]880. Decoded String at Index 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博 ...
- 【LeetCode】#344 Reverse String
[Question] Write a function that takes a string as input and returns the string reversed. Example: G ...
- 【Leetcode_easy】686. Repeated String Match
problem 686. Repeated String Match solution1: 使用string类的find函数: class Solution { public: int repeate ...
- #Leetcode# 942. DI String Match
https://leetcode.com/problems/di-string-match/ Given a string S that only contains "I" (in ...
- LeetCode 942 DI String Match 解题报告
题目要求 Given a string S that only contains "I" (increase) or "D" (decrease), let N ...
随机推荐
- Linux批量新建文件夹(大括号表达式的应用)
如果想要批量新建文件夹来存放照片,按照年份和月份,格式为YYYY-MM.可以使用下面命令批量新建: mkdir {2007..2009}-{01..12} 结果如下: 2007-01 2007-07 ...
- Ubuntu新建用户组
新建用户组 sudo addgroup groupname 把现有用户加入新建的用户组 sudo adduser username groupname
- JAVA学习笔记--赋值(“=”)
参考来源:<java编程思想(第四版)> 见第三章3.4节 基本数据类型存储了实际的数值,并非指向一个对象的引用,故其赋值,就是直接将一个地方的内容复制到了另一个地方.例如,对基本数据类型 ...
- 用闭包解决 js 循环中函数变量暂存问题
需求:有一个数组,根据数组的值渲染对应的数字div,单击对应的div 在控制台打印对应的数字.如点击1,控制台打印1. 问题: 不管点击哪个值 打出来都是4 代码如下 <!DOCTYPE htm ...
- vue之条件语句小结
vue之条件语句小结 v-if, v-else 随机生成一个数字,判断是否大于0.5,然后输出对应信息: <!DOCTYPE html> <html> <head> ...
- Java中static修饰类的问题
Java中static修饰类的问题 众所周知,Java中static关键字可以修饰方法与变量: 修饰变量的时候,这个变量属于类变量,可以直接通过类名.变量名来引用. 修饰方法的时候可以直接通过类名.方 ...
- linux 给指定用户分配文件夹权限
1.更改目录所有者命令:chown -R 用户名称 目录名称2.更改目录权限命令:chmod -R 755 目录名称3.查看文件夹的权限ls -la 目录
- 四两拨千斤,ARM是如何运作、靠什么赚钱的
在智能手机.平板大行其道的今天,ARM这个名字我们几乎每天都要见到或者听到几次,作为编辑的我更是如此,每天涉及到的新闻总是或多或少跟ARM扯上关系,它还与Intel.AMD.NVIDA等公司有说不清道 ...
- Oracle 表空间、用户
一.表空间 临时表空间 创建临时表空间范例 CREATE TEMPORARY TABLESPACE duke_temp /* 临时表空间名称:duke_temp */ tempfile 'C:\ora ...
- Chrome-谷歌页面翻译增强插件开发
最近想做一个 Chrome 的插件(看别的博客说其实叫插件不准确,应该叫拓展,大家叫习惯了就按习惯的来吧).一开始咱先直接看了[Chrome 开发(360 翻译)](http://open.chrom ...