Q942 增减字符串匹配
给定只含 "I"(增大)或 "D"(减小)的字符串 S ,令 N = S.length。
返回 [0, 1, ..., N] 的任意排列 A 使得对于所有 i = 0, ..., N-1,都有:
- 如果
S[i] == "I",那么A[i] < A[i+1] - 如果
S[i] == "D",那么A[i] > A[i+1]
示例 1:
输出:"IDID"
输出:[0,4,1,3,2]
示例 2:
输出:"III"
输出:[0,1,2,3]
示例 3:
输出:"DDI"
输出:[3,2,0,1]
提示:
1 <= S.length <= 1000S只包含字符"I"或"D"。
class Solution942 {
public int[] diStringMatch(String S) {
int[] nums = new int[S.length() + 1];
char[] chs = S.toCharArray();
int n = 0;
for (char c : chs) {
if (c == 'D')
n++;
}
int m = n + 1;
nums[0] = n--;
for (int i = 0; i < chs.length; i++) {
if (chs[i] == 'I')
nums[i + 1] = m++;
else
nums[i + 1] = n--;
}
return nums;
}
}
Q942 增减字符串匹配的更多相关文章
- LeetCode 942. 增减字符串匹配(DI String Match) 49
942. 增减字符串匹配 942. DI String Match 题目描述 每日一算法2019/6/21Day 49LeetCode942. DI String Match Java 实现 and ...
- [Swift]LeetCode942. 增减字符串匹配 | DI String Match
Given a string S that only contains "I" (increase) or "D" (decrease), let N = S. ...
- PHP算法之增减字符串匹配
给定只含 "I"(增大)或 "D"(减小)的字符串 S ,令 N = S.length. 返回 [0, 1, ..., N] 的任意排列 A 使得对于所有 i ...
- 字符串匹配的KMP算法
~~~摘录 来源:阮一峰~~~ 字符串匹配是计算机的基本任务之一. 举例来说,有一个字符串”BBC ABCDAB ABCDABCDABDE”,我想知道,里面是否包含另一个字符串”ABCDABD”? 许 ...
- {Reship}{KMP字符串匹配}
关于KMP字符串匹配的介绍和归纳,作者的思路非常清晰,推荐看一下 http://blog.csdn.net/v_july_v/article/details/7041827
- 字符串匹配(hash算法)
hash函数对大家来说不陌生吧 ? 而这次我们就用hash函数来实现字符串匹配. 首先我们会想一下二进制数. 对于任意一个二进制数,我们将它化为10进制的数的方法如下(以二进制数1101101为例): ...
- 【C++实现python字符串函数库】二:字符串匹配函数startswith与endswith
[C++实现python字符串函数库]字符串匹配函数startswith与endswith 这两个函数用于匹配字符串的开头或末尾,判断是否包含另一个字符串,它们返回bool值.startswith() ...
- sdut 2125串结构练习--字符串匹配【两种KMP算法】
串结构练习——字符串匹配 Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目链接:http://acm.sdut.edu.cn/sduto ...
- C语言字符串匹配函数
C语言字符串匹配函数,保存有需要时可以用: #include <stdio.h> #include <stdlib.h> #include <string.h> # ...
随机推荐
- Django框架 之 logging配置
Django框架 之 logging配置 logging配置文件 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 2 ...
- js 二维数组排序sort()函数
一.按数值排序 var arr = [[1, 2, 3], [7, 2, 3], [3, 2, 3]]; arr.sort(function(x, y){ return x[0] – y[0];}) ...
- LightOJ 1027 A Dangerous Maze (数学期望)
题意:你面前有 n 个门,每次你可以选择任意一个进去,如果xi是正数,你将在xi后出去,如果xi是负数,那么xi后你将回来并且丢失所有记忆,问你出去的期望. 析:两种情况,第一种是直接出去,期望就是 ...
- Schwartz kernel theorem施瓦兹核定理
In mathematics, the Schwartz kernel theorem is a foundational result in the theory of generalized fu ...
- Mathematical optimization数学上的最优化
https://en.wikipedia.org/wiki/Mathematical_optimization In mathematics, computer science and operati ...
- OpenSSH/PuTTY/SSH使用
OpenSSH/PuTTY/SSH 常用SSH服务指令 ① 启动SSH服务的命令 service sshd start ② 停止SSH服务的命令 service sshd stop ③ 重新启动SSH ...
- 查询某张表被哪些存储过程或者视图用到的sql语句
/*查询某张表被哪些存储过程或者视图用到的sql语句*/select distinct object_name(id) from syscomments where id in (select id ...
- [转]history.back(-1)和history.go(-1)的区别
目录: 1.这个方法的用途 2.两个方法的区别 3.总结 概述: H5页面做多了,自然就会做到页面上的返回功能,返回功能大致有两种:history.back(-1)和history.go(-1),今天 ...
- Ubuntu的Unable to locate package无法更新源问题解决方案
https://blog.csdn.net/long19910605/article/details/47017889/ 问题: 更新源时提示不能联网(does the network require ...
- 关于nosql的讲解
Data Base 关于nosql的讲解 nosql非关系型数据库. 优点: 1.可扩展 2.大数据量,高性能 3.灵活的数据模型 4.高可用 缺点: 1.不正式 2.不标准 非关系型数据库有哪些: ...