657. Robot Return to Origin
problem description
algorithm thought
code
class Solution {
public:
bool judgeCircle(string moves) {
stack<char> lr;
stack<char> ud;
for(auto ch:moves){
if(ch=='L'){
if(lr.empty()||lr.top()=='L'){
lr.push(ch);
}else
lr.pop();
}
if(ch=='R'){
if(lr.empty()||lr.top()=='R'){
lr.push(ch);
}else
lr.pop();
}
if(ch=='U'){
if(ud.empty()||ud.top()=='U'){
ud.push(ch);
}else
ud.pop();
}
if(ch=='D'){
if(ud.empty()||ud.top()=='D'){
ud.push(ch);
}else
ud.pop();
}
}
return lr.empty()&&ud.empty();
}
};algorithm analysis
Last updated