Search

๋ฐ•์€๋นˆ

2.๋ฌธ์ œ์ด๋ฆ„
3. ์ˆ˜ํ–‰์‹œ๊ฐ„[์ดˆ(s)]
7200
์ข‹์•„์š” ๋ˆ„๋ฅด๊ธฐ
์ข‹์•„์š” ์ˆ˜
: 0
5 more properties
| ์ฝ”๋“œ ์ž‘์„ฑํ•˜๊ธฐ
HashSet<Integer> numberSet = new HashSet<>(); public int solution(String numbers) { int answer = 0; recursive("",numbers); Iterator<Integer> it = numberSet.iterator(); while(it.hasNext()) { int number = it.next(); if(isPrime(number)) answer++; } return answer; } public void recursive(String comb, String others) { if (!comb.equals("")) numberSet.add(Integer.valueOf(comb)); for (int i = 0; i < others.length(); i++) { recursive(comb + others.charAt(i), others.substring(0, i) + others.substring(i + 1)); } } public boolean isPrime(int num) { if(num == 0 || num == 1) return false; for(int i=2; i<=(int)Math.sqrt(num); i++) { if(num%i == 0 ) return false; } return true; }
Java
๋ณต์‚ฌ
| ์ฝ”๋“œ ์„ค๋ช…ํ•˜๊ธฐ
1.
์ˆซ์ž๋“ค์„ ์กฐํ•ฉํ•˜์—ฌ ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๋งŒ๋“ ๋‹ค
a.
์ˆซ์ž์˜ ์กฐํ•ฉ์ธ comb์™€ ๋‚จ์€ ์ˆซ์ž๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” others๋ฅผ ์ธ์ˆ˜๋กœ ๊ฐ–๋Š”๋‹ค
b.
for๋ฌธ์„ ํ†ตํ•ด ๋‚จ์€ ์ˆซ์ž์˜ ๊ธธ์ด๋งŒํผ ๋ฐ˜๋ณตํ•œ๋‹ค
c.
์ˆซ์ž ํ•˜๋‚˜๋ฅผ ๊บผ๋‚ด comb์— ์ถ”๊ฐ€ํ•˜๊ณ  ๋‚จ์€ ์ˆซ์ž๋ฅผ ๋‚˜ํƒ€๋‚ด๊ธฐ์œ„ํ•ด ๊บผ๋‚ธ์ˆซ์ž๋ฅผ ์ œ์™ธํ•˜๊ณ  substring์„ ํ•˜์˜€๋‹ค
d.
๋‹ค์‹œ ํ•จ์ˆ˜๋ฅผ ์„ ์–ธํ•œ๋‹ค
e.
๊ทธ๋ ‡๊ฒŒ ๋ฐ˜๋ณตํ•˜๋ฉด ๋ชจ๋“  ๊ฒฝ์šฐ์˜ ์ˆ˜๋ฅผ ๋‹ค ๋„ฃ๊ฒŒ๋œ๋‹ค
2.
์ค‘๋ณต๋˜์ง€์•Š๊ฒŒ hashSet์— ๋„ฃ๋Š”๋‹ค
3.
๊ฐ’์„ ํ•˜๋‚˜ํ•˜๋‚˜ ๊บผ๋‚ด์–ด ์†Œ์ˆ˜์ธ์ง€ ํ™•์ธํ•œ๋‹ค
a.
0๊ณผ 1์€ ์†Œ์ˆ˜๊ฐ€ ์•„๋‹ˆ๊ธฐ๋•Œ๋ฌธ์— ๊ทธ ์ˆ˜๊ฐ€๋‚˜์˜ฌ๊ฒฝ์šฐ ๋ฐ”๋กœ false๋ฅผํ•œ๋‹ค
b.
์†Œ์ˆ˜๋Š” ์ž๊ธฐ ์ž์‹ ์˜ ์–‘์˜ ์ œ๊ณฑ๊ทผ๊นŒ์ง€๋งŒ ๊ตฌํ•˜๋ฉด ๋˜๊ธฐ๋•Œ๋ฌธ์— ์–‘์˜ ์ œ๊ณฑ๊ทผ๊นŒ์ง€ for๋ฌธ์„ ์ž…๋ ฅํ•œ๋‹ค
c.
๊ทธ๋ฆฌ๊ณ  ๋‚˜๋จธ์ง€๊ฐ€ 0์ผ๊ฒฝ์šฐ ๋‚˜๋ˆ„์–ด ๋–จ์–ด์ง€๋Š” ์ˆ˜๊ฐ€ ์žˆ์œผ๋ฏ€๋กœ false์ฒ˜๋ฆฌํ•œ๋‹ค
d.
for๋ฌธ์ด ๋๋‚˜๋„ ์—†์„๊ฒฝ์šฐ ์†Œ์ˆ˜์ด๋ฏ€๋กœ true์ฒ˜๋ฆฌํ•œ๋‹ค
4.
์†Œ์ˆ˜์ผ๊ฒฝ์šฐ answer๋ฅผ 1 ๋”ํ•œ๋‹ค
๋งŒ์•ฝ 1,7,3์ด๋ผ๋Š” ์ˆ˜๊ฐ€ ์žˆ์„๊ฒฝ์šฐ
โ€ข
for( i < 3 ) i = 0
โ—ฆ
comb = 1, others = 73
โ–ช
for( i < 2 ) i = 0
โ€ข
comb = 17, others = 3
โ—ฆ
for( i < 1 ) i = 0
โ–ช
comb = 173, others = โ€œโ€
โ€ข
for๋ฌธ์ด ๋์ด๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค์‹œ ๋Œ์•„๊ฐ„๋‹ค
โ—ฆ
for๋ฌธ์ด ๋์ด๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค์‹œ ๋Œ์•„๊ฐ„๋‹ค
โ–ช
for( i < 2 ) i = 1
โ€ข
comb = 13, other = 7
โ—ฆ
for( i < 1 ) i = 0
โ–ช
comb = 137, others = โ€œโ€
โ€ข
for๋ฌธ์ด ๋์ด๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค์‹œ ๋Œ์•„๊ฐ„๋‹ค
โ—ฆ
for๋ฌธ์ด ๋์ด๊ธฐ๋•Œ๋ฌธ์— ๋‹ค์‹œ ๋Œ์•„๊ฐ„๋‹ค
โ–ช
for๋ฌธ์ด ๋์ด๊ธฐ ๋•Œ๋ฌธ์— ๋‹ค์‹œ ๋Œ์•„๊ฐ„๋‹ค
โ€ข
for( i < 3 ) i = 1
โ—ฆ
comb = 7, others = 13
โ–ช
for( i < 2 ) i = 0
โ€ข
comb = 71, other = 3
โ€ฆ.๊ณ„์† ๋ฐ˜๋ณต