9월 29일 개인공부

안효빈·2022년 9월 29일
0

개인 공부

목록 보기
21/36

1. 코테쪼금.

프로그래머스 코테 [정수 내림차순으로 배치하기]

class Solution {
    public long solution(long n) {
        
       String str = "" + n;
        
        long[] iarr = new long[str.length()];
        
        int b = str.length();
        
        for(int i = 0; i < b; i++){
            iarr[i] = n % 10;
            n /= 10;
        }
        
        for(int j = b; j > 0; j--){
            for(int i = 0; i < b-1; i++){
                if(iarr[i] < iarr[i+1]){
                    long a;
                    a = iarr[i];
                    iarr[i] = iarr[i+1];
                    iarr[i+1] = a;
                    if(i == b){
                        break;
                    }
                }
            }
        }
        
        StringBuilder sb = new StringBuilder("");
        
        for(int i = 0; i < b; i++) {
        	sb.append(iarr[i]);
        }
        
        String sbsb = sb.toString();
        
	    
        long answer = Long.parseLong(sbsb);
        
        
        
        return answer;
    }
}

+1점 ㅅㅂ

복습할 점 : Integer.ParseInt(String @#)을 쓰면 문자열을 숫자로 바꿔줌


프로그래머스 코테 [문자열을 정수로 바꾸기]

class Solution {
    public int solution(String s) {
        int answer = 0;
        
        answer = Integer.parseInt(s);
		
        
        return answer;
    }
}

아까 배운 ParseInt 한줄쓰니까 됨

+1점


프로그래머스 코테 x만큼 간격이 있는 n개의 숫자

class Solution {
    public long[] solution(int x, int n) {
        
		long answer[] = new long[n];
		
		long x1 = (int)x;
        long n1 = (int)n;
		
		
        for(int i = 0; i < n; i++) {
        	answer[i] = x1*(i+1);
        }
        
        return answer;
    }
}

+5점


프로그래머스 코테 나머지가 1이 되는 수 찾기

class Solution {
    public int solution(int n) {
        int answer = 2;
		
		while(n % answer != 1) {
			answer++;
		}
        
        return answer;
    }
}

+2점
점수 많이 못받았을땐 남의 풀이를 나중에 한번 보는게 나을거같음


프로그래머스 코테 두 정수 사이의 합

class Solution {
    public long solution(int a, int b) {
        long answer = 0;
        long sum = 0;
        long a1 = (long)a;
        long b1 = (long)b;
        
        long c = (b1-a1);
        
        if(c < 0){
            c *= -1;
            c++;
        }else {
        	c++;
        }
        
        for(long i = 1; i <= c; i++) {
        	if(a<b) {sum += (a+i-1);}
        	else {sum += (b+i-1);}
        }
        
        answer = sum;
        
        return answer;
    }
}

+3점

복습할 점 : 변수 타입을 잘 봐야함. 항상 테케를 크게도 잡아보고 생각.


프로그래머스 코테 서울에서 김서방 찾기

class Solution {
    public String solution(String[] seoul) {
		String answer = "";
        int t = seoul.length;
        int x = 0;
        for(int i = 0; i < t; i++){
            if(seoul[i].equals("Kim")){
                answer = "김서방은 "+i+"에 있다"; 
                break;}
        }
        
        
        return answer;
    }
}

복습할 점 : String을 비교할때 equals() ==> 내용 자체를 비교, ==는 주소값을 비교
length쓸때 length()는 문자열의 길이를 받는 것,
저거말고 length로 쓸것(배열길이 반환)


프로그래머스 코테 핸드폰 번호 가리기

class Solution {
    public String solution(String phone_number) {
        String answer = "";
		
		int c = phone_number.length();
		
		char[] arr = new char[c];
		
		for(int i = 0; i<c; i++) {
			arr[i] = phone_number.charAt(i);
		}
        
		char f1 = arr[c-1];
		char f2 = arr[c-2];
		char f3 = arr[c-3];
		char f4 = arr[c-4];
		
        for(int i = 0; i < c; i++) {
        	arr[i] = '*';
        }
		
        arr[c-1] = f1;
        arr[c-2] = f2;
		arr[c-3] = f3;
		arr[c-4] = f4;
        
		for(int i = 0; i<c; i++) {
			answer += arr[i];
		}
        
        
        return answer;
    }
}

+1점

속도가 느려서 별로 안나오는것 같음

class Solution {
    public String solution(String phone_number) {
        String answer = "";
		
		int c = phone_number.length();
		
		char[] arr = new char[c];
		
		for(int i = 0; i<c; i++) {
			arr[i] = phone_number.charAt(i);
		}
        
		char f1 = arr[c-1];
		char f2 = arr[c-2];
		char f3 = arr[c-3];
		char f4 = arr[c-4];
		
        for(int i = 0; i < c; i++) {
        	arr[i] = '*';
        }
		
        arr[c-1] = f1;
        arr[c-2] = f2;
		arr[c-3] = f3;
		arr[c-4] = f4;
        
		StringBuffer sb = new StringBuffer();
		
		for(int i = 0; i<c; i++) {
			sb.append(arr[i]);
		}
		answer = sb.toString();
        
        return answer;
    }
}

StringBuffer로 받아서 했더니 속도는 압도적으로 오름

근데 점수는 더 안줌

나중에 다른 계정으로 복붙했을떄 1점 주는지 확인해봐야할듯


프로그래머스 코테 나누어 떨어지는 숫자 배열

ArrayList arr1 = new ArrayList();
        
       
        
		for(int i = 0; i < arr.length; i++) {
			if(arr[i] % divisor == 0) {
				arr1.add(arr[i]);
			}
		}
		
		if(arr1.size() <= 0) {
			arr1.add(-1);
		}
		
		
		
		int[] answer = new int[arr1.size()];
		
		for(int i = 0; i < arr1.size(); i++) {
			answer[i] = (int) arr1.get(i);
		}
		
		if(arr1.size() == 0) {
			answer[0] = -1;
		}
		
        
        
        
//		이제 answer를 오름차순으로만 해주면 됨
		
		for(int i = 0; i < answer.length; i++) {
			
			for(int j = 0; j < answer.length - i - 1; j++) {
				
				if(answer[j]>answer[j+1]) {
					int a = answer[j];
					answer[j] = answer[j+1];
					answer[j+1] = a;
				}
				
			}
			
			
		}

+2점 ㅅㅂ

복습할 점 : 오름차순 이중포문으로 만드는거 좀 더 해봐야할듯
ArrayList, Array 개수 신경쓰면서 할것


  1. 자바스크립트쪼금할수있으면 집가서
profile
다들 화이팅

0개의 댓글