반응형
https://leetcode.com/problems/path-crossing/
방향을 가리키는 문자열을 따라 이동했을 때 같은 점은 두 번 지나가는지 묻는 문제.
for문으로 원점을 처음부터 하나씩 바꿔가면서
다시 원점으로 돌아가는지 확인한다.
class Solution {
public:
bool isPathCrossing(string path) {
bool ans = false;
int upDown = 0;
int leftRight = 0;
for(int i = 0; i<path.size();i++){
upDown = leftRight = 0;
for(int j = i;j<path.size();j++){
if(path[j]=='N')
upDown++;
else if(path[j]=='S')
upDown--;
else if(path[j]=='E')
leftRight++;
else if(path[j]=='W')
leftRight--;
if(leftRight ==0 && upDown ==0){
ans = true;
return ans;
}
}
}
return ans;
}
};
반응형
'IT > 알고리즘' 카테고리의 다른 글
27] Leetcode 1266. Minimum Time Visiting All Points (0) | 2020.07.06 |
---|---|
26★] Leetcode 1491. Average Salary Excluding the Minimum and Maximum Salary (0) | 2020.07.05 |
24] Leetcode 1502. Can Make Arithmetic Progression From Sequence (0) | 2020.07.05 |
23] Leetcode 728. Self Dividing Numbers (0) | 2020.07.04 |
22] Leetcode 1446. Consecutive Characters (0) | 2020.07.03 |
댓글