본문 바로가기

[JAVA] LeetCode 118 - Pascal's Triangle Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. In Pascal's triangle, each number is the sum of the two numbers directly above it. 음수가 아닌 정수 numRows가 주어지면, 파스칼 삼각형을 만들어라. 파스칼 삼각형에서 각 숫자는 그 바로 위에 있는 두 숫자의 합이다. 풀이 방법 문제 그대로 2중 반복문으로 파스칼 삼각형을 만들면 된다. 핵심 풀이 방법은 다음과 같다. 1. 현재 입력해야 하는 인덱스가 라인의 양 끝일 경우 1을 입력한다. 2. 라인의 양 끝이 아닐 경우, 값을 구하기 위해 이전 라인에 있던 리스트 객체를 받아온다. ..
[JAVA] 백준 1159 - 농구 경기 import java.util.*; public class Main { public static void main(String[] args) { int[] alpha = new int[26]; ArrayList al = new ArrayList(); Scanner sc = new Scanner(System.in); int cnt = sc.nextInt(); for(int i=0; i= 5) { if(!al.contains((char)(index+97))) { al.add((char)(index+97)); } } } if(al.size() == 0) { // 같은 성을 가진 사람이 5명이 안될 경우 System.out.print("PREDAJA"); } else { // 같은 성을 가진 사람이 5명 이상일..
[JAVA] 백준 1152 - 단어의 개수 import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s = sc.nextLine(); String[] parse = s.split(" "); int cnt = 0; for(int i=0; i 0) cnt++; } System.out.println(cnt); } }
[JAVA] 백준 1100 - 하얀 칸 public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); boolean status = true; int count = 0; for(int i=0; i
[JAVA] 백준 1076 - 저항 코드 돌아가는데 엄청난 시간이 걸렸다 -_ㅠ public class Main { enum Color { black, brown, red, orange, yellow, green, blue, violet, grey, white; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); int res = 0; for(int i=1; i
[JAVA] 백준 1075 - 나누기 public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); int F = sc.nextInt(); N = (N/100) * 100; // 마지막 두자리 00으로 바꾸기 while(N%F != 0) // 나누어 떨어지는 수 찾기 N++; N %= 100; if(N < 10) System.out.println("0" + N); else System.out.println(N); } }
[JAVA] LeetCode 21 - Merge Two Sorted Lists Merge two sorted linked lists and return it as a new sorted list. The new list should be made by splicing together the nodes of the first two lists. Example 1: 두 개의 정렬된 연결 리스트를 병합하여 하나의 새로운 정렬된 리스트로 반환하라. 새로운 리스트는 주어진 두 개의 연결 리스트 노드를 함께 분할하여 만들어야 한다. 풀이 방법 1. 루트 노드와 초기에 생성한 루트 노드의 주소를 가지는 head 노드를 만든다. 2. 주어진 연결 리스트(l1,l2)가 모두 null이 될 때까지(마지막 노드까지 탐색), 반복문을 수행한다. 3. val1과 val2에 각각 현재 리스트(l1, l2)..
[JAVA] LeetCode 13 - Roman to Integer Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M. Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. Symbol Value I 1 V 5..