매일 Algorithm

신재원·2023년 5월 8일
0

Algorithm

목록 보기
116/243

프로그래머스 (rny_string)

public class problem380 {
    class Solution {
        public String solution(String rny_string) {
            // rny_string 문자열에있는 모든 "m"을 "rn"으로 replace한다. 
            String answer = rny_string.replaceAll("m", "rn");
            return answer;
        }
    }
}

프로그래머스 (특정한 문자를 대문자로 바꾸기)

public class problem381 {
    class Solution {
        public String solution(String my_string, String alp) {
            String answer = "";
            String[] temp = my_string.split("");
            
            // temp 배열에서 값 비교
            for (int i = 0; i < temp.length; i++) {
                if (temp[i].equals(alp)) {
                    answer += temp[i].toUpperCase();
                } else {
                    answer += temp[i];
                }
            }
            return answer;
        }
    }
}

프로그래머스 (배열의 길이에 따라 다른 연산하기)

public class problem382 {
    class Solution {
        public int[] solution(int[] arr, int n) {
            int[] answer = new int[arr.length];

            // n이 짝수일경우
            // for문의 증감문을 신경써주면서 작성해줘야한다.
            if(arr.length % 2 == 0){
                for(int i = 1; i < arr.length; i+=2){
                    answer[i] = arr[i] + n;
                }
                for(int i = 0; i < arr.length; i+=2){
                    answer[i] = arr[i];
                }
            }
            // n이 홀수일경우
            else{
                for(int i = 1; i < arr.length; i+=2){
                    answer[i] = arr[i];
                }
                for(int i = 0; i < arr.length; i+=2){
                    answer[i] = arr[i] + n;
                }
            }
            return answer;
        }
    }
}

프로그래머스 (글자 이어 붙여 문자열 만들기)

public class problem383 {
    class Solution {
        public String solution(String my_string, int[] index_list) {
            String answer = "";

            String[] temp = my_string.split("");
            for(int i = 0; i < index_list.length; i++){
                // index_list 배열의 index값을 담아준다.
                answer += temp[index_list[i]];
            }

            return answer;
        }
    }
}

프로그래머스 (문자열 바꿔서 찾기)

public class problem384 {
    class Solution {
        public int solution(String myString, String pat) {
            int answer = 0;
            String[] temp = myString.split("");
            String result = "";
            for (int i = 0; i < temp.length; i++) {

                // temp 배열에서 "A"가 있을경우, "B"로 치환
                if (temp[i].equals("A")) {
                    result += temp[i].replace(temp[i], "B");
                }
                // temp 배열에서 "B"가 있을경우, "A"로 치환
                else {
                    result += temp[i].replace(temp[i], "A");
                }
            }

            if (result.contains(pat)) {
                answer = 1;
            } else {
                answer = 0;
            }

            return answer;
        }
    }
}

0개의 댓글