본문 바로가기

[JAVA] 백준 1173 - 운동 import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); // N분 int m = sc.nextInt(); // 초기 맥박 int M = sc.nextInt(); // 최대 맥박 int T = sc.nextInt(); // 1분당 T만큼 맥박 증가 int R = sc.nextInt(); // 1분당 R만큼 맥박 감소 int res = 0; // 결과 int move = 0; // 운동 한 시간 int init_m = m; // 초기 맥박 while(move != N) { res++; // 시간 증가 // 운동 후 ..
[JAVA] LeetCode 268 - Missing Number Given an array nums containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array. [0, n] 범위의 고유 숫자 n개를 포함하는 배열이 주어지면, 배열에서 누락된 숫자를 반환하라. 풀이 방법 1. 배열의 길이만큼 반복문을 수행하며 0부터 n까지의 합과, 배열의 요소의 합을 구한다. 2. 두 수의 차를 반환한다. (두 수의 차는 누락된 수이므로) class Solution { public int missingNumber(int[] nums) { int sum = 0, arr = 0; for(int i=0; i
[JAVA] LeetCode 387 - First Unique Character in a String Given a string, find the first non-repeating character in it and return its index. If it doesn't exist, return -1. 문자열이 주어지면, 반복되지 않는 가장 첫번째 문자 찾아 인덱스를 반환하라. 만약 존재하지 않는다면, -1을 반환하라. 풀이 방법 1. 총 26개 알파벳의 카운트 값을 담을 배열 alpha를 선언한다. 2. 문자열의 길이만큼 반복문을 수행하며, 한 글자씩 파싱한다. 3. 파싱된 문자의 아스키코드 값 - 97을 하여, 해당 알파벳의 카운팅을 수행한다. 4. 다시 문자열의 길이만큼 반복문을 수행하며, 카운트가 1인 알파벳의 인덱스를 반환한다. 5. 카운트가 1인 알파벳이 없을 경우, 반복문을 마친 후 -1..
[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