[Programmers] #120841 - 점의 위치 구하기 [Java][C++][Python]
좌표의 부호 조합으로 점이 속한 사분면 번호를 구하는 워밍업 문제.
[Programmers] #120841 - 점의 위치 구하기 [Java][C++][Python]
1. 아이디어
점이 축 위에 놓이지 않음이 보장되므로, x와 y가 각각 양수인지 음수인지만 따지면 사분면이 하나로 정해진다.
x가 양수면y가 양수일 때 1사분면, 음수일 때 4사분면이다.x가 음수면y가 양수일 때 2사분면, 음수일 때 3사분면이다.
2. 복잡도
| 접근 | 시간 | 공간 |
|---|---|---|
| 풀이 | $O(1)$ | $O(1)$ |
3. 코드
풀이 [Java][C++][Python]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
public int solution(int[] dot) {
int x = dot[0], y = dot[1];
if (x > 0) {
if (y > 0) {
return 1;
} else {
return 4;
}
} else {
if (y > 0) {
return 2;
} else {
return 3;
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <bits/stdc++.h>
using namespace std;
int solution(vector<int> dot) {
int x = dot[0], y = dot[1];
if (x > 0) {
if (y > 0) {
return 1;
} else {
return 4;
}
} else {
if (y > 0) {
return 2;
} else {
return 3;
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
def solution(dot):
x, y = dot
if x > 0:
if y > 0:
return 1
else:
return 4
else:
if y > 0:
return 2
else:
return 3
This post is licensed under CC BY 4.0 by the author.