Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
310 changes: 176 additions & 134 deletions README.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ import com_github_leetcode.TreeNode
*/
class Solution {
private var sum = 0

fun sumNumbers(root: TreeNode): Int {
recurseSum(root, 0)
return sum
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 ...

```kotlin
class Solution {
fun convertToTitle(n: Int): String {
var num = n
fun convertToTitle(columnNumber: Int): String {
var num = columnNumber
val sb = StringBuilder()
while (num != 0) {
var remainder = num % 26
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)

## 341\. Flatten Nested List Iterator

Medium

You are given a nested list of integers `nestedList`. Each element is either an integer or a list whose elements may also be integers or other lists. Implement an iterator to flatten it.

Implement the `NestedIterator` class:

* `NestedIterator(List<NestedInteger> nestedList)` Initializes the iterator with the nested list `nestedList`.
* `int next()` Returns the next integer in the nested list.
* `boolean hasNext()` Returns `true` if there are still some integers in the nested list and `false` otherwise.

Your code will be tested with the following pseudocode:

initialize iterator with nestedList
res = []
while iterator.hasNext()
append iterator.next() to the end of res
return res

If `res` matches the expected flattened list, then your code will be judged as correct.

**Example 1:**

**Input:** nestedList = \[\[1,1],2,[1,1]]

**Output:** [1,1,2,1,1]

**Explanation:** By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,1,2,1,1].

**Example 2:**

**Input:** nestedList = [1,[4,[6]]]

**Output:** [1,4,6]

**Explanation:** By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,4,6].

**Constraints:**

* `1 <= nestedList.length <= 500`
* The values of the integers in the nested list is in the range <code>[-10<sup>6</sup>, 10<sup>6</sup>]</code>.

## Solution

```kotlin
import com_github_leetcode.NestedInteger

/*
* // This is the interface that allows for creating nested lists.
* // You should not implement it, or speculate about its implementation
* class NestedInteger {
* // Constructor initializes an empty nested list.
* constructor()
*
* // Constructor initializes a single integer.
* constructor(value: Int)
*
* // @return true if this NestedInteger holds a single integer, rather than a nested list.
* fun isInteger(): Boolean
*
* // @return the single integer that this NestedInteger holds, if it holds a single integer
* // Return null if this NestedInteger holds a nested list
* fun getInteger(): Int?
*
* // Set this NestedInteger to hold a single integer.
* fun setInteger(value: Int): Unit
*
* // Set this NestedInteger to hold a nested list and adds a nested integer to it.
* fun add(ni: NestedInteger): Unit
*
* // @return the nested list that this NestedInteger holds, if it holds a nested list
* // Return null if this NestedInteger holds a single integer
* fun getList(): List<NestedInteger>?
* }
*/
class NestedIterator(nestedList: List<NestedInteger>) {
private var flattenList = mutableListOf<Int>()
private var index = 0

init {
flatten(nestedList, flattenList)
}

private fun flatten(nestedList: List<NestedInteger>, flattenList: MutableList<Int>) {
nestedList.forEach { nestedInteger ->
if (nestedInteger.isInteger()) {
flattenList.add(nestedInteger.getInteger()!!)
} else {
flatten(nestedInteger.getList()!!, flattenList)
}
}
}

fun next(): Int = flattenList[index++]

fun hasNext(): Boolean = index < flattenList.size
}

/*
* Your NestedIterator object will be instantiated and called as such:
* var obj = NestedIterator(nestedList)
* var param_1 = obj.next()
* var param_2 = obj.hasNext()
*/
```
71 changes: 71 additions & 0 deletions src/main/kotlin/g0301_0400/s0371_sum_of_two_integers/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)

## 371\. Sum of Two Integers

Medium

Given two integers `a` and `b`, return _the sum of the two integers without using the operators_ `+` _and_ `-`.

**Example 1:**

**Input:** a = 1, b = 2

**Output:** 3

**Example 2:**

**Input:** a = 2, b = 3

**Output:** 5

**Constraints:**

* `-1000 <= a, b <= 1000`

## Solution

```kotlin
@Suppress("NAME_SHADOWING")
class Solution {
fun getSum(a: Int, b: Int): Int {
var a = a
var b = b
var ans = 0
var memo = 0
var exp = 0
var count = 0
while (count < 32) {
val val1 = a and 1
val val2 = b and 1
var `val` = sum(val1, val2, memo)
memo = `val` shr 1
`val` = `val` and 1
a = a shr 1
b = b shr 1
ans = ans or (`val` shl exp)
exp = plusOne(exp)
count = plusOne(count)
}
return ans
}

private fun sum(val1: Int, val2: Int, val3: Int): Int {
var count = 0
if (val1 == 1) {
count = plusOne(count)
}
if (val2 == 1) {
count = plusOne(count)
}
if (val3 == 1) {
count = plusOne(count)
}
return count
}

private fun plusOne(`val`: Int): Int {
return -`val`.inv()
}
}
```
112 changes: 112 additions & 0 deletions src/main/kotlin/g0301_0400/s0372_super_pow/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
[![](https://img.shields.io/github/forks/javadev/LeetCode-in-Kotlin?label=Fork%20me%20on%20GitHub%20&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin/fork)

## 372\. Super Pow

Medium

Your task is to calculate <code>a<sup>b</sup></code> mod `1337` where `a` is a positive integer and `b` is an extremely large positive integer given in the form of an array.

**Example 1:**

**Input:** a = 2, b = [3]

**Output:** 8

**Example 2:**

**Input:** a = 2, b = [1,0]

**Output:** 1024

**Example 3:**

**Input:** a = 1, b = [4,3,3,8,5,2]

**Output:** 1

**Constraints:**

* <code>1 <= a <= 2<sup>31</sup> - 1</code>
* `1 <= b.length <= 2000`
* `0 <= b[i] <= 9`
* `b` does not contain leading zeros.

## Solution

```kotlin
@Suppress("NAME_SHADOWING")
class Solution {
fun superPow(a: Int, b: IntArray): Int {
val phi = phi(MOD)
val arrMod = arrMod(b, phi)
return if (isGreaterOrEqual(b, phi)) {
// Cycle has started
// cycle starts at phi with length phi
exp(a % MOD, phi + arrMod)
} else exp(a % MOD, arrMod)
}

private fun phi(n: Int): Int {
var n = n
var result = n.toDouble()
var p = 2
while (p * p <= n) {
if (n % p > 0) {
p++
continue
}
while (n % p == 0) {
n /= p
}
result *= 1.0 - 1.0 / p
p++
}
if (n > 1) {
// if starting n was also prime (so it was greater than sqrt(n))
result *= 1.0 - 1.0 / n
}
return result.toInt()
}

// Returns true if number in array is greater than integer named phi
private fun isGreaterOrEqual(b: IntArray, phi: Int): Boolean {
var cur = 0
for (j in b) {
cur = cur * 10 + j
if (cur >= phi) {
return true
}
}
return false
}

// Returns number in array mod phi
private fun arrMod(b: IntArray, phi: Int): Int {
var res = 0
for (j in b) {
res = (res * 10 + j) % phi
}
return res
}

// Binary exponentiation
private fun exp(a: Int, b: Int): Int {
var a = a
var b = b
var y = 1
while (b > 0) {
if (b % 2 == 1) {
y = y * a % MOD
}
a = a * a % MOD
b /= 2
}
return y
}

companion object {
private const val MOD = 1337
}
}
```
Loading