Skip to content

Commit 43d0483

Browse files
committed
Added tasks 341-400.
1 parent 242f485 commit 43d0483

File tree

31 files changed

+2446
-136
lines changed

31 files changed

+2446
-136
lines changed

README.md

Lines changed: 176 additions & 134 deletions
Large diffs are not rendered by default.

src/main/kotlin/g0101_0200/s0129_sum_root_to_leaf_numbers/readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ import com_github_leetcode.TreeNode
7272
*/
7373
class Solution {
7474
private var sum = 0
75+
7576
fun sumNumbers(root: TreeNode): Int {
7677
recurseSum(root, 0)
7778
return sum

src/main/kotlin/g0101_0200/s0168_excel_sheet_column_title/readme.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ A -> 1 B -> 2 C -> 3 ... Z -> 26 AA -> 27 AB -> 28 ...
3737

3838
```kotlin
3939
class Solution {
40-
fun convertToTitle(n: Int): String {
41-
var num = n
40+
fun convertToTitle(columnNumber: Int): String {
41+
var num = columnNumber
4242
val sb = StringBuilder()
4343
while (num != 0) {
4444
var remainder = num % 26
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](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)
3+
4+
## 341\. Flatten Nested List Iterator
5+
6+
Medium
7+
8+
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.
9+
10+
Implement the `NestedIterator` class:
11+
12+
* `NestedIterator(List<NestedInteger> nestedList)` Initializes the iterator with the nested list `nestedList`.
13+
* `int next()` Returns the next integer in the nested list.
14+
* `boolean hasNext()` Returns `true` if there are still some integers in the nested list and `false` otherwise.
15+
16+
Your code will be tested with the following pseudocode:
17+
18+
initialize iterator with nestedList
19+
res = []
20+
while iterator.hasNext()
21+
append iterator.next() to the end of res
22+
return res
23+
24+
If `res` matches the expected flattened list, then your code will be judged as correct.
25+
26+
**Example 1:**
27+
28+
**Input:** nestedList = \[\[1,1],2,[1,1]]
29+
30+
**Output:** [1,1,2,1,1]
31+
32+
**Explanation:** By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,1,2,1,1].
33+
34+
**Example 2:**
35+
36+
**Input:** nestedList = [1,[4,[6]]]
37+
38+
**Output:** [1,4,6]
39+
40+
**Explanation:** By calling next repeatedly until hasNext returns false, the order of elements returned by next should be: [1,4,6].
41+
42+
**Constraints:**
43+
44+
* `1 <= nestedList.length <= 500`
45+
* The values of the integers in the nested list is in the range <code>[-10<sup>6</sup>, 10<sup>6</sup>]</code>.
46+
47+
## Solution
48+
49+
```kotlin
50+
import com_github_leetcode.NestedInteger
51+
52+
/*
53+
* // This is the interface that allows for creating nested lists.
54+
* // You should not implement it, or speculate about its implementation
55+
* class NestedInteger {
56+
* // Constructor initializes an empty nested list.
57+
* constructor()
58+
*
59+
* // Constructor initializes a single integer.
60+
* constructor(value: Int)
61+
*
62+
* // @return true if this NestedInteger holds a single integer, rather than a nested list.
63+
* fun isInteger(): Boolean
64+
*
65+
* // @return the single integer that this NestedInteger holds, if it holds a single integer
66+
* // Return null if this NestedInteger holds a nested list
67+
* fun getInteger(): Int?
68+
*
69+
* // Set this NestedInteger to hold a single integer.
70+
* fun setInteger(value: Int): Unit
71+
*
72+
* // Set this NestedInteger to hold a nested list and adds a nested integer to it.
73+
* fun add(ni: NestedInteger): Unit
74+
*
75+
* // @return the nested list that this NestedInteger holds, if it holds a nested list
76+
* // Return null if this NestedInteger holds a single integer
77+
* fun getList(): List<NestedInteger>?
78+
* }
79+
*/
80+
class NestedIterator(nestedList: List<NestedInteger>) {
81+
private var flattenList = mutableListOf<Int>()
82+
private var index = 0
83+
84+
init {
85+
flatten(nestedList, flattenList)
86+
}
87+
88+
private fun flatten(nestedList: List<NestedInteger>, flattenList: MutableList<Int>) {
89+
nestedList.forEach { nestedInteger ->
90+
if (nestedInteger.isInteger()) {
91+
flattenList.add(nestedInteger.getInteger()!!)
92+
} else {
93+
flatten(nestedInteger.getList()!!, flattenList)
94+
}
95+
}
96+
}
97+
98+
fun next(): Int = flattenList[index++]
99+
100+
fun hasNext(): Boolean = index < flattenList.size
101+
}
102+
103+
/*
104+
* Your NestedIterator object will be instantiated and called as such:
105+
* var obj = NestedIterator(nestedList)
106+
* var param_1 = obj.next()
107+
* var param_2 = obj.hasNext()
108+
*/
109+
```
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](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)
3+
4+
## 371\. Sum of Two Integers
5+
6+
Medium
7+
8+
Given two integers `a` and `b`, return _the sum of the two integers without using the operators_ `+` _and_ `-`.
9+
10+
**Example 1:**
11+
12+
**Input:** a = 1, b = 2
13+
14+
**Output:** 3
15+
16+
**Example 2:**
17+
18+
**Input:** a = 2, b = 3
19+
20+
**Output:** 5
21+
22+
**Constraints:**
23+
24+
* `-1000 <= a, b <= 1000`
25+
26+
## Solution
27+
28+
```kotlin
29+
@Suppress("NAME_SHADOWING")
30+
class Solution {
31+
fun getSum(a: Int, b: Int): Int {
32+
var a = a
33+
var b = b
34+
var ans = 0
35+
var memo = 0
36+
var exp = 0
37+
var count = 0
38+
while (count < 32) {
39+
val val1 = a and 1
40+
val val2 = b and 1
41+
var `val` = sum(val1, val2, memo)
42+
memo = `val` shr 1
43+
`val` = `val` and 1
44+
a = a shr 1
45+
b = b shr 1
46+
ans = ans or (`val` shl exp)
47+
exp = plusOne(exp)
48+
count = plusOne(count)
49+
}
50+
return ans
51+
}
52+
53+
private fun sum(val1: Int, val2: Int, val3: Int): Int {
54+
var count = 0
55+
if (val1 == 1) {
56+
count = plusOne(count)
57+
}
58+
if (val2 == 1) {
59+
count = plusOne(count)
60+
}
61+
if (val3 == 1) {
62+
count = plusOne(count)
63+
}
64+
return count
65+
}
66+
67+
private fun plusOne(`val`: Int): Int {
68+
return -`val`.inv()
69+
}
70+
}
71+
```
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
2+
[![](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)
3+
4+
## 372\. Super Pow
5+
6+
Medium
7+
8+
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.
9+
10+
**Example 1:**
11+
12+
**Input:** a = 2, b = [3]
13+
14+
**Output:** 8
15+
16+
**Example 2:**
17+
18+
**Input:** a = 2, b = [1,0]
19+
20+
**Output:** 1024
21+
22+
**Example 3:**
23+
24+
**Input:** a = 1, b = [4,3,3,8,5,2]
25+
26+
**Output:** 1
27+
28+
**Constraints:**
29+
30+
* <code>1 <= a <= 2<sup>31</sup> - 1</code>
31+
* `1 <= b.length <= 2000`
32+
* `0 <= b[i] <= 9`
33+
* `b` does not contain leading zeros.
34+
35+
## Solution
36+
37+
```kotlin
38+
@Suppress("NAME_SHADOWING")
39+
class Solution {
40+
fun superPow(a: Int, b: IntArray): Int {
41+
val phi = phi(MOD)
42+
val arrMod = arrMod(b, phi)
43+
return if (isGreaterOrEqual(b, phi)) {
44+
// Cycle has started
45+
// cycle starts at phi with length phi
46+
exp(a % MOD, phi + arrMod)
47+
} else exp(a % MOD, arrMod)
48+
}
49+
50+
private fun phi(n: Int): Int {
51+
var n = n
52+
var result = n.toDouble()
53+
var p = 2
54+
while (p * p <= n) {
55+
if (n % p > 0) {
56+
p++
57+
continue
58+
}
59+
while (n % p == 0) {
60+
n /= p
61+
}
62+
result *= 1.0 - 1.0 / p
63+
p++
64+
}
65+
if (n > 1) {
66+
// if starting n was also prime (so it was greater than sqrt(n))
67+
result *= 1.0 - 1.0 / n
68+
}
69+
return result.toInt()
70+
}
71+
72+
// Returns true if number in array is greater than integer named phi
73+
private fun isGreaterOrEqual(b: IntArray, phi: Int): Boolean {
74+
var cur = 0
75+
for (j in b) {
76+
cur = cur * 10 + j
77+
if (cur >= phi) {
78+
return true
79+
}
80+
}
81+
return false
82+
}
83+
84+
// Returns number in array mod phi
85+
private fun arrMod(b: IntArray, phi: Int): Int {
86+
var res = 0
87+
for (j in b) {
88+
res = (res * 10 + j) % phi
89+
}
90+
return res
91+
}
92+
93+
// Binary exponentiation
94+
private fun exp(a: Int, b: Int): Int {
95+
var a = a
96+
var b = b
97+
var y = 1
98+
while (b > 0) {
99+
if (b % 2 == 1) {
100+
y = y * a % MOD
101+
}
102+
a = a * a % MOD
103+
b /= 2
104+
}
105+
return y
106+
}
107+
108+
companion object {
109+
private const val MOD = 1337
110+
}
111+
}
112+
```

0 commit comments

Comments
 (0)