Skip to content

Commit 64b4d79

Browse files
add kotlin solutions
1 parent 5dd7167 commit 64b4d79

File tree

13 files changed

+573
-0
lines changed

13 files changed

+573
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package string_integer
2+
3+
import kotlin.math.abs
4+
5+
class AngleBetweenHandsofaClockKotlin1344 {
6+
fun angleClock(hour: Int, minutes: Int): Double {
7+
val baseHour = when (hour) {
8+
12 -> 0
9+
else -> 30 * hour
10+
}
11+
val hourVal = minutes.toDouble() / 2 + baseHour
12+
13+
val minutesVal = minutes.toDouble() * 6
14+
15+
val result = abs(hourVal - minutesVal)
16+
17+
return if (result > 180) 360 - result else result
18+
}
19+
}
20+
21+
fun main() {
22+
val solution = AngleBetweenHandsofaClockKotlin1344()
23+
// 165
24+
println(solution.angleClock(12, 30))
25+
// 75
26+
println(solution.angleClock(3, 30))
27+
// 7.5
28+
println(solution.angleClock(3, 15))
29+
// 155
30+
println(solution.angleClock(4, 50))
31+
// 0
32+
println(solution.angleClock(12, 0))
33+
// 76.5
34+
println(solution.angleClock(1, 57))
35+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package string_integer
2+
3+
class ReverseWordsinaStringKotlin151 {
4+
fun reverseWords(s: String): String =
5+
s.trim()
6+
.split("\\s+".toRegex())
7+
.reversed()
8+
.joinToString(separator = " ")
9+
/*
10+
fun reverseWords(s: String): String {
11+
val list: MutableList<String> = LinkedList()
12+
var index = 0
13+
while (index < s.length) {
14+
if (s[index] == ' ') {
15+
++index
16+
} else {
17+
val stringBuilder = StringBuilder()
18+
while (index < s.length && s[index] != ' ') {
19+
stringBuilder.append(s[index])
20+
++index
21+
}
22+
list.add(stringBuilder.toString())
23+
}
24+
}
25+
val result = StringBuilder()
26+
for (i in list.size - 1 downTo 0) {
27+
result.append(list[i])
28+
if (i != 0){
29+
result.append(" ")
30+
}
31+
}
32+
return result.toString()
33+
}
34+
*/
35+
}
36+
37+
fun main() {
38+
val solution = ReverseWordsinaString151()
39+
// blue is sky the
40+
println(solution.reverseWords("the sky is blue"))
41+
// world! hello
42+
println(solution.reverseWords(" hello world! "))
43+
// example good a
44+
println(solution.reverseWords("a good example"))
45+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package binary_search
2+
3+
class PowxnKotlin50 {
4+
fun myPow(x: Double, n: Int): Double {
5+
return when {
6+
x == 0.0 -> 0.0
7+
n == 1 -> x
8+
n == -1 -> 1 / x
9+
n == 0 -> 1.0
10+
// Int.MIN_VALUE = Int.MAX_VALUE + 1
11+
n == Int.MIN_VALUE -> 1 / powLog(x, Int.MAX_VALUE) * x
12+
n > 0 -> powLog(x, n)
13+
n < 0 -> 1 / powLog(x, -n)
14+
else -> -1.0
15+
}
16+
}
17+
18+
private fun powLog(x: Double, n: Int): Double =
19+
when {
20+
n == 1 -> x
21+
n % 2 == 0 -> {
22+
val result = powLog(x, n.shr(1))
23+
result * result
24+
}
25+
n % 2 == 1 -> {
26+
val result = powLog(x, n.shr(1))
27+
result * result * x
28+
}
29+
else -> -1.0
30+
}
31+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package list_array
2+
3+
class TopKFrequentElementsKotlin347 {
4+
fun topKFrequent(nums: IntArray, k: Int): IntArray {
5+
val map: MutableMap<Int, Int> = HashMap()
6+
nums.forEach {
7+
map[it] = map.getOrDefault(it, 0) + 1
8+
}
9+
return map
10+
.toList()
11+
.sortedByDescending(Pair<Int, Int>::second)
12+
.subList(0, k)
13+
.map { it.first }
14+
.toIntArray()
15+
}
16+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package graph
2+
3+
class CourseScheduleKotlin207 {
4+
fun canFinish(numCourses: Int, prerequisites: Array<IntArray>): Boolean {
5+
// 1 true, -1 false, 0 not judge
6+
val coursesArray = IntArray(numCourses)
7+
val graph: MutableMap<Int, MutableList<Int>> = HashMap()
8+
for (pre in prerequisites) {
9+
graph.computeIfAbsent(pre[1]) { mutableListOf() }.add(pre[0])
10+
}
11+
for (index in coursesArray.indices) {
12+
if (!dfs(index, coursesArray, graph)) {
13+
return false
14+
}
15+
}
16+
return true
17+
}
18+
19+
// true -> can finish
20+
private fun dfs(
21+
current: Int,
22+
coursesArray: IntArray,
23+
graph: Map<Int, List<Int>>
24+
): Boolean {
25+
return when {
26+
coursesArray[current] == -1 -> false
27+
coursesArray[current] == 1 -> true
28+
else -> {
29+
coursesArray[current] = -1
30+
graph[current]?.forEach {
31+
if (!dfs(it, coursesArray, graph)) {
32+
return false
33+
}
34+
}
35+
coursesArray[current] = 1
36+
true
37+
}
38+
}
39+
}
40+
/*
41+
fun canFinish(numCourses: Int, prerequisites: Array<IntArray>): Boolean {
42+
val coursesArray = IntArray(numCourses)
43+
val graph: MutableMap<Int, MutableList<Int>> = HashMap()
44+
for (pre in prerequisites) {
45+
graph.computeIfAbsent(pre[1]) { mutableListOf() }.add(pre[0])
46+
++coursesArray[pre[0]]
47+
}
48+
val queue: Queue<Int> = LinkedList()
49+
coursesArray.forEachIndexed { index, i ->
50+
if (i == 0) {
51+
queue.offer(index)
52+
}
53+
}
54+
while (queue.isNotEmpty()) {
55+
val current = queue.poll()
56+
graph[current]?.forEach {
57+
if (--coursesArray[it] == 0) {
58+
queue.offer(it)
59+
}
60+
}
61+
}
62+
return coursesArray.count { it == 0 } == numCourses
63+
}
64+
*/
65+
}
66+
67+
fun main() {
68+
val solution = CourseScheduleKotlin207()
69+
// true
70+
println(solution.canFinish(2, arrayOf(intArrayOf(0, 1))))
71+
// false
72+
println(solution.canFinish(2, arrayOf(intArrayOf(0, 1), intArrayOf(1, 0))))
73+
// true
74+
println(solution.canFinish(3, arrayOf(intArrayOf(2, 1), intArrayOf(1, 0))))
75+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package string_integer
2+
3+
class AddBinaryKotlin67 {
4+
fun addBinary(a: String, b: String): String {
5+
if (a.length > b.length) {
6+
return addBinary(b, a)
7+
}
8+
val diff = b.length - a.length
9+
var next = 0
10+
val result = StringBuilder()
11+
for (index in a.length - 1 downTo 0) {
12+
val currentA = a[index].toString().toInt()
13+
val currentB = b[index + diff].toString().toInt()
14+
when (next + currentA + currentB) {
15+
0 -> result.append(0)
16+
1 -> {
17+
result.append(1)
18+
next = 0
19+
}
20+
2 -> {
21+
result.append(0)
22+
next = 1
23+
}
24+
3 -> {
25+
result.append(1)
26+
next = 1
27+
}
28+
}
29+
}
30+
for (index in diff - 1 downTo 0) {
31+
val currentB = b[index].toString().toInt()
32+
when (currentB + next) {
33+
0 -> result.append(0)
34+
1 -> {
35+
result.append(1)
36+
next = 0
37+
}
38+
2 -> {
39+
result.append(0)
40+
next = 1
41+
}
42+
}
43+
}
44+
if (next == 1) {
45+
result.append(next)
46+
}
47+
return result.reverse().toString()
48+
}
49+
}
50+
51+
fun main() {
52+
val solution = AddBinaryKotlin67()
53+
// 100
54+
println(solution.addBinary("11", "1"))
55+
// 10101
56+
println(solution.addBinary("1010", "1011"))
57+
// 1100
58+
println(solution.addBinary("1011", "1"))
59+
// 1110
60+
println(solution.addBinary("1011", "11"))
61+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package list_array
2+
3+
class RemoveLinkedListElementsKotlin203 {
4+
class ListNode(var `val`: Int) {
5+
var next: ListNode? = null
6+
}
7+
8+
fun removeElements(head: ListNode?, `val`: Int): ListNode? {
9+
if (head == null) {
10+
return null
11+
}
12+
if (head.`val` == `val`) {
13+
return removeElements(head.next, `val`)
14+
}
15+
head.next = removeElements(head.next, `val`)
16+
return head
17+
}
18+
/*
19+
fun removeElements(head: ListNode?, `val`: Int): ListNode? {
20+
var current: ListNode? = head ?: return head
21+
while (current != null && current.`val` == `val`) {
22+
current = current.next
23+
}
24+
val result = current
25+
var previous = current
26+
current = current?.next
27+
while (current != null) {
28+
if (current.`val` == `val`) {
29+
while (current != null && current.`val` == `val`) {
30+
current = current.next
31+
}
32+
previous!!.next = current
33+
}
34+
previous = current
35+
current = current?.next
36+
}
37+
return result
38+
}
39+
*/
40+
}
41+
42+
fun main() {
43+
val solution = RemoveLinkedListElementsKotlin203()
44+
val l1 = RemoveLinkedListElementsKotlin203.ListNode(1)
45+
val l2 = RemoveLinkedListElementsKotlin203.ListNode(2)
46+
val l22 = RemoveLinkedListElementsKotlin203.ListNode(2)
47+
val l12 = RemoveLinkedListElementsKotlin203.ListNode(1)
48+
49+
l1.next = l2
50+
l2.next = l22
51+
l22.next = l12
52+
53+
val result = solution.removeElements(l1, 2)
54+
println(result)
55+
}

0 commit comments

Comments
 (0)