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

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์„œ์šธ์—์„œ ๊น€์„œ๋ฐฉ ์ฐพ๊ธฐ | ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค

Stringํ˜• ๋ฐฐ์—ด seoul์˜ element์ค‘ Kim์˜ ์œ„์น˜ x๋ฅผ ์ฐพ์•„, ๊น€์„œ๋ฐฉ์€ x์— ์žˆ๋‹ค๋Š” String์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜, solution์„ ์™„์„ฑํ•˜์„ธ์š”. seoul์— Kim์€ ์˜ค์ง ํ•œ ๋ฒˆ๋งŒ ๋‚˜ํƒ€๋‚˜๋ฉฐ ์ž˜๋ชป๋œ ๊ฐ’์ด ์ž…๋ ฅ๋˜๋Š” ๊ฒฝ์šฐ๋Š” ์—†์Šต๋‹ˆ๋‹ค. ์ œํ•œ ์‚ฌํ•ญ seoul์€ ๊ธธ์ด 1 ์ด์ƒ, 1000 ์ดํ•˜์ธ ๋ฐฐ์—ด์ž…๋‹ˆ๋‹ค. seoul์˜ ์›์†Œ๋Š” ๊ธธ์ด 1 ์ด์ƒ, 20 ์ดํ•˜์ธ ๋ฌธ์ž์—ด์ž…๋‹ˆ๋‹ค. Kim์€ ๋ฐ˜๋“œ์‹œ seoul ์•ˆ์— ํฌํ•จ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ž…์ถœ๋ ฅ ์˜ˆ seoul return

programmers.co.kr

 

 

1. ์‹ค์ˆ˜ํ•œ ํ’€์ด

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution {
  public String solution(String[] seoul) {
      String answer = "";
      String x="";
      
      for(int i=0; i<seoul.length;i++){
          
          if(seoul[i].equals("Kim")){
              x+=i;
              answer="๊น€์„œ๋ฐฉ์€ i์— ์žˆ๋‹ค";
              answer.replaceAll("i","x");
              
          }
          
          
      }
      
      
      return answer;
  }
}
cs

 

- ๋ฌธ์ž์—ด์„ ์–ด๋–ป๊ฒŒ ๋”ํ•ด์•ผํ• ์ง€ ๋ชจ๋ฅด๊ฒ ๋‹ค..

-??๊ฐ‘์ž๊ธฐ ๋ญ๊ฐ€ ํ—ท๊ฐˆ๋ ธ๋Š”์ง€ ๋ชจ๋ฅด๊ฒ ๋‹ค.

 

 

2. ์ฒ˜์Œ ํ‘ผ ํ’€์ด

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[] seoul) {
      String answer = "";
     
      for(int i=0; i<seoul.length;i++){
                
          if(seoul[i].equals("Kim")){
              
              answer="๊น€์„œ๋ฐฉ์€ ";
              answer+=i;
              answer+="์— ์žˆ๋‹ค";
              
          }
 
      }
     
      return answer;
  }
}
cs

์ƒ๊ฐ๊ณผ์ •

- ๋ฌธ์ž์—ด์€ ==์—ฐ์‚ฐ์ž๋กœ ๋น„๊ตํ•˜๋ฉด ์ฃผ์†Œ๊ฐ’์„ ๋น„๊ตํ•˜๋Š” ๊ฒƒ์ด๋ฏ€๋กœ ์›ํ•˜๋Š” ๋‹ต์„ ์–ป์ง€ ๋ชปํ•œ๋‹ค.

- ๊ทธ๋ž˜์„œ .equals ํ•จ์ˆ˜๋กœ ํ•˜๋‚˜ํ•˜๋‚˜ ๋น„๊ตํ•ด์„œ 'Kim'์˜ ์œ„์น˜๋ฅผ ์ฐพ๋Š”๋‹ค.

- int๊ฐ’์„ ๋”ํ•ด์„œ ๋ฌธ์žฅ์„ ๋งŒ๋“ค์ˆ˜์žˆ๋‹ค.

- ์ธ๋ฑ์Šค i ๊ฐ’์„ ์ฐพ์•„์„œ ๋ฌธ์žฅ์‚ฌ์ด์— ๋”ํ•ด์„œ returnํ•ด์ค€๋‹ค. 

 

//   answer = "๊น€์„œ๋ฐฉ์€ "+ i + "์— ์žˆ๋‹ค"; ์ด๋ ‡๊ฒŒ ํ•ด๋„ ๋œ๋‹ค.

+ Recent posts