public class Solution {
    public static int[] solution(int[] arr){
        int[] answer = new int[arr.length];
        int min = Integer.MAX_VALUE;
        int idx = 0;

        for(int i=0; i<arr.length; i++){
            min = Integer.MAX_VALUE;
            for(int j=i+1; j<arr.length; j++){
                if(arr[j] < min){
                    min = arr[j];
                    idx = j;
                }
            }

            // 교체
            int temp = arr[i];
            arr[i] = min;
            arr[idx] = temp;
        }

        for(int i=0; i<arr.length; i++){
            answer[i] = arr[i];
            System.out.println(answer[i]);
        }
        return answer;
    }

    public static void main(String[] args) {
        Solution T = new Solution();
        int[] arr = {13,5,11,7,23,15};
        System.out.println(T.solution(arr));
    }
}
public class Solution {
    public static int solution(int[] arr, int K){
        // 그냥 하면 오름차순으로 정렬되기 때문에 Collections.reverseOrder으로 내림차순 만들기
        TreeSet<Integer> Tset = new TreeSet<>(Collections.reverseOrder());
        int answer = 0;

        // 카드 3장 뽑기
        for(int i=0; i<arr.length; i++){
            for(int j=i+1; j<arr.length; j++){
                for(int z=j+1; z<arr.length; z++){
                    Tset.add(arr[i]+arr[j]+arr[z]);
                }
            }
        }

        int cnt = 0;
        for (int x: Tset) {
            cnt++;
            if(cnt==3){
                answer = x;
                break;
            }
        }

        return answer;
    }

    public static void main(String[] args) {
        Solution T = new Solution();
        int[] arr = {13,15,34,23,45,65,33,11,26,42};
        int K = 3;
        System.out.println(T.solution(arr, K));
    }
}

Set

- 순서가 없는 데이터의 집합

- 중복 허용 X

- HashSet, TreeSet

 

 

Map

- key, value가 한 쌍으로 이루어지는 데이터의 집합

- 순서가 없는 데이터의 집합

- key 중복 허용 X, value는 중복 허용

- HashMap, TreeMap, Hashtable, Properties

 

Set Map
value key : value
값 중복 X key 중복 X, value 허용
contains(value) containsKey(key)
get 불가능 get(key)

 

Hash

- 순서가 없다.

 

 

Tree

- 트리 구조로, 순서 유지

 

Hash Tree
순서 없음 순서 유지
O(1) O(log n)

 

HashMap vs HashSet vs TreeMap vs TreeSet

HashMap HashSet TreeMap TreeSet
key 중복 X value 허용 value 중복 X key 중복X value 허용 value 중복X
순서 없음 순서 없음 순서 유지 순서 유지

 

 

 

 

 

 

 

public class Solution {
    public static String solution(String str1, String str2){
        Queue<Character> queue = new LinkedList<>();
        String answer = "YES";

        for(int i=0; i< str1.length(); i++){
            char element = str1.charAt(i);
            queue.add(element);
        }

        for(int i=0; i<str2.length(); i++){
            char element = str2.charAt(i);
            if(queue.contains(element)){
                if(element!= queue.poll()){
                    answer = "NO";
                    break;
                }
            }
            if(!queue.isEmpty()){
                answer = "NO";
            }
        }
        return answer;
    }

    public static void main(String[] args) {
        Solution T = new Solution();
        String str1 = "CBA";
        String str2 = "CBDAGE";
        System.out.println(T.solution(str1,str2));
    }
}
import java.util.Stack;

