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".

Approach #1:

class Solution {
public:
vector<int> diStringMatch(string S) {
int len = S.length();
vector<int> ans(len+1, 0);
int index = 0;
for (int i = 0; i < len; ++i)
if (S[i] == 'I') ans[i] = index++;
ans[len] = index++;
for (int i = len-1; i >= 0; --i)
if (S[i] == 'D') ans[i] = index++;
return ans;
}
};

  

Weekly Contest 111-------->942. DI String Match的更多相关文章

  1. 【Leetcode_easy】942. DI String Match

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

  2. #Leetcode# 942. DI String Match

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

  3. LeetCode 942 DI String Match 解题报告

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

  4. 【leetcode】942. DI String Match

    题目如下: Given a string S that only contains "I" (increase) or "D" (decrease), let  ...

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

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

  6. LeetCode 942. 增减字符串匹配(DI String Match) 49

    942. 增减字符串匹配 942. DI String Match 题目描述 每日一算法2019/6/21Day 49LeetCode942. DI String Match Java 实现 and ...

  7. LeetCode.942-DI字符串匹配(DI String Match)

    这是悦乐书的第361次更新,第388篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第223题(顺位题号是942).给定仅包含I(增加)或D(减少)的字符串S,令N = S ...

  8. [Swift]LeetCode942. 增减字符串匹配 | DI String Match

    Given a string S that only contains "I" (increase) or "D" (decrease), let N = S. ...

  9. Leetcode942. DI String Match增减字符串

    给定只含 "I"(增大)或 "D"(减小)的字符串 S ,令 N = S.length. 返回 [0, 1, ..., N] 的任意排列 A 使得对于所有 i ...

随机推荐

  1. cocos2d-x lua 中使用protobuf并对http进行处理

    cocos2d-x lua 中使用protobuf并对http进行处理 本文介绍 cocos2d-x lua 中使用http 和 基于cocos2d-x 对lua http的封装(部分ok) 本博客链 ...

  2. 查看客户端java日志

    通过 Java 控制面板启用 Java 控制台 Windows 8 使用搜索来查找控制面板 按 Windows 徽标键 + W 以打开搜索框来搜索设置,或者将鼠标指针拖动到屏幕的右下角,然后单击搜索图 ...

  3. 【BZOJ2161】布娃娃 扫描线+线段树

    [BZOJ2161]布娃娃 Description 小时候的雨荨非常听话,是父母眼中的好孩子.在学校是老师的左右手,同学的好榜样.后来她成为艾利斯顿第二代考神,这和小时候培养的良好素质是分不开的.雨荨 ...

  4. 一步步玩pcDuino3--uboot下的ping,加入命令能够接受来自host的ping

    uboot是一个很优秀的开源项目.不只能够学习bootloader.嵌入式,各种总线协议. 还能够了解网络协议栈.在嵌入式开发中,常常使用uboot的tftp和nfs来加快开发的效率.那么在tftp能 ...

  5. LVS的体系结构

    LVS集群的体系结构 章文嵩 (wensong@linux-vs.org) 转自LVS官方资料 2002 年 4 月 本文主要介绍了LVS集群的体系结构.先给出LVS集群的通用体系结构,并讨论了其的设 ...

  6. eclipse 中PlantUML的安装和使用

    安装: 填写的地址:http://hallvard.github.io/plantuml/ 安装完plantUML后,还要下载一个Graphviz https://pan.baidu.com/s/1g ...

  7. centos6.5安装jdk

    1.查看Linux环境自带JDK 使用命令:# rpm -qa|grep gcj 显示内容其中包含相应信息# java-x.x.x-gcj-compat-x.x.x.x-xxjpp# java-x.x ...

  8. 矩阵管理——和visitor模式没有本质区别,都是为了避免资源重复

    矩阵管理中的员工是双线汇报的模式.其上司有两个,一个是流程上司,一个是专业上司.流程上司负责你的日常考核,专业上司负责你的晋升和任免. 管理条件 相对于矩阵管理的矩阵式组织,适合于某些较为庞大的全球性 ...

  9. ubuntu166.04之Caffe安装

    写在前面:之前一直在搞keras,最近由于某些需求,需要学习caffe,在此记录caffe的安装记录.默认已经安装了cuda 如果是从其他的深度学习平台迁移到Caffe,那么按照这个教程来就可以了. ...

  10. 数组(Array)的初始化

    如果这样: private static int unsorted[]; for(int i = 1 ; i < 8 ; i ++ ) unsorted[i] = 1 ; 是会报NullPoin ...