This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import kotlin.math.abs | |
class Solution { | |
fun findDuplicates(nums: IntArray): List<Int> { | |
fun twiceFound(acc: Pair<List<Int>, MutableList<Int>>, num: Int): Pair<List<Int>, MutableList<Int>> { | |
val (uniques, negs) = acc | |
return if (negs[abs(num) - 1] < 0) { | |
uniques.plus(num) to negs | |
} else { | |
negs[abs(num) - 1] = -1 * negs[abs(num) - 1] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
fun findDuplicates(nums: IntArray): List<Int> { | |
fun twiceFound(uniques: MutableMap<Int, Int>, num: Int): MutableMap<Int, Int> { | |
uniques.set(num, uniques.getOrPut(num){ 0 } + 1) | |
return uniques | |
} | |
return nums.fold(mutableMapOf<Int, Int>(), { t, num -> twiceFound(t, num) }).toMap().filterValues{ it == 2 }.keys.toList() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
val mask: Int = (0..31 step 2).fold(0){ acc, v -> acc + (1 shl v)} | |
fun isPowerOfFour(num: Int): Boolean { | |
return (num - 1) and num == 0 && (mask and num) > 0 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
fun isPalindrome(s: String): Boolean { | |
val targetString: String = s.filter{ it.isLetterOrDigit() }.toLowerCase() | |
return targetString == targetString.reversed() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyHashSet() { | |
val numBuckets: Int = 97 | |
val buckets = List<MutableSet<Int>>(numBuckets){ mutableSetOf<Int>() } | |
val hashFunc : (Int) -> Int = { key: Int -> key % numBuckets } | |
fun add(key: Int) { | |
buckets.get(hashFunc(key)).add(key) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Solution { | |
fun detectCapitalUse(word: String): Boolean { | |
return word.all{ it.isLowerCase() } || | |
word.all{ it.isUpperCase() } || | |
(word.first().isUpperCase() && word.drop(1).all{ it.isLowerCase() }) | |
} | |
} |