https://programmers.co.kr/learn/courses/30/lessons/12933

 

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ฝ”๋“œ ์ค‘์‹ฌ์˜ ๊ฐœ๋ฐœ์ž ์ฑ„์šฉ. ์Šคํƒ ๊ธฐ๋ฐ˜์˜ ํฌ์ง€์…˜ ๋งค์นญ. ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค์˜ ๊ฐœ๋ฐœ์ž ๋งž์ถคํ˜• ํ”„๋กœํ•„์„ ๋“ฑ๋กํ•˜๊ณ , ๋‚˜์™€ ๊ธฐ์ˆ  ๊ถํ•ฉ์ด ์ž˜ ๋งž๋Š” ๊ธฐ์—…๋“ค์„ ๋งค์นญ ๋ฐ›์œผ์„ธ์š”.

programmers.co.kr

 

 

<  ์ •์ˆ˜ ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ๋ฐฐ์น˜ํ•˜๊ธฐ  >

 

โ—‹ ๋‚ด๊ฐ€ ํ’€์–ด๋ณธ ํ’€์ด

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import java.util.*;
class Solution {
  public long solution(long n) {
      long answer;
      String n_temp="";
      n_temp+=n;
      char[] nc_temp = n_temp.toCharArray();
      Arrays.sort(nc_temp);
      n_temp="";
      for(int i =nc_temp.length-1 ; i>=0 ;i--){
          n_temp+=nc_temp[i];
      }
      answer= Long.parseLong(n_temp);    
      
      return answer;
  }
}
cs

 

์ƒ๊ฐ๊ณผ์ • )

1. long ํ˜•ํƒœ์˜ n ์˜ ์ž๋ฆฟ์ˆ˜๋ฅผ ํ•˜๋‚˜ํ•˜๋‚˜ ๋„์–ด ์ค˜์•ผํ•œ๋‹ค →String์œผ๋กœ ๋ณ€ํ™˜

2. ์ •๋ ฌ ํ•ด์•ผํ•˜๊ธฐ ๋•Œ๋ฌธ์— String ์„ char[]๋กœ ๋ณ€ํ™˜

3. ์ •๋ ฌ ( ?? ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๋Š” ๋ฒ•์€ ๋ญ์ง€?)

4. ์ •๋ ฌํ•œ ๋‹ค์Œ ๋’ค์— ๊ฐ’๋ถ€ํ„ฐ ๋”ํ•ด์„œ String์œผ๋กœ ๋ณ€ํ™˜

5. String ํ˜•ํƒœ์˜ n_temp๋ฅผ Long.parseLong()๋กœ Longํ˜•์œผ๋กœ ๋ณ€ํ™˜ ( โ€ป ๋งจ์ฒ˜์Œ Integer.parseInt()๋กœ ํ–ˆ๋‹ค๊ฐ€ ์‹คํŒจ๊ฐ€ ๋–ด๋‹ค )  

→ ์ค‘๊ฐ„์— n_temp์— ์ด๋ฏธ ๋ฌธ์ž์—ด์ด ๋“ค์–ด ์žˆ์œผ๋ฏ€๋กœ n_temp=""; ํ•ด์ค˜์–ด์•ผํ•œ๋‹ค.

 

โ—‹ ์ฐธ๊ณ ํ•  ํ’€์ด & ์ƒ๊ฐ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 
import java.util.Arrays;
 
public class ReverseInt {
    public int reverseInt(int n){
 
        String str = Integer.toString(n);
        char[] c = str.toCharArray();
        Arrays.sort(c);
        StringBuilder sb = new StringBuilder(new String(c,0,c.length));  
        return Integer.parseInt(((sb.reverse()).toString()));
    }
 
    // ์•„๋ž˜๋Š” ํ…Œ์ŠคํŠธ๋กœ ์ถœ๋ ฅํ•ด ๋ณด๊ธฐ ์œ„ํ•œ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.
    public static void  main(String[] args){
        ReverseInt ri = new ReverseInt();
        System.out.println(ri.reverseInt(118372));
    }
}
cs
1
2
3
4
5
6
7
8
 
//toCharArray
String arr[]=String.valueOf(n).split("");
 
//์—ญ์ˆœ์ •๋ ฌ
Arrays.sort(arr, Collections.reverseOrder()); 
 
 
cs
StringBuffer reverse()

 

https://programmers.co.kr/learn/courses/30/lessons/12931

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์ž๋ฆฟ์ˆ˜ ๋”ํ•˜๊ธฐ | ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

