Skip to content

Commit

Permalink
add kotlin solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
MagicienDeCode committed Jul 26, 2020
1 parent 5dd7167 commit 64b4d79
Show file tree
Hide file tree
Showing 13 changed files with 573 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package string_integer

import kotlin.math.abs

class AngleBetweenHandsofaClockKotlin1344 {
fun angleClock(hour: Int, minutes: Int): Double {
val baseHour = when (hour) {
12 -> 0
else -> 30 * hour
}
val hourVal = minutes.toDouble() / 2 + baseHour

val minutesVal = minutes.toDouble() * 6

val result = abs(hourVal - minutesVal)

return if (result > 180) 360 - result else result
}
}

fun main() {
val solution = AngleBetweenHandsofaClockKotlin1344()
// 165
println(solution.angleClock(12, 30))
// 75
println(solution.angleClock(3, 30))
// 7.5
println(solution.angleClock(3, 15))
// 155
println(solution.angleClock(4, 50))
// 0
println(solution.angleClock(12, 0))
// 76.5
println(solution.angleClock(1, 57))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package string_integer

class ReverseWordsinaStringKotlin151 {
fun reverseWords(s: String): String =
s.trim()
.split("\\s+".toRegex())
.reversed()
.joinToString(separator = " ")
/*
fun reverseWords(s: String): String {
val list: MutableList<String> = LinkedList()
var index = 0
while (index < s.length) {
if (s[index] == ' ') {
++index
} else {
val stringBuilder = StringBuilder()
while (index < s.length && s[index] != ' ') {
stringBuilder.append(s[index])
++index
}
list.add(stringBuilder.toString())
}
}
val result = StringBuilder()
for (i in list.size - 1 downTo 0) {
result.append(list[i])
if (i != 0){
result.append(" ")
}
}
return result.toString()
}
*/
}

fun main() {
val solution = ReverseWordsinaString151()
// blue is sky the
println(solution.reverseWords("the sky is blue"))
// world! hello
println(solution.reverseWords(" hello world! "))
// example good a
println(solution.reverseWords("a good example"))
}
31 changes: 31 additions & 0 deletions July-LeetCoding-Challenge/16-Pow(x,n)/Pow(x,n).kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package binary_search

class PowxnKotlin50 {
fun myPow(x: Double, n: Int): Double {
return when {
x == 0.0 -> 0.0
n == 1 -> x
n == -1 -> 1 / x
n == 0 -> 1.0
// Int.MIN_VALUE = Int.MAX_VALUE + 1
n == Int.MIN_VALUE -> 1 / powLog(x, Int.MAX_VALUE) * x
n > 0 -> powLog(x, n)
n < 0 -> 1 / powLog(x, -n)
else -> -1.0
}
}

private fun powLog(x: Double, n: Int): Double =
when {
n == 1 -> x
n % 2 == 0 -> {
val result = powLog(x, n.shr(1))
result * result
}
n % 2 == 1 -> {
val result = powLog(x, n.shr(1))
result * result * x
}
else -> -1.0
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package list_array

class TopKFrequentElementsKotlin347 {
fun topKFrequent(nums: IntArray, k: Int): IntArray {
val map: MutableMap<Int, Int> = HashMap()
nums.forEach {
map[it] = map.getOrDefault(it, 0) + 1
}
return map
.toList()
.sortedByDescending(Pair<Int, Int>::second)
.subList(0, k)
.map { it.first }
.toIntArray()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package graph

class CourseScheduleKotlin207 {
fun canFinish(numCourses: Int, prerequisites: Array<IntArray>): Boolean {
// 1 true, -1 false, 0 not judge
val coursesArray = IntArray(numCourses)
val graph: MutableMap<Int, MutableList<Int>> = HashMap()
for (pre in prerequisites) {
graph.computeIfAbsent(pre[1]) { mutableListOf() }.add(pre[0])
}
for (index in coursesArray.indices) {
if (!dfs(index, coursesArray, graph)) {
return false
}
}
return true
}

// true -> can finish
private fun dfs(
current: Int,
coursesArray: IntArray,
graph: Map<Int, List<Int>>
): Boolean {
return when {
coursesArray[current] == -1 -> false
coursesArray[current] == 1 -> true
else -> {
coursesArray[current] = -1
graph[current]?.forEach {
if (!dfs(it, coursesArray, graph)) {
return false
}
}
coursesArray[current] = 1
true
}
}
}
/*
fun canFinish(numCourses: Int, prerequisites: Array<IntArray>): Boolean {
val coursesArray = IntArray(numCourses)
val graph: MutableMap<Int, MutableList<Int>> = HashMap()
for (pre in prerequisites) {
graph.computeIfAbsent(pre[1]) { mutableListOf() }.add(pre[0])
++coursesArray[pre[0]]
}
val queue: Queue<Int> = LinkedList()
coursesArray.forEachIndexed { index, i ->
if (i == 0) {
queue.offer(index)
}
}
while (queue.isNotEmpty()) {
val current = queue.poll()
graph[current]?.forEach {
if (--coursesArray[it] == 0) {
queue.offer(it)
}
}
}
return coursesArray.count { it == 0 } == numCourses
}
*/
}

fun main() {
val solution = CourseScheduleKotlin207()
// true
println(solution.canFinish(2, arrayOf(intArrayOf(0, 1))))
// false
println(solution.canFinish(2, arrayOf(intArrayOf(0, 1), intArrayOf(1, 0))))
// true
println(solution.canFinish(3, arrayOf(intArrayOf(2, 1), intArrayOf(1, 0))))
}
61 changes: 61 additions & 0 deletions July-LeetCoding-Challenge/19-Add-Binary/Add-Binary.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package string_integer

class AddBinaryKotlin67 {
fun addBinary(a: String, b: String): String {
if (a.length > b.length) {
return addBinary(b, a)
}
val diff = b.length - a.length
var next = 0
val result = StringBuilder()
for (index in a.length - 1 downTo 0) {
val currentA = a[index].toString().toInt()
val currentB = b[index + diff].toString().toInt()
when (next + currentA + currentB) {
0 -> result.append(0)
1 -> {
result.append(1)
next = 0
}
2 -> {
result.append(0)
next = 1
}
3 -> {
result.append(1)
next = 1
}
}
}
for (index in diff - 1 downTo 0) {
val currentB = b[index].toString().toInt()
when (currentB + next) {
0 -> result.append(0)
1 -> {
result.append(1)
next = 0
}
2 -> {
result.append(0)
next = 1
}
}
}
if (next == 1) {
result.append(next)
}
return result.reverse().toString()
}
}

fun main() {
val solution = AddBinaryKotlin67()
// 100
println(solution.addBinary("11", "1"))
// 10101
println(solution.addBinary("1010", "1011"))
// 1100
println(solution.addBinary("1011", "1"))
// 1110
println(solution.addBinary("1011", "11"))
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package list_array

class RemoveLinkedListElementsKotlin203 {
class ListNode(var `val`: Int) {
var next: ListNode? = null
}

fun removeElements(head: ListNode?, `val`: Int): ListNode? {
if (head == null) {
return null
}
if (head.`val` == `val`) {
return removeElements(head.next, `val`)
}
head.next = removeElements(head.next, `val`)
return head
}
/*
fun removeElements(head: ListNode?, `val`: Int): ListNode? {
var current: ListNode? = head ?: return head
while (current != null && current.`val` == `val`) {
current = current.next
}
val result = current
var previous = current
current = current?.next
while (current != null) {
if (current.`val` == `val`) {
while (current != null && current.`val` == `val`) {
current = current.next
}
previous!!.next = current
}
previous = current
current = current?.next
}
return result
}
*/
}

fun main() {
val solution = RemoveLinkedListElementsKotlin203()
val l1 = RemoveLinkedListElementsKotlin203.ListNode(1)
val l2 = RemoveLinkedListElementsKotlin203.ListNode(2)
val l22 = RemoveLinkedListElementsKotlin203.ListNode(2)
val l12 = RemoveLinkedListElementsKotlin203.ListNode(1)

l1.next = l2
l2.next = l22
l22.next = l12

val result = solution.removeElements(l1, 2)
println(result)
}
Loading

0 comments on commit 64b4d79

Please sign in to comment.