본문 바로가기
코딩테스트/programmers

프로그래머스 [#7] 코딩테스트 연습 JAVA - 7

by 8Antony 2023. 1. 5.

 

 

1. 문자열 내 마음대로 정렬하기 

 

 

 

 

https://school.programmers.co.kr/learn/courses/30/lessons/12915?language=java

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

 

 

 

 

 

import java.util.*;

class Solution {
    public String[] solution(String[] strings, int n) {
        String[] answer = new String[strings.length];
        
        Arrays.sort(strings); //정렬 
        
        ArrayList<String> arr = new ArrayList<>();
        
        for (int i=0; i<strings.length; i++) {
            arr.add(strings[i].charAt(n) + "" + strings[i]);
        } //n번째 글자 마지막 글자 앞에 붙이기 car -> acar, mom -> omom
        
        arr.sort(null); //정렬 
        
        for (int i=0; i<arr.size(); i++) {
            answer[i] = arr.get(i).substring(1, arr.get(i).length());
        }  // 앞에 붙인거 자르기 
        return answer;
    }
}

 

 

 

2. K 번째 수 

 

 

https://school.programmers.co.kr/learn/courses/30/lessons/42748/?language=java# 

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

 

 

 

 

 

import java.util.*;

class Solution {
    public int[] solution(int[] array, int[][] commands) {
        int[] answer = new int[commands.length]; //정답 배열 생성 
        
        for(int i=0;i<commands.length;i++){
            
            int[] temp = new int[commands[i][1]-(commands[i][0]-1)];
            // new int[4] // new int[1] // new int[7]
            for(int j= 0; j < temp.length; j++){ // j = 2 ~ 5 / j = 4 ~ 4 / j= 1 ~ 7
                temp[j] = array[j+(commands[i][0]-1)];
            // {5,2,6,3} / {6} / {1,5,2,6,3,7,4}
            }
            Arrays.sort(temp); // 오름차순 정렬 {2,3,5,6} / {6} / {1,2,3,4,5,6,7}
            answer[i] = temp[commands[i][2]-1]; // {5,6,3}
 
        }
        return answer;
    }
}

 

 

 

3. 숫자 문자열과 영단어 

 

 

 

https://school.programmers.co.kr/learn/courses/30/lessons/81301/?language=java# 

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

 

 

 

 

 

import java.util.*;

class Solution {
    public int solution(String s) {
        int answer = 0;
        
        String[] word = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
        String[] num = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
        
        for (int i=0; i<10; i++) {
            s = s.replace(word[i], num[i]); //문자를 숫자로 -> 
        }   
        
        answer = Integer.parseInt(s);
        return answer;
    }
}

 

 



4. 두개 뽑아서 더하기 

 

 

 

https://school.programmers.co.kr/learn/courses/30/lessons/68644/?language=java#

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

 

 

 

 

 

 

import java.util.*;

class Solution {
    public Integer[] solution(int[] numbers) {
        
        Set<Integer> set = new HashSet<>(); 
        //중복 배열을 제거하기 위해 set에 넣음 
        
        for(int i = 0; i < numbers.length - 1; ++i){
            for(int j = i+1; j < numbers.length; ++j){
                set.add(numbers[i] + numbers[j]);
            }
        }
        
        Integer[] arr = set.toArray(new Integer[0]);
        Arrays.sort(arr);
        return arr;
    }
}

   

 

 

 

 


 

 

Appendix

 

1. 리스트 정렬은 collections.sort(arr), 어레이 정렬은  Arrays.sort(arr);

 

2. String 자르기는 substring 

 

3. ArrayList 란 ? 

 

 

 

4. LinkedList 란 ?

 

 

 

 

댓글