์ž์—ฐ์ˆ˜ N์ด ์ฃผ์–ด์ง€๋ฉด, N์˜ ๊ฐ ์ž๋ฆฟ์ˆ˜์˜ ํ•ฉ์„ ๊ตฌํ•ด์„œ return ํ•˜๋Š” solution ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค์–ด ์ฃผ์„ธ์š”. ์˜ˆ๋ฅผ๋“ค์–ด N = 123์ด๋ฉด 1 + 2 + 3 = 6์„ return ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค. ์ œํ•œ์‚ฌํ•ญ N์˜ ๋ฒ”์œ„ : 100,000,000 ์ดํ•˜์˜ ์ž์—ฐ์ˆ˜ ์ž…์ถœ๋ ฅ ์˜ˆ N answer 123 6 987 24 ์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช… ์ž…์ถœ๋ ฅ ์˜ˆ #1 ๋ฌธ์ œ์˜ ์˜ˆ์‹œ์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค. ์ž…์ถœ๋ ฅ ์˜ˆ #2 9 + 8 + 7 = 24์ด๋ฏ€๋กœ 24๋ฅผ return ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

programmers.co.kr

 

 

< ์ž๋ฆฟ์ˆ˜ ๋”ํ•˜๊ธฐ>

 

 โ—‹ ์ฒ˜์Œ ํ‘ผ ํ’€์ด

1
2
3
4
5
6
7
8
9
10
11
12
public class Solution {
    public int solution(int n) {
        int answer = 0;
        String temp ="";
        temp += n;
        
        for(int i=0 ; i<temp.length();i++){
            answer+= Integer.parseInt(temp.substring(i,i+1));
        }   
        return answer;
    }
}
cs

์ƒ๊ฐ๊ณผ์ •

1. int ์ž๋ฃŒํ˜•์œผ๋กœ ํ•œ๋‹ค๋ฉด ์ž๋ฆฟ์ˆ˜๋ฅผ ๊ตฌํ•ด์„œ ํ•ด์ค˜์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค๋ฃจ๊ธฐ ์‰ฌ์šด ๋ฌธ์ž์—ด๋กœ  ๋ฐ”๊ฟ”์•ผ๊ฒ ๋‹ค.

2. ๋ฌธ์ž์—ด๋กœ ๋ฐ”๊พผ๋‹ค์Œ charAt() ์ด๋‚˜ substring() ์„ ์‚ฌ์šฉํ•ด์•ผ๊ฒ ๋‹ค. ํ•˜์ง€๋งŒ charAt()์€ char ํ˜•์œผ๋กœ ๋ฐ”๋€Œ๊ธฐ ๋•Œ๋ฌธ์— substring ์„ ์‚ฌ์šฉํ•œ๋‹ค

3.substring()์œผ๋กœ ํ•˜๋‚˜ํ•˜๋‚˜ ํ•ด์ค€๋‹ค์Œ parseInt๋กœ int ํ˜•์œผ๋กœ ๋ฐ”๊ฟ” ์ค€๋‹ค์Œ ํ•˜๋‚˜ํ•˜๋‚˜ answer ์— ๋”ํ•ด์ค€๋‹ค.

 

โ—‹ ๊ฐ™์ด ๋ณด๊ณ ์‹ถ์€ ํ’€์ด

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import java.util.*;
 
public class Solution {
    public int solution(int n) {
        int answer = 0;
 
        while(true){
            answer+=n%10;
            if(n<10)
                break;
 
            n=n/10;
        }
 
        // [์‹คํ–‰] ๋ฒ„ํŠผ์„ ๋ˆ„๋ฅด๋ฉด ์ถœ๋ ฅ ๊ฐ’์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
        System.out.println("Hello Java");
 
        return answer;
    }
}
cs

 


โ—‹ ๋ณต์Šตํ’€์ด ( ๋ฌธ์ž์—ด๋กœ ๋ฐ”๊ฟ”์„œ ํ’€์ด / 20200421)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
public class Solution {
    public int solution(int n) {
        int answer = 0;
        String temp ="";
        temp+=n;
        // ์ •์ˆ˜ n ์„ String ์œผ๋กœ ๋ฐ”๊ฟ”์„œ ์ฒ˜๋ฆฌํ•œ๋‹ค.
        for(int i=0;i<temp.length();i++)//0 1 2  ๋ฒ”์œ„ ์ฃผ์˜ํ•ด์•ผํ•œ๋‹ค.
        {                                
            answer+= Integer.parseInt(temp.substring(i,i+1));
            // ์ฃผ์˜์  substring(i,i+1)
            // substring(0,1)  substring(1,2)  substring(2,3)       
            
        }
        return answer;
    }
}
cs

