답안 :
package answer;
import java.util.HashMap;
import java.util.Map;
class Solution {
public int[] solution(String[] park, String[] routes) {
/*
* 0. 명령맵핑 1. 현재위치파악 2. 이동명령확인 3. 명령파악(최대사이즈인가? 장애물이있는가?) 4. 이동 명령시행 or 무시
* 5. 현재위치 리턴 및 종료
*/
int[] n = { -1, 0 };
int[] s = { 1, 0 };
int[] e = { 0, 1 };
int[] w = { 0, -1 };
int location[] = new int[2];
int nextLocation[] = new int[2];
// 방위별 이동거리 맵핑
Map<Character, int[]> order = new HashMap<>() {
{
put('N', n);
put('S', s);
put('E', e);
put('W', w);
}
};
// 시작지점 찾기
for (int i = 0; i < routes.length; i++) {
if (park[i].contains("S")) {
location[0] = i;
location[1] = park[i].indexOf("S");
break;
}
}
for (int i = 0; i < routes.length; i++) {
nextLocation[0] = location[0];
nextLocation[1] = location[1];
// 이동 방향과 거리 찾아옴
int[] ordersLocation = order.get(routes[i].charAt(0));
char military = routes[i].charAt(0);
// 이동 방향 별 거리 이동 및 검증
switch (military) {
case 'N':
for (int j = 0; j < Character.getNumericValue(routes[i].charAt(2)); j++) {
// 공원밖으로 나가거나 자신의 이동 방향에 X 표시일경우 무시
if (nextLocation[0] - 1 < 0 || park[nextLocation[0] - 1].charAt(nextLocation[1]) == 'X') {
nextLocation[0] = location[0];
break;
} else {
nextLocation[0] += ordersLocation[0];
}
}
break;
case 'S':
for (int j = 0; j < Character.getNumericValue(routes[i].charAt(2)); j++) {
if (park.length <= nextLocation[0] + 1
|| park[nextLocation[0] + 1].charAt(nextLocation[1]) == 'X') {
nextLocation[0] = location[0];
break;
} else {
nextLocation[0] += ordersLocation[0];
}
}
break;
case 'E':
for (int j = 0; j < Character.getNumericValue(routes[i].charAt(2)); j++) {
if (park[nextLocation[0]].length() <= nextLocation[1] + 1
|| park[nextLocation[0]].charAt(nextLocation[1] + 1) == 'X') {
nextLocation[1] = location[1];
break;
} else {
nextLocation[1] += ordersLocation[1];
}
}
break;
case 'W':
for (int j = 0; j < Character.getNumericValue(routes[i].charAt(2)); j++) {
if (nextLocation[1] - 1 < 0 || park[nextLocation[0]].charAt(nextLocation[1] - 1) == 'X') {
nextLocation[1] = location[1];
break;
} else {
nextLocation[1] += ordersLocation[1];
}
}
break;
}
location[0] = nextLocation[0];
location[1] = nextLocation[1];
}
return location;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Solution s = new Solution();
String[] park = { "SOO", "OOO", "XOO" };
String[] routes = { "S 1" };
int[] a = s.solution(park, routes);
for (int item : a) {
System.out.println(item);
}
}
}
틀린답안 :
for (int j = 0; j < Character.getNumericValue(routes[i].charAt(2)); j++) {
if (nextLocation[0] <= -1 || nextLocation[0] <= -1) {
break;
}
if (park[nextLocation[0]].length() <= nextLocation[1] + 1
|| park[nextLocation[0]].charAt(nextLocation[1] + 1) == 'X') {
break;
} else if (park[nextLocation[0]].length() <= nextLocation[0] + 1
|| park[nextLocation[0] + 1].charAt(nextLocation[0]) == 'X') {
break;
} else {
nextLocation[0] = nextLocation[0] + ordersLocation[0];
nextLocation[1] = nextLocation[1] + ordersLocation[1];
}
location[0] = nextLocation[0];
location[1] = nextLocation[1];
}
- Content 컬럼의 길이가 15자
초과인 id 출력문제
답안 :
select tweet_id
from Tweets
where CHAR_LENGTH(content)>15
CHAR_LENGTH(): 해당 문자열의 길이를 리턴한다.
- EmployeeUNI 테이블과 Employees 테이블중에서 unique_id 와 name을 출력
id 가 없을경우NULL출력
답안 :
select e2.unique_id, e1.name
from Employees e1
left join EmployeeUNI e2
on e1.id = e2.id
- Sales 테이블과 Product 테이블에서 판매된 상품의 이름,년도,가격 출력
답안 :
select p1.product_name, s1.year, s1.price
from Sales s1
inner join Product p1
on s1.product_id = p1.product_id