문제요약
그래고리력 날짜 date가 문자열로 주어진다.yyyy-mm-dd형태의 date를 2진수로 각 자리별로 2진수로 변환하여 반환해야한다.
예시
입력: date = "2080-02-29"
출력: "100000100000-10-11101"입력: 날짜 = "1900-01-01"
출력: "11101101100-1-1"
풀이
10진수에서 2진수로의 변환과정을 알아야한다.

- 숫자 NUM을 1이 남을때 까지 2로 나눈다.
- 마지막 몫 1과 계산과정에서의 나머지를 모두 합친다.
function convertBinary(num){
const stack = [];
while(num > 0){
stack.push(num%2);
num = Math.floor(num/2);
}
return stack.reverse().join("");
}
var convertDateToBinary = function(date) {
const [year, month, day] = date.split("-");
const y = convertBinary(Number(year))
const m = convertBinary(Number(month))
const d = convertBinary(Number(day));
return `${y}-${m}-${d}`
};
- date로 받은 문자열을 '-'를 기준으로 나눈다.
- 위에서 배운 2진수변환법을 이용하여 2진수로 변환한다.
- 모두 합친다.
시간복잡도 O(log N)
'코딩테스트' 카테고리의 다른 글
| LeetCode - Set Matrix Zeroes (1) | 2025.01.02 |
|---|---|
| LeetCode - Minimum Add to Make Parentheses Valid (1) | 2025.01.02 |
| LeetCode - Numberof Different Integer in a String (0) | 2024.12.30 |
| LeetCode - Restore IP Addresses (0) | 2024.12.28 |
| LeetCode - Path Sum (0) | 2024.12.25 |