모든 서브셋을 구하고 모든 서브셋의 xor값의 합을 리턴
걍시키는대로하면됨
정답
class Solution {
fun subsetXORSum(nums: IntArray): Int {
val numList = nums.toList()
val subsetList = numList.subset()
var res=0
for (i in subsetList){
if (i.isEmpty()) continue
if(i.size==1){
res+=i[0]
continue
}
var tempRes=i[0]
for (j in i.subList(1,i.size)){
tempRes=tempRes xor j
}
res+=tempRes
}
return res
}
}
fun List<Int>.subset(): List<List<Int>> {
val res = mutableListOf<List<Int>>()
for (i in 0 until (1 shl this.size)) {
val list = mutableListOf<Int>()
for (j in this.indices) {
if (i and (1 shl j) != 0) {
list.add(this[j])
}
}
res.add(list)
}
return res
}
'알고리즘' 카테고리의 다른 글
릿코드 648. Replace Words 코틀린 (0) | 2024.06.07 |
---|---|
릿코드 200일 달성 (0) | 2024.05.27 |
릿코드 1325. Delete Leaves With a Given Value 코틀린 (0) | 2024.05.17 |
릿코드 2373. Largest Local Values in a Matrix 코틀린 (0) | 2024.05.12 |
릿코드 786. K-th Smallest Prime Fraction 코틀린 (0) | 2024.05.10 |