import java.util.LinkedList; import java.util.Queue; Queue queue = new LinkedList(); Queue queue = new LinkedList(); //값 추가 Queue queue = new LinkedList(); queue.add(1); queue.add(2); queue.add(3); queue.add(4); //첫번째 값 반환 queue.peek(); //첫번째 값 반환 후 제거 queue.poll(); queue.remove(); //모든 요소 제거 queue.clear();
import java.util.Stack; Stack stack = new Stack(); Stack stack = new Stack(); //값 추가 Stack stack = new Stack(); stack.push(1); stack.push(2); stack.push(3); stack.push(4); //값 제거 stack.pop(); //전체 값 제거 stack.clear(); //스택 크기 출력 stack.size(); //스택에 요소가 있는지 확인 stack.contains(1); //스택이 비어있는지 확인 stack.empty();
//선언만 int[] arr; //선언과 동시에 크기 할당 int[] arr = new int[5]; String[] arr = new String[5]; //선언과 동시에 값 초기화 int[] arr = {1,2,3,4,5}; String[] arr = {"ab","cd","ef","gh","i"};
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int score = Integer.parseInt(br.readLine()); switch(score/10){ case 9: case 10: System.out.println("A"); break; case 8: System.out.println("B"); break;..
N = int(input()) for i in range(1, N+1): print("*"*i) for i in reversed(range(N)): print("*"*i) import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int n = Integer.parseInt(br.readLine()); for(int i=1; i
sum = 0 for _ in range(5): score = int(input()) if score < 40: score = 40 sum += score print(int(sum/5)) import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); int sum = 0; for( int i=0; i
x = int(input()) y = int(input()) if x>0 and y>0: print(1) elif x0: print(2) elif x0) System.out.println(1); else if (x0) System.out.println(2); else if (x
첫번째 코드 - 60ms import sys T = int(input()) for _ in range(T): line = sys.stdin.readline() bracket =[] cnt = 0 for ch in line: if cnt 0: bracket.pop() cnt -= 1 else: cnt = -1 break if cnt == 0: print("YES") else: print("NO") 처음에는 단순히 스택만 생각해서 리스트를 만들고 append하고 pop하는 방식으로 구현했는데 뭔가 코드가 더러워져서 다시 짰다. 코드 길이도 훨씬 짧고 시간도 훨씬..
K = int(input()) list = [] for _ in range(K): n = int(input()) if n == 0: if len(list)>0: list.pop() else: list.append(n) print(sum(list)) import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Stack; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(S..
import sys N = int(input()) stack = [] for _ in range(N): line = sys.stdin.readline().split() if line[0] == "push": stack.append(int(line[1])) elif line[0] == "pop": if len(stack)>0: print(stack[-1]) stack.pop() else: print(-1) elif line[0] == "size": print(len(stack)) elif line[0] == "empty": if len(stack) == 0: print(1) else: print(0) elif line[0] == "top": if len(stack) > 0: print(stack[-1]) ..