
Problem ๐ป
https://school.programmers.co.kr/learn/courses/30/lessons/120854
๋ฌธ์์ด ๋ฐฐ์ด strlist๊ฐ ๋งค๊ฐ๋ณ์๋ก ์ฃผ์ด์ง๋๋ค. strlist ๊ฐ ์์์ ๊ธธ์ด๋ฅผ ๋ด์ ๋ฐฐ์ด์ returnํ๋๋ก solution ํจ์๋ฅผ ์์ฑํด์ฃผ์ธ์.
์ ํ์ฌํญ
- 1 ≤ strlist ์์์ ๊ธธ์ด ≤ 100
- strlist๋ ์ํ๋ฒณ ์๋ฌธ์, ๋๋ฌธ์, ํน์๋ฌธ์๋ก ๊ตฌ์ฑ๋์ด ์์ต๋๋ค.
์ ์ถ๋ ฅ ์
strlistresult
| ["We", "are", "the", "world!"] | [2, 3, 3, 6] |
| ["I", "Love", "Programmers."] | [1, 4, 12] |
Approach 1 โ - ๋์ ์ด๊ธฐ ์ ๊ทผ๋ฒ
class Solution {
public int[] solution(String[] strlist) {
int n = strlist.length;
int[] answer;
for(int i = 0; i < n; i++) {
int count[i] = strlist[i].length;
}
for (int l : )
}
}
<๋์ ๋ฏธ์ํ ์๊ฐ์ ํ๋ฆ ๊ณผ์ >
0. ํ์ํ ๋ณ์๋ฅผ ์ ์ธํ๋ค.
1. ๋ฌธ์์ด ๋ฐฐ์ด์ ๊ฐ ์์์ ๊ธธ์ด๋ฅผ ์ธก์ ํ๊ธฐ ์ํด length ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๋ค.
2. ๊ฐ ์์์ ๊ธธ์ด๋ฅผ ์ธก์ ํ๋ ค๋ฉด for ๋ฌธ์ ์ฌ์ฉํ์ฌ ๋ฐฐ์ด์ ์์๋ฅผ ์ฐจ๋ก๋๋ก ์ฒ๋ฆฌํด์ผ ํ๋ค.
3. for ๋ฌธ ์์์ ์ธก์ ๋ ๊ธธ์ด๋ฅผ int[] answer ๋ฐฐ์ด์ ์ ์ฅํ๋ค.
4. ๊ฒฐ๊ณผ๋ฅผ ์ถ๋ ฅํ๋ค.
Approach 2 โญ - ๋์ ์ ๊ทผ๋ฒ ๊ต์
์ฝ๋ ๋ถ์ ๋ฐ ๋ฌธ์ ํด๊ฒฐ ๋จ๊ณ
1. ๋ณ์ ์ ์ธ
int n = strlist.length; ๋ ์ํ๋ค. strlist ๋ฐฐ์ด์ ๊ธธ์ด๋ฅผ n์ ์ ์ฅํ๋ค.
๊ทธ๋ฌ๋ int[] answer;๋ ํฌ๊ธฐ๋ฅผ ๋ช ์ํ์ง ์์๋ค. ๋ฐฐ์ด์ ์์ฑํ ๋ ํฌ๊ธฐ๋ฅผ ์ง์ ํด์ผ ํ๋ค.
int[] answer = new int[n]; //n๋งํผ์ ๊ธธ์ด๋ฅผ ๊ฐ์ง ๋ฐฐ์ด ์์ฑ
2. ๊ฐ ์์์ ๊ธธ์ด ๊ณ์ฐ
int count[i] = strlist[i].length;๋ ๋ฌธ๋ฒ์ ์ผ๋ก ํ๋ ธ๋ค.
count๋ผ๋ ๋ฐฐ์ด์ ์๋ก ์ ์ธํ๋ ค๊ณ ํ ๊ฑด๋ฐ, ์ด๋ฏธ ๋ฐฐ์ด์ answer๋ก ์ ์ธํ์ผ๋ ์ด๊ฑธ ํ์ฉํด์ผ ํ๋ค.
๋ฐฐ์ด answer์ i๋ฒ์งธ ์์น์ ๊ฐ์ ์ ์ฅํด์ผ ํ๋๊น ์๋์ ๊ฐ์ด ์์ฑํด์ผ ํ๋ค.
answer[i] = strlist[i].length();
3. ์ถ๋ ฅ
ํ์ฌ for (int l : ) ๋ถ๋ถ์ด ๋ฏธ์์ฑ์ด๋ค. for-each ๋ฌธ๋ฒ์ ๋ฐฐ์ด์ ๋ชจ๋ ์์๋ฅผ ์ํํ ๋ ์ ์ฉํ๋ค.
๊ทธ๋ฌ๋ ์ด ๋ฌธ์ ์์๋ ๋จ์ํ ๋ฐฐ์ด์ ๋ฐํํ๊ธฐ๋ง ํ๋ฉด ๋๊ธฐ ๋๋ฌธ์ return answer;๋ก ์ถฉ๋ถํ๋ค.
return answer;
Solution ๐ก
<์ ๋ต ์ฝ๋>
public class Length {
public int[] solution(String[] strlist) { // strlist๋ String ๋ฐฐ์ด์ด์ด์ผ ํฉ๋๋ค.
int n = strlist.length; // ๋ฐฐ์ด์ ๊ธธ์ด ๊ฐ์ ธ์ค๊ธฐ
int[] answer = new int[n]; // ๊ฒฐ๊ณผ ๋ฐฐ์ด ์์ฑ
for (int i = 0; i < n; i++) {
answer[i] = strlist[i].length(); // ๊ฐ ๋ฌธ์์ด์ ๊ธธ์ด ์ ์ฅ
}
return answer; // ๊ฒฐ๊ณผ ๋ฐํ
}
// ํ
์คํธ ์ถ๋ ฅ ์ฝ๋
public static void main(String[] args) {
Length length = new Length(); // ๊ฐ์ฒด ์์ฑ
String[] strlist = {"hello", "world", "java", "coding"}; // ํ
์คํธ์ฉ ๋ฌธ์์ด ๋ฐฐ์ด
int[] result = length.solution(strlist); // solution ํจ์ ํธ์ถ
// ๊ฒฐ๊ณผ ์ถ๋ ฅ
for (int len : result) {
System.out.print(len + " ");
}
}
}
์ถ๋ ฅ์ฝ๋์์์ Length length = new Length();์ ์๋ฏธ :
1. ํด๋์ค์ ๊ฐ์ฒด
์๋ฐ๋ ๊ฐ์ฒด์งํฅ ํ๋ก๊ทธ๋๋ฐ ์ธ์ด์ด๋ค. ํด๋์ค๋ ๊ฐ์ฒด๋ฅผ ๋ง๋ค๊ธฐ ์ํ ์ค๊ณ๋์ด๊ณ ,
๊ฐ์ฒด(object)๋ ๊ทธ ์ค๊ณ๋๋ก ๋ง๋ ์ค์ ์ธ์คํด์ค์ด๋ค.
public class Length {
//ํด๋์ค ์ ์ธ
}
์ฌ๊ธฐ์ Length๋ผ๋ ํด๋์ค๋ ์ค๊ณ๋ ์ญํ ์ ํ๋ค. ์ด ํด๋์ค๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ์ค์ ๋ก ๋์ํ๋ ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ค๋ฉด new ํค์๋๋ฅผ ์ฌ์ฉํด์ผ ํ๋ค.
2. new Length()์ ์๋ฏธ
new Length()๋ Length ํด๋์ค๋ก๋ถํฐ ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ๊ตฌ๋ฌธ์ด๋ค.
- new ๋ ์๋ก์ด ๊ฐ์ฒด๋ฅผ ์์ฑํ๋ค๋ ์๋ฏธ.
- Length()๋ ํด๋์ค์ ์์ฑ์(Constructor)๋ฅผ ํธ์ถํ๋ค. ์์ฑ์๋ ๊ฐ์ฒด๊ฐ ์์ฑ๋ ๋ ์คํ๋๋ ํน๋ณํ ๋ฉ์๋์ด๋ค.
- new Length()๋ ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ์ Length ํด๋์ค์ ๊ฐ์ฒด๋ฅผ ๋ง๋ค๊ณ ์ด๊ธฐํํ๋ค.
3. Length length = new Length();์ ์ญํ
3.1 ํด๋์ค ๊ฐ์ฒด ์์ฑ : new Length()๋ฅผ ํตํด Length ํด๋์ค์ ๊ฐ์ฒด๋ฅผ ๋ฉ๋ชจ๋ฆฌ์ ์์ฑํ๋ค.
3.2 ๋ณ์์ ๊ฐ์ฒด ์ฐธ์กฐ : Length length๋ผ๋ ๋ณ์๋ฅผ ์ ์ธํ์ฌ, ์์ฑ๋ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ ์ ์๋๋ก ํ๋ค.
์ฆ, length๋ Lengthํด๋์ค์ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํค๋ ์ฐธ์กฐ ๋ณ์์ด๋ค. ์ด๋ฅผ ํตํด ์ฐ๋ฆฌ๋ Length ํด๋์ค์ ๋ฉ์๋์ ์์ฑ์ ์ ๊ทผํ ์ ์๋ค.
4. ์ ๊ฐ์ฒด๋ฅผ ์์ฑํด์ผ ํ๋?
ํด๋์ค์ ์ ์๋ ์ธ์คํด์ค ๋ฉ์๋๋ฅผ ํธ์ถํ๊ฑฐ๋ ํด๋์ค ๋ด๋ถ ๋ฐ์ดํฐ๋ฅผ ์ฌ์ฉํ ๋๋ ๋ฐ๋์ ๊ฐ์ฒด๊ฐ ํ์ํ๋ค.
(Static ์๋ ๋ฉ์๋๋ ๋ฐ๋์ ๊ฐ์ฒด๋ฅผ ์์ฑํด์ผ๋ง ํธ์ถํ ์ ์๋ค.)
์๋ฅผ ๋ค์ด, solution ๋ฉ์๋๋ Length ํด๋์ค์ ์ธ์คํด์ค ๋ฉ์๋์ด๊ธฐ ๋๋ฌธ์ Lengthํด๋์ค์ ๊ฐ์ฒด๊ฐ ์์ด์ผ ํธ์ถํ ์ ์๋ค.
public class Length {
public int[] solution(String[] strlist) {
// ๋ฉ์๋ ๊ตฌํ
return new int[0];
}
public static void main(String[] args) {
Length length = new Length(); //๊ฐ์ฒด ์์ฑ
int[] result = length.solution(new String[] {"hello", "world"}); //๋ฉ์๋ ํธ์ถ
}
}
๋ง์ฝ ๊ฐ์ฒด๋ฅผ ์์ฑํ์ง ์๊ณ solution ๋ฉ์๋๋ฅผ ํธ์ถํ๋ ค ํ๋ค๋ฉด, ์ปดํ์ผ ์๋ฌ๊ฐ ๋ฐ์ํ๋ค.
์ธ์คํด์ค ๋ฉ์๋๋?
[Java] static์ ์ ๋ฌด (์ธ์คํด์ค ๋ฉ์๋ & ํด๋์ค ๋ฉ์๋)
1. ํด๋์ค๋?ํด๋์ค๋ ์ค๊ณ๋์ด๋ค.์ค๊ณ๋๋ฅผ ์ด์ฉํด์ ๋ฌผ๊ฑด(๊ฐ์ฒด)๋ฅผ ๋ง๋ค ์ ์๋ค.์๋ฅผ ๋ค์ด, ์๋์ฐจ ์ค๊ณ๋(Car)๋ฅผ ๋ง๋ค๋ฉด, ์ด ์ค๊ณ๋๋ฅผ ๋ฐํ์ผ๋ก ๋นจ๊ฐ์ ์๋์ฐจ์ ํ๋์ ์๋์ฐจ๋ผ๋ ๋ ๋์ ์
yeonbikim.tistory.com
์์ฑ์๋?
๋งํฌ ์ฐธ๊ณ .
'๐ฐ๐ท ํ๊ตญ์ด (Korean) > Java Algorithm Coding Test' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [Java] ๋ฐฐ์ด์ ์ ์ฌ๋ (HashSet๊ณผ HashMap์ ์ฐจ์ด) (1) | 2024.12.14 |
|---|---|
| [Java] ๋ฐฐ์ด ์๋ฅด๊ธฐ (๋ฐฐ์ด์ ํญ์ ์ธ๋ฑ์ค 0๋ถํฐ ์์๋๋ค.) (0) | 2024.12.08 |
| [Java] ๋ฒ๋ธ๋ฐฐ์ด ๋ฌธ์ ํ์ด (1) | 2024.12.05 |
| [Java] ๋ฒ๋ธ ์ ๋ ฌ ๊ฐ๋ (0) | 2024.12.05 |
| [Java] ์ ํ์ ๋ ฌ ์์ฉ ๋ฌธ์ - ์ค๋ฆ์ฐจ์/ ๋ด๋ฆผ์ฐจ์ / ์ํ๋ฒณ (1) | 2024.12.05 |