10월 4일 개인공부

안효빈·2022년 10월 4일
0

개인 공부

목록 보기
24/36

프로그래머스 코테 [같은 숫자는 싫어]

import java.util.*;

public class Solution {
    public int[] solution(int []arr) {
		
        ArrayList<Integer> an = new ArrayList<Integer>();
		
        for(int i = 0; i < arr.length-1; i++) {
        	
        	if(arr[i] == arr[i+1]) {
        		continue;
        	}else {
        		an.add(arr[i]);
        	}
        	
        }
        
        if(arr[arr.length-2] == arr[arr.length-1]) {
        	an.add(arr[arr.length-1]);
        }else if(arr[arr.length-2] != arr[arr.length-1]) {
        	an.add(arr[arr.length-1]);
        }
        
        
        int[] answer = new int[an.size()];
        
        for(int i = 0; i < an.size(); i++){
            answer[i] = an.get(i);
        }
        
        return answer;
    }
}

+3점


StringTokenizer와 Split메서드의 비교

[참고자료] :
https://blog.naver.com/PostView.nhn?blogId=makga87&logNo=221949199317&parentCategoryNo=&categoryNo=17&viewDate=&isShowPopularPosts=true&from=search

되게 유익한 글인데 대부분의 코테에선 토크나이저가 더 빠를거같음


프로그래머스 코테 [이상한 문자 만들기]

import java.util.*;
class Solution {
    public String solution(String s) {
        
        ArrayList sibal = new ArrayList();
		String answer = "";
        String[] arr = s.split("");
        
        for(int i = 0; i < arr.length; i++) {
        	
        	System.out.println(arr[i]);
        	
        }
        
        int a = 0;
        
        for(int i = 0; i < arr.length; i++){
            if(arr[i].equals(" ")){
                a = 0;
            }
            else if(a % 2 == 0){
            	arr[i] = arr[i].toUpperCase();
                a++;
            }
            else if(a % 2 == 1){
            	arr[i] = arr[i].toLowerCase();
                a++;
            }
            answer += arr[i];
        }
        
        return answer;
    }
}

[참고] : https://itprogramming119.tistory.com/entry/JAVA38-%EB%AC%B8%EC%9E%90%EC%97%B4%EC%9D%84-%EC%86%8C%EB%AC%B8%EC%9E%90-%EB%8C%80%EB%AC%B8%EC%9E%90%EB%A1%9C-%EB%B3%80%ED%99%98%EC%8B%9C%EC%BC%9C%EC%A3%BC%EB%8A%94-toLoweCase%EC%99%80-toUpperCase-%EB%A9%94%EC%86%8C%EB%93%9C-String-%ED%81%B4%EB%9E%98%EC%8A%A4

===> 문자열을 대문자와 소문자로 바꿔주는 메소드

+11점

자꾸 틀리는 이유가 뭔가 했는데

테케에 애초에 대문자가 박혀서 오는 경우를 생각 안함


프로그래머스 코테 [3진법 뒤집기]

import java.util.ArrayList;
class Solution {
    public int solution(int n) {
        int answer = 0;
		int a = n % 3;
		int b = 1;
		
		ArrayList<Integer> an = new ArrayList<Integer>();
		ArrayList<Integer> rev_an = new ArrayList<Integer>();
		
		while(n/(Math.pow(3, b)) > 1) {
			
			b++;
			
		}
		
		if(n == (Math.pow(3, b))){
			
		}else {
			
			b -= 1;
			
		}
		
		for(int i = b; i > -1; i--) {
			
			an.add((int)(n/Math.pow(3, i)));
			n = (int)(n % Math.pow(3, i));
			
		}
		
		// System.out.println(an);
		
		for(int i = an.size()-1; i > -1; i--) {
			
			rev_an.add(an.get(i));
			
		}
		
		// System.out.println(rev_an);
		
		for(int i = 0; i < rev_an.size(); i++) {
			answer += (Math.pow(3, i) * rev_an.get(rev_an.size()-1-i));
			
		}
        return answer;
    }
}

+3점

Math.Pow(a,b) ==> a의 b제곱
따로 import안해도 굴러감


profile
다들 화이팅

0개의 댓글