[Kotlin] ์—†๋Š” ์ˆซ์ž ๋”ํ•˜๊ธฐ (ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค ์•Œ๊ณ ๋ฆฌ์ฆ˜)
๋ฐ˜์‘ํ˜•

 

๋ฌธ์ œ

https://school.programmers.co.kr/learn/courses/30/lessons/86051

 

 


 

์ ‘๊ทผ

 

  • ์•„๋ž˜์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ๋‚ด๋ถ€์˜ ์›์†Œ๋ฅผ ๊ฒ€์‚ฌํ•˜๊ณ , ์—†๋Š” ๊ฒฝ์šฐ sum์„ ์ถ”๊ฐ€ํ•˜๋ฉด ๋œ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋‹ค.
class Solution {
    fun solution(numbers: IntArray): Int {
        
        var sum = 0
        if (numbers.contains(1) == false) sum += 1
        if (numbers.contains(2) == false) sum += 2
        if (numbers.contains(3) == false) sum += 3
        if (numbers.contains(4) == false) sum += 4
        if (numbers.contains(5) == false) sum += 5
        if (numbers.contains(6) == false) sum += 5
        .
        .
        .
        return sum
    }
}
  • ์ด ๋‚ด์šฉ์„ for๋ฌธ์œผ๋กœ ๊ฐ„๋‹จํ•˜๊ฒŒ ์ •๋ฆฌํ•˜๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ์™„๋ฃŒ๋œ๋‹ค.

 


 

์„ฑ๊ณต ์ฝ”๋“œ

 

class Solution {
    fun solution(numbers: IntArray): Int {
        var sum = 0
        for (i in 1..9) {
        if (numbers.contains(i) == false) sum += i }
        return sum
    }
}

 


 

์„ค๋ช…

 

  1. for๋ฌธ์œผ๋กœ 1๋ถ€ํ„ฐ 9๊นŒ์ง€ ๋ฐ˜๋ณตํ•˜๋ฉฐ numbers ๋ฐฐ์—ด ์•ˆ์— 1๋ถ€ํ„ฐ 9๊นŒ์ง€์˜ ์›์†Œ๊ฐ€ ์žˆ๋Š”์ง€ ๊ฒ€์‚ฌํ•œ๋‹ค.(contains)
  2. ์›์†Œ๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ(numbers.contains(i) == false) ์—๋Š” sum์— ํ•ด๋‹น ์›์†Œ๋ฅผ ๋”ํ•ด์ค€๋‹ค.
  3. ์—†๋Š” ์›์†Œ๊ฐ€ ๋ชจ๋‘ ๋”ํ•ด์ง„ ๋ณ€์ˆ˜ sum์„ ๋ฆฌํ„ดํ•œ๋‹ค.
  4. numbers.contains(i) == false ๋Œ€์‹  i in numbers == false ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

 

 


๐Ÿ’ก Kotlin์—์„œ ๋ฐฐ์—ด ์•ˆ์˜ ํŠน์ • ์›์†Œ์™€ ๊ด€๋ จ๋œ ๋ฉ”์„œ๋“œ

 

val array = arrayOf(1, 2, 3, 4, 5)
val result = 3 in array // true
  • in ์—ฐ์‚ฐ์ž
  • ๋ฐฐ์—ด ์•ˆ์— ํŠน์ • ์›์†Œ๊ฐ€ ์žˆ๋Š”์ง€๋ฅผ ๊ฒ€์‚ฌ
  • ํ•ด๋‹น ์›์†Œ๊ฐ€ ๋ฐฐ์—ด์— ํฌํ•จ๋˜์–ด ์žˆ์œผ๋ฉด true๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ , ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด false๋ฅผ ๋ฐ˜ํ™˜

 

val array = arrayOf(1, 2, 3, 4, 5)
val result = array.contains(3) // true
  • contains() 
  • ๋ฐฐ์—ด์— ํŠน์ • ์›์†Œ๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€๋ฅผ ๊ฒ€์‚ฌ
  • ํ•ด๋‹น ์›์†Œ๊ฐ€ ๋ฐฐ์—ด์— ํฌํ•จ๋˜์–ด ์žˆ์œผ๋ฉด true๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ , ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด false๋ฅผ ๋ฐ˜ํ™˜

 

val array = arrayOf(1, 2, 3, 4, 5)
val containsElement = array.any { it == 3 } // true
  • .any() 
  • ๋ฐฐ์—ด์— ์ฃผ์–ด์ง„ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ์š”์†Œ๊ฐ€ ํ•˜๋‚˜๋ผ๋„ ์žˆ๋Š”์ง€๋ฅผ ํ™•์ธ
  • ๋งŒ์•ฝ ๋ฐฐ์—ด์— ํ•ด๋‹น ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ์š”์†Œ๊ฐ€ ํ•˜๋‚˜๋ผ๋„ ์žˆ๋‹ค๋ฉด true๋ฅผ ๋ฐ˜ํ™˜, ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด false๋ฅผ ๋ฐ˜ํ™˜

 

