728x90
 

프로그래머스

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

programmers.co.kr

 

 

Language: Java

1
2
3
4
5
6
7
8
9
10
class Solution {
    public int solution(int n) {
        int answer = 0;
        for(int i=1; i<=n; i++){
            if(n%i==0) answer += i;
        }
        return answer;
    }
}
 

* 1부터n까지 약수 확인

1
2
3
4
5
6
7
8
9
10
class Solution {
    public int solution(int n) {
        int answer = 0;
        for(int i=1; i<=n/2; i++){
            if(n%i==0) answer += i;
        }
        return answer + n;
    }
}
 

* 1부터 n/2까지 약수 확인

    -  n의 절반까지만 반복하는 이유: n의 절반을 넘어가면 더 이상 n의 약수가 될 수 없음

    - 1을 제외한 최소 약수는 2, 3, 4 ...인데, 약수는 대칭구조를 이루므로 n/2보다 클 수 없음

* answer + n

    - 자기 자신은 자기 자신의 약수이므로 return값에 추가

 

 

 

🔗 소스 코드
GitHub

 

📚 참고 자료

 

프로그래머스

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

programmers.co.kr

 

728x90
728x90
 

프로그래머스

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

programmers.co.kr

 

Language: Java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
class Solution {
    public String solution(String s, int n) {
        String answer = "";
        StringBuilder sb = new StringBuilder();
        for(int i=0; i<s.length(); i++){
            if(s.charAt(i)>='A' && s.charAt(i)<='Z'){
                sb.append(upperCase(s.charAt(i), n));
            }
            if(s.charAt(i)>='a' && s.charAt(i)<='z'){
                sb.append(lowerCase(s.charAt(i), n));
            }
            if(s.charAt(i)==' '){
                sb.append(" ");
            }
        }
        answer = sb.toString();
        
        return answer;
    }
    
    public char upperCase(char c, int n){
        char result = (char) (c + n);
        if(result > 'Z') result -= 26;
        return result;
    }
    
    public char lowerCase(char c, int n){
        char result = (char) (c + n);
        if(result > 'z') result -= 26;
        return result;
    }
    
}
 

* 알고리즘

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
    public String solution(String s, int n) {
        String answer = "";
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if (Character.isLowerCase(ch)) {
                ch = (char) ((ch - 'a' + n) % 26 + 'a');
            }
            if (Character.isUpperCase(ch)) {
                ch = (char) ((ch - 'A' + n) % 26 + 'A');
            }
            answer += ch;
        }
        
        return answer;
        
    }
}
 

* Character 내장 함수

- (ch - 'a' + n) % 26 + 'a'

    - 0~25 사이의 위치를 계산후, 소문자 기준으로 위치 변환

 

 

 

🔗 소스 코드
GitHub

 

📚 참고 자료

 

프로그래머스

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

programmers.co.kr

 

728x90
728x90
 

프로그래머스

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

programmers.co.kr

 

Language: Java

1
2
3
4
5
6
7
8
class Solution {
    public int solution(String s) {
        int answer = 0;
        answer = Integer.parseInt(s);
        return answer;
    }
}
 

* parseInt

 

1
2
3
4
5
6
7
8
class Solution {
    public int solution(String s) {
        int answer = 0;
        answer = Integer.valueOf(s);
        return answer;
    }
}
 

* valueOf

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
class Solution {
    public int solution(String s) {
        int answer = 0;
        for(int i=0; i<s.length(); i++){
            if(s.charAt(i) >= 48 && s.charAt(i) <= 57){
                answer += s.charAt(i)-48;
                if(i==s.length()-1break;
                answer *= 10;                
            }
        }
        if(s.charAt(0)=='-') answer *=-1;
        return answer;
    }
}
 

* 알고리즘

 

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
    public int solution(String s) {
        int answer = 0;
        for(int i=0; i<s.length(); i++){
            if(s.charAt(i) >= 48 && s.charAt(i) <= 57){
                answer = answer*10 + s.charAt(i)-48;
            }
        }
        if(s.charAt(0)=='-') answer *=-1;
        return answer;
    }
}
 

* 알고리즘

    - *10 위치 조정 및 if 조건문 제거

 

 

 

🔗 소스 코드
GitHub

 

📚 참고 자료

 

프로그래머스

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

programmers.co.kr

 

728x90
728x90
 

프로그래머스

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

programmers.co.kr

 

Language: Java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class Solution {
    public String solution(int n) {
        String answer = "";
        StringBuilder sb = new StringBuilder();
        while(n-->0){
            if(n%2==0){
                sb.append("수");
            } else {
                sb.append("박");
            }
        }
        answer = sb.reverse().toString();
        return answer;
    }
}
 

* sb.reverse().toString()

    - StringBuilder 내장 메서드 사용

 

 

 

🔗 소스 코드
GitHub

 

728x90
728x90
 

프로그래머스

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

programmers.co.kr

 

Language: Java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Solution {
    public int solution(int n) {
        int answer = 0;
        
        for(int i=2; i<=n; i++){
            answer++;
            for(int j=2; j<=Math.sqrt(i); j++){
                if(i%j==0){
                    answer--;
                    break;
                }
            }
            
        }
        
        return answer;
    }
}
 
 
 

* for(int j=2; j<=Math.sqrt(i); j++)

    - 제곱근까지 확인

 

 

 

🔗 소스 코드
GitHub

 

728x90