Re: [闲聊] 每日leetcode

楼主: SecondRun (雨夜琴声)   2024-04-07 15:26:08
想法差不多
但我一开始最后面只判断个数没检查index
(就是直接return star.Count>=left.Count)
想说为什么都过不了
修正之后就过了
大神好强
C# code:
public class Solution {
public bool CheckValidString(string s) {
var left = new Stack<int>();
var star = new Stack<int>();
int length = s.Length;
for (int i=0; i<length; i++)
{
if (s[i] == '(')
{
left.Push(i);
continue;
}
if (s[i] == '*')
{
star.Push(i);
continue;
}
if (left.Count > 0)
{
left.Pop();
continue;
}
if (star.Count > 0)
{
star.Pop();
continue;
}
return false;
}
if (left.Count > star.Count) return false;
while (left.Count != 0)
{
if (left.Pop() > star.Pop()) return false;
}
return true;
}
}
作者: JIWP (JIWP)   2024-04-07 15:28:00
大师,别卷了

Links booklink

Contact Us: admin [ a t ] ucptt.com