반응형
주어진 문자열대로 이동했을때 원래 위치로 돌아올 수 있는지 묻는 문제.
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;
}
};
반응형
'IT > 알고리즘' 카테고리의 다른 글
23] Leetcode 728. Self Dividing Numbers (0) | 2020.07.04 |
---|---|
22] Leetcode 1446. Consecutive Characters (0) | 2020.07.03 |
20] Leetcode 1450. Number of Students Doing Homework at a Given Time (0) | 2020.07.01 |
19★] Leetcode 1436. Destination City (0) | 2020.06.30 |
18] Leetcode 1475. Final Prices With a Special Discount in a Shop (0) | 2020.06.29 |
댓글