原题链接:https://leetcode-cn.com/problems/di-string-match/
花了半个多小时的时间才做出来,
问题出在为什么最后总是少一位。仔细读题将最后一位补上即可
这种题最好先画出来再做。
public class Solution {
public int[] DiStringMatch(string S) {
List<int> res = new List<int>();
int first = 0; int end = S.Length;
char[] c = S.ToCharArray();
for (int i =0; i < c.Length; i++)
{
if (c[i]=='I')
{
res.Add(first);
first++;
}
else
{
res.Add(end);
end--;
}
}
if (c[S.Length-1]=='I')
{
res.Add(first);
first++;
}
else
{
res.Add(end);
end--;
}
return res.ToArray();
}
}