IT/알고리즘
21] Leetcode 657. Robot Return to Origin
깻잎쌈
2020. 7. 1. 20:28
반응형
Robot Return to Origin - LeetCode
Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.
leetcode.com
주어진 문자열대로 이동했을때 원래 위치로 돌아올 수 있는지 묻는 문제.
class Solution {
public:
bool judgeCircle(string moves) {
int upDown = 0;
int leftRight =0;
for(int i = 0;i<moves.size();i++){
if(moves[i] =='U')
upDown++;
else if(moves[i] =='D')
upDown--;
else if(moves[i] =='L')
leftRight++;
else if(moves[i] =='R')
leftRight--;
}
if(upDown == 0 && leftRight ==0)
return true;
else
return false;
}
};
반응형