โ—‹ ๋ณต์Šตํ’€์ด ( ๋ฌธ์ž์—ด๋กœ ๋ฐ”๊พธ์ง€ ์•Š๊ณ  ํ’€์ด /20200421)

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

(โ€ป while ๋ฒ”์œ„๋ฅผ ์ •ํ•  ๋•Œ ์ฃผ์˜ํ•ด์•ผํ•œ๋‹ค.

      n๊ฐ’์ด 1๋ณด๋‹ค ์ปธ์„ ๋•Œ ๋งŒ ๋Œ์•„๊ฐ„๋‹ค. 10์œผ๋กœ ๊ณ„์† ๋‚˜๋ˆ ์ง€๊ฒŒ ๋˜๋‹ˆ๊น 

)

1. Rect ํด๋ž˜์Šค์— ์ž…๋ ฅ๋ฐ›๋Š” ๋ฉ”์†Œ๋“œ, ๋‘˜๋ ˆ๊ตฌํ•˜๋Š” ๋ฉ”์†Œ๋“œ, ๋„“์ด ๊ตฌํ•˜๋Š” ๋ฉ”์†Œ๋“œ, ์ถœ๋ ฅํ•˜๋Š” ๋ฉ”์†Œ๋“œ๋กœ ๋‚˜๋ˆ ์„œ ๊ตฌํ˜„ํ•œ ํ›„

๊ฐ’์„ ๊ตฌํ•˜์—ฌ๋ผ.

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import java.util.Scanner;
 
class Rect{//์ง์‚ฌ๊ฐํ˜• ํด๋ž˜์Šค ์„ค๊ณ„
 
        //๋ฐ์ดํ„ฐ ( ์†์„ฑ, ์ƒํƒœ) → ๋ณ€์ˆ˜
        int w, h;                //๊ฐ€๋กœ ์„ธ๋กœ
 
        //๊ธฐ๋Šฅ (๋™์ž‘, ํ–‰์œ„) →๋ฉ”์†Œ๋“œ
        void input(){                //๊ฐ€๋กœ์„ธ๋กœ์ž…๋ ฅ() 
                                    //์‚ฌ์šฉ์ž๋กœ ๋ถ€ํ„ฐ ๊ฐ€๋กœ,์„ธ๋กœ๋ฅผ ์ž…๋ ฅ ๋ฐ›๊ธฐ ๋•Œ๋ฌธ์— ๋งค๊ฐœ๋ณ€์ˆ˜ ํ•„์š”์—†์Œ
                                    // ๋ฐ˜ํ™˜๊ฐ’์ด ์—†์œผ๋ฉด void ์‚ฌ์šฉ, ์—†์„ ๋•Œ ์•„๋ฌด๊ฒƒ๋„ ์•ˆ์“ฐ๋Š” ๋ฌธ๋ฒ•์€ ์ด๋ฏธ ์ƒ์„ฑ์ž๊ฐ€ ๊ฐ€์ง€๊ณ  ๊ฐ”๋”ฐ.
            
            Scanner sc = new Scanner(System.in);
 
            System.out.print("๊ฐ€๋กœ ์ž…๋ ฅ:");
            w = sc.nextInt();
 
            System.out.print("์„ธ๋กœ ์ž…๋ ฅ:");
            h = sc.nextInt();
            
        }
        int calArea(){                //๋„“์ด๊ณ„์‚ฐ()//๋ชจ๋‘ ๋ฉค๋ฒ„์ด๊ธฐ ๋•Œ๋ฌธ์—
                                    //๋„“์ด์— ๋Œ€ํ•œ ๋ณ€์ˆ˜๊ฐ€ ์—†์œผ๋‹ˆ๊น return ๊ฐ’์œผ๋กœ int ๋กœ ๊ณฑํ•ด์„œ ์ค€๋‹ค.//๊ฐ์ฒด๋„ ๋ฐ˜ํ™˜ํ•ด์ค„์ˆ˜ ์žˆ์Œ
            int result;
            result= w * h ;
            return result;            //return ๋’ค์— ์ ์–ด ๋ดค์ž ์•„๋ฌด ์˜๋ฏธ ์—†์Œ
 
        }
        int calLength(){            //๋‘˜๋ ˆ ๊ณ„์‚ฐ()
 
            int result;
            result = (w + h) *2;
            return result;
 
        }
        void print(int a, int l)    {//๊ฒฐ๊ณผ ์ถœ๋ ฅ()//๋งค๊ฐœ๋ณ€์ˆ˜ ์ด๋ฆ„์€ ์ƒ๊ด€์—†๋‹ค
 
            System.out.println("๊ฐ€๋กœ : " + w);    //๊ฐ€๋กœ : 10
            System.out.println("์„ธ๋กœ : " + h);    //์„ธ๋กœ : 20
            System.out.println("๋„“์ด : " + a);    //๋„“์ด : xxx
            System.out.println("๋‘˜๋ ˆ : " + l);  //๋‘˜๋ ˆ : xxx
        }
}
public class Test065{
 
