942. DI String Match
Easy
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
:
Example 1:
1 | Input: "IDID" |
Example 2:
1 | Input: "III" |
Example 3:
1 | Input: "DDI" |
Note:
1 <= S.length <= 10000
S
only contains characters"I"
or"D"
.
方案一
思路:返回的vector大小比S大 1,最大值即为S.size(),最小值为0,遇到I,最小值加1,遇到D,最大值减1;
1 | class Solution { |
result
Runtime: 56 ms, faster than 8.41% of C++ online submissions for DI String Match.
Memory Usage: 10.3 MB, less than 56.84% of C++ online submissions for DI String Match.
方案二
思路差不多,使用case代替if,初始化vector指定长度;
1 | class Solution { |
result
Runtime: 36 ms, faster than 97.85% of C++ online submissions for DI String Match.
Memory Usage: 9.8 MB, less than 95.04% of C++ online submissions for DI String Match.