public class Solution {
    public static int solution(String str){
        Stack<Integer> s = new Stack<>();
        int answer = 0;

        for(int i=0; i<str.length(); i++){
            char ch = str.charAt(i);
            if(Character.isDigit(ch)){ // 숫자일 경우
                s.add(ch-48);
            } else { // 숫자가 아닐 경우
                int rt = s.pop();
                int lt = s.pop();

                if(ch == '+'){
                    s.add(lt+rt);
                } else if(ch == '-'){
                    s.add(lt-rt);
                } else if(ch == '/'){
                    s.add(lt/rt);
                } else if(ch == '*'){
                    s.add(lt*rt);
                }
            }
        }

        answer = s.get(0); // 맨 앞의 결과가 연산의 결과가 된다.
        return answer;
    }

    public static void main(String[] args) {
        Solution T = new Solution();
        String str = "352+*9-";
        System.out.println(T.solution(str));
    }
}
import java.util.Stack;

public class Solution {
    public static int solution(int[][] board, int[] moves){
        Stack<Integer> stack = new Stack<>();
        int answer = 0;

        for(int i=0; i<moves.length; i++){
            int idx = moves[i]-1;
            for(int a=0; a<board.length; a++){
                if(board[a][idx]!=0){
                    int temp = board[a][idx];
                    board[a][idx] = 0;
                    if(!stack.isEmpty() && temp==stack.peek()){
                        answer+=2;
                        stack.pop();
                    } else {
                        stack.add(temp);
                    }
                    break;
                }
            }
        }
        return answer;
    }

    public static void main(String[] args) {
        Solution T = new Solution();
        int[][] 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}
        };

        int[] moves = {1,5,3,5,1,2,1,4};
        System.out.println(T.solution(board,moves));
    }
}
import java.util.Stack;

// 괄호가 입력되면 올바른 괄호는 YES 아니면 NO 출력
public class Solution {
    public static String solution(String str){
        Stack<Character> stack = new Stack<>();
        String answer = "YES";

        for(int i=0; i<str.length(); i++){
            char element = str.charAt(i);
            if(stack.isEmpty()){
                stack.add(element);
            } else {
                if(!stack.peek().equals(element)){
                    stack.pop();
                } else {
                    stack.add(element);
                }
            }
        }
        if(!stack.isEmpty()){
            answer = "NO";
        }
        return answer;
    }

    public static void main(String[] args) {
        Solution T = new Solution();
        System.out.println(T.solution("(()(()))(()"));
    }
}

 

 

import java.util.Stack;

public class Solution {
    public static String solution(String str){
        Stack<Character> stack = new Stack<>();
        String answer = "";

        for(int i=0; i<str.length(); i++){
            char element = str.charAt(i);

            if(stack.isEmpty()){
               stack.add(element);
            } else {
                if(element==')') {
                    while (stack.pop() != '(');
                } else {
                    stack.add(element);
                }
            }
        }

        for(int i=0; i<stack.size(); i++){
            answer += stack.get(i);
        }
        return answer;
    }

    public static void main(String[] args) {
        Solution T = new Solution();
        System.out.println(T.solution("(A(BC)D)EF(G(H)(IJ)K)LM(N)"));
    }
}

String(정수) to Int

- int num = Integer.parseInt(str);

- int num = Integer.valueOf(str);

 

Int to String

- String str = Integer.toString(i);

- String str = String.valueOf(i);

 

Char to Int

int num = (int)(ch-'0'); // 0은 48

 

Int to Char

- char ch = (char)(num+'0');

 

String to char

- String str = "A";

- char[] ch = str.toCharArray();

- ch[0]; // 'A'

 

Double to String

- String str = Double.toString(db);

 

String to Double

- double db = Double.valueOf(str).doubleValue();

 

long to String

- String str = Long.toString(l);

 

String to long 

- long l = Long.valueOf(str).longValue();

- long l = Long.parseLong(str);

 

float to String

- String str = Float.toString(f);

 

String to float

- float f = Float.valueOf(str).floatValue();

 

ASCII Code to String

- String str = new Character((char)num).toString();

 

String to ASCII Code

- int num = (int)ch;

 

10진수 -> 2진수

- String binaryStr = Integer.toBinaryString(num);

 

+ Recent posts