[프로그래머스/Lv.1] 개인정보 수집 유효기간 (Java)
·
✏️/Programmers
개인정보 수집 유효기간https://school.programmers.co.kr/learn/courses/30/lessons/150370풀이한 달 = 28일 -> 모든 날짜를 일(day)로 변환해서 계산static int convert(String date) { String[] d = date.split("\\."); int y = Integer.parseInt(d[0]); int m = Integer.parseInt(d[1]); int day = Integer.parseInt(d[2]); return y * 12 * 28 + m * 28 + day;} `terms`를 `HashMap`에 저장Map HM = new HashMap();for (String t : terms) { ..
[프로그래머스/Lv.1] 성격 유형 검사하기 (Java)
·
✏️/Programmers
성격 유형 검사하기https://school.programmers.co.kr/learn/courses/30/lessons/118666풀이`survey` + `score``choice`: 1 -> 앞 문자 +3`choice`: 2 -> 앞 문자 +2`choice`: 3 -> 앞 문자 +1`choice`: 4 -> 0`choice`: 5 -> 뒤 문자 +1`choice`: 6 -> 뒤 문자 +2`choice`: 7 -> 뒤 문자 +3ex. survey = "AN" / choice = 2 -> 앞 문자(R) +2점for (int i = 0; i 코드import java.util.*;class Solution { public String solution(String[] survey, int[] choice..
[프로그래머스/Lv.1] 비밀지도 (Java)
·
✏️/Programmers
비밀지도https://school.programmers.co.kr/learn/courses/30/lessons/17681풀이`map = arr1[i] | arr2[i]`: 둘 중 하나라도 1이면 1-> ex. arr1[i] = 9(10진수) = 1001(2진수) 컴퓨터 안에서는 항상 이진수로 저장 `Integer.toBinaryString(map)`으로 이진수 변환하면 앞의 0 자동으로 빠짐-> `String.format("%" + n + "s", binary)`로 공백 채우기(길이가 n이 될 때까지 왼쪽에 공백 채워서 문자열 맞추기) 1 -> '#' / 0 -> ' '로 벽 변환코드class Solution { public String[] solution(int n, int[] arr1, int[..
[프로그래머스/Lv.1] 크레인 인형뽑기 (Java)
·
✏️/Programmers
크레인 인형뽑기https://school.programmers.co.kr/learn/courses/30/lessons/64061풀이해당 열에서 위에서부터 인형 찾기`moves`: 크레인이 이동하는 열 번호`col = m - 1` => 0-based 인덱스로 변환 `board[row][col] != 0`: 인형 발견 `borad[row][col] = 0`: 인형 뽑기바구니 맨 위 인형같음 -> 2개 사라짐: `stack.poll()`, `cnt += 2`다름 -> `stack.push(doll)`예시board = [[0,0,0,0,0],[0,0,1,0,3],[0,2,5,0,1],[4,2,4,4,2],[3,5,1,3,1]] col: 1 2 3 4 5-------------------row0 | 0 ..
[프로그래머스/Lv.1] 키패드 누르기 (Java)
·
✏️/Programmers
키패드 누르기풀이Pos 클래스좌표 + 거리 계산(맨해튼 거리)static class Pos { int x, y; Pos(int x, int y) { this.x = x; this.y = y; } int dist(Pos p) { return Math.abs(this.x - p.x) + Math.abs(this.y - p.y); }} 숫자 -> 키패드 좌표 변환`(n - 1) / 3`: 행`(n - 1) % 3`: 열(0,0) (0,1) (0,2) 1 2 3(1,0) (1,1) (1,2) 4 5 6(2,0) (2,1) (2,2) 7 8 9(3,0) (3,1) (3,2) * ..
[프로그래머스/Lv.1] 문자열 내 마음대로 정렬하기 (Java)
·
✏️/Programmers
문자열 내 마음대로 정렬하기https://school.programmers.co.kr/learn/courses/30/lessons/12915풀이객체 비교Comparable (자기 자신 기준)클래스 정렬 기준 정하고 싶을 때 사용클래스 내부에 구현정렬 기준 1개`compareTo` 메서드 사용-> 재정의(Override) 해야 함class Person implements Comparable { int age; Person(int age) { this.age = age; } @Override public int compareTo(Person o) { return this.age - o.age; // 나이 오름차순 }}Arrays.sort(persons..
kimmeoww
'분류 전체보기' 카테고리의 글 목록