val array = arrayOf(1, 2, 3, 4, 5)
val index = array.indexOf(3) // 2
  • indexOf()
  • ๋ฐฐ์—ด์—์„œ ํŠน์ • ์›์†Œ์˜ ์ธ๋ฑ์Šค๋ฅผ ๊ฒ€์ƒ‰
  • ํ•ด๋‹น ์›์†Œ๊ฐ€ ๋ฐฐ์—ด์— ์žˆ์œผ๋ฉด ์ธ๋ฑ์Šค๊ฐ’, ๋ฐฐ์—ด์— ์—†์œผ๋ฉด -1์„ ๋ฐ˜ํ™˜

 

val sortedArray = arrayOf(1, 2, 3, 4, 5)
val index = sortedArray.binarySearch(3) // 2
  • binarySearch()
  • ๋ฐฐ์—ด์ด ์ •๋ ฌ๋˜์–ด ์žˆ์„ ๊ฒฝ์šฐ, ์ด์ง„ ๊ฒ€์ƒ‰์„ ์ˆ˜ํ–‰ํ•˜์—ฌ ํŠน์ • ์›์†Œ์˜ ์ธ๋ฑ์Šค๋ฅผ ๊ฒ€์ƒ‰
  • ํ•ด๋‹น ์›์†Œ๊ฐ€ ๋ฐฐ์—ด์— ์žˆ์œผ๋ฉด ์ธ๋ฑ์Šค๊ฐ’, ๋ฐฐ์—ด์— ์—†์œผ๋ฉด ์Œ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜

 

val array = arrayOf(1, 2, 3, 4, 5)
val filteredArray = array.filter { it == 3 } // [3]
val containsElement = filteredArray.isNotEmpty() // true
  • .filter() 
  • ๋ฐฐ์—ด์˜ ๊ฐ ์š”์†Œ์— ๋Œ€ํ•ด ์ฃผ์–ด์ง„ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ์š”์†Œ๋งŒ์„ ์„ ํƒํ•˜์—ฌ ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ์ƒ์„ฑ
  • ๋”ฐ๋ผ์„œ ํŠน์ • ์›์†Œ๊ฐ€ ๋ฐฐ์—ด์— ์žˆ๋Š”์ง€๋ฅผ ํ™•์ธํ•˜๋Š” ๊ฒƒ๋ณด๋‹ค๋Š”, ํ•ด๋‹น ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜๋Š” ์›์†Œ๊ฐ€ ์žˆ๋Š”์ง€๋ฅผ ํ™•์ธํ•˜๋Š” ๋ฐ ์ ํ•ฉ





 


 

๋‹ค๋ฅธ ๋ฐฉ๋ฒ•

 

class Solution {
    fun solution(numbers: IntArray): Int = (0..9).filterNot(numbers::contains).sum()
}
  • ์œ„์™€ ๋ฐ˜๋Œ€๋กœ .filter()๊ฐ€ ์•„๋‹Œ .filterNot()์„ ์‚ฌ์šฉํ•œ ๋ฐฉ๋ฒ•์ด๋‹ค.
  • filterNot(numbers::contains)
    - ์ž…๋ ฅ๋œ ๋ฐฐ์—ด numbers์— ํฌํ•จ๋˜์ง€ ์•Š๋Š” ์ˆซ์ž๋“ค์„ ํ•„ํ„ฐ๋ง
    - filterNot() : ์ฃผ์–ด์ง„ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•˜์ง€ ์•Š๋Š” ์š”์†Œ๋“ค์„ ์„ ํƒํ•˜์—ฌ ์ƒˆ๋กœ์šด ์ปฌ๋ ‰์…˜์„ ์ƒ์„ฑ
    - numbers::contains : numbers์˜ contains() ๋ฉ”์„œ๋“œ๋ฅผ ์ฐธ์กฐํ•˜๋Š” ๋ฉ”์„œ๋“œ ์ฐธ์กฐ
  • sum()์œผ๋กœ ์ด๋ ‡๊ฒŒ ํ•„ํ„ฐ๋ง๋œ ์ˆซ์ž๋“ค์˜ ํ•ฉ์„ ๊ณ„์‚ฐํ•˜๊ณ  ๋ฆฌํ„ด

 

class Solution {
    fun solution(numbers: IntArray): Int  = 45 - numbers.sum()
}
  • 1๋ถ€ํ„ฐ 9๊นŒ์ง€ ์ „๋ถ€ ๋”ํ•œ ๊ฐ’(45)์—์„œ numbers์˜ ํ•ฉ์„ ๋นผ์ค€ ๊ฐ„๋‹จํ•œ ํ’€์ด์ด๋‹ค.

 

 

 

๋ฐ˜์‘ํ˜•
 ๐Ÿ’ฌ C O M M E N T