【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 ...
随机推荐
- controllerweb.xml
<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://w ...
- 4412 搭建tftp服务器
搭建服务器 --安装xinetd,sudo apt-get install xinetd --安装tftp和tftpd,sudo apt-get install tftp tftpd --配置/etc ...
- 「长乐集训 2017 Day8」修路 (斯坦纳树)
题目描述 村子间的小路年久失修,为了保障村子之间的往来,AAA君决定带领大家修路. 村子可以看做是一个边带权的无向图GGG, GGG 由 nnn 个点与 mmm 条边组成,图中的点从 1∼n1 \si ...
- hdu 1007 Quoit Design (经典分治 求最近点对)
Problem Description Have you ever played quoit in a playground? Quoit is a game in which flat rings ...
- 跨域共享cookie
1. JSP中Cookie的读写 Cookie的本质是一个键值对,当浏览器访问web服务器的时候写入在客户端机器上,里面记录一些信息.Cookie还有一些附加信息,比如域名.有效时间.注释等等. 下面 ...
- 安装uWebSocketIO
https://github.com/uNetworking/uWebSockets sudo apt-get install libuv1-dev git clone https://github. ...
- window安装consul
安装consul 下载包: https://www.consul.io/ 解压 consul_1..2_windows_amd64.zip 复制 consul.exe 到 d:\soft\consul ...
- python之正则表达式(re模块)用法总结
用一句表示正则表达式,就是 字符串的模糊 匹配
- Activator.CreateInstance with parameters
https://docs.microsoft.com/en-us/dotnet/api/system.activator.createinstance?view=netframework-4.8#Sy ...
- SSAS MDX语句 期末查询简单示例
WITH Member [Measures].[num Last Day of Month] AS( [时间].[YQMD].CurrentMember.LastChild,[Measures].[门 ...