    public static void main(String[] args){
 
        //Rect ์ธ์Šคํ„ด์Šค ์ƒ์„ฑ -- Rect๋ฅผ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•ด์•ผํ•œ๋‹ค./์ด์ œ ๋ถ•์–ด๋นต
        Rect ob = new Rect();
 
        //์ž…๋ ฅ ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ
        ob.input();
 
        //๋„“์ด ์—ฐ์‚ฐ ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ
         int area = ob.calArea();    //ob.calArea();ํ˜ธ์ถœ๋˜๋ฉด 55๋ผ๊ณ  ๊ฐ’์„ ๋‘๊ณ ๊ฐ€์„œ ๊ทธ๊ฑธ ๋‹ด์„ ๋ณ€์ˆ˜๊ฐ€ ํ•„์š”
        
        //๋‘˜๋ ˆ ์—ฐ์‚ฐ ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ
         int length = ob.calLength();
 
         //๊ฒฐ๊ณผ ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ
         ob.print(area , length);
 
    }
}
cs

https://programmers.co.kr/learn/courses/30/lessons/12917

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ๋ฌธ์ž์—ด ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ๋ฐฐ์น˜ํ•˜๊ธฐ | ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

๋ฌธ์ž์—ด s์— ๋‚˜ํƒ€๋‚˜๋Š” ๋ฌธ์ž๋ฅผ ํฐ๊ฒƒ๋ถ€ํ„ฐ ์ž‘์€ ์ˆœ์œผ๋กœ ์ •๋ ฌํ•ด ์ƒˆ๋กœ์šด ๋ฌธ์ž์—ด์„ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜, solution์„ ์™„์„ฑํ•ด์ฃผ์„ธ์š”. s๋Š” ์˜๋ฌธ ๋Œ€์†Œ๋ฌธ์ž๋กœ๋งŒ ๊ตฌ์„ฑ๋˜์–ด ์žˆ์œผ๋ฉฐ, ๋Œ€๋ฌธ์ž๋Š” ์†Œ๋ฌธ์ž๋ณด๋‹ค ์ž‘์€ ๊ฒƒ์œผ๋กœ ๊ฐ„์ฃผํ•ฉ๋‹ˆ๋‹ค. ์ œํ•œ ์‚ฌํ•ญ str์€ ๊ธธ์ด 1 ์ด์ƒ์ธ ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค. ์ž…์ถœ๋ ฅ ์˜ˆ s return Zbcdefg gfedcbZ

programmers.co.kr

<๋ฌธ์ž์—ด ๋‚ด๋ฆผ์ฐจ์ˆœ์œผ๋กœ ๋ฐฐ์น˜ํ•˜๊ธฐ>

โ—‹ ์ฒ˜์Œ ํ‘ผ ํ’€์ด (ํ•จ์ˆ˜ ์—†์ด ํ’€๊ธฐ)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import java.util.Arrays;
class Solution {
  public String solution(String s) {
      String answer = "";
      char[] a= new char [s.length()];
     //๋ฌธ์ž์—ด charAt์œผ๋กœ char๋ฐฐ์—ด์— ๋‹ด๊ธฐ
      for(int i=0; i<s.length(); i++){
          a[i]=s.charAt(i);
      }
      //์ •๋ ฌ
       Arrays.sort(a);
       //๋ฐ˜๋Œ€๋กœ ๋”ํ•ด์ฃผ๊ธฐ
       for(int i=s.length()-1; i>=0; i--){
        answer+=a[i];
       }
      
      return answer;
  }
}
cs
 

 โ—‹ ์ฐธ๊ณ  ํ•  ํ’€์ด

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import java.util.Arrays;
 
public class ReverseStr {
    public String reverseStr(String str){
    char[] sol = str.toCharArray();
    Arrays.sort(sol);
    return new StringBuilder(new String(sol)).reverse().toString();
    }
 
    // ์•„๋ž˜๋Š” ํ…Œ์ŠคํŠธ๋กœ ์ถœ๋ ฅํ•ด ๋ณด๊ธฐ ์œ„ํ•œ ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค.
    public static void main(String[] args) {
        ReverseStr rs = new ReverseStr();
        System.out.println( rs.reverseStr("Zbcdefg") );
    }
}
cs

 

 

 

 

 

+ Recent posts