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
358 changes: 234 additions & 124 deletions README.md

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ The overall run time complexity should be `O(log (m+n))`.
## Solution

```kotlin
import kotlin.collections.ArrayList

class Solution {
fun findMedianSortedArrays(nums1: IntArray, nums2: IntArray): Double {
val l: MutableList<Int> = ArrayList()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ Since -91283472332 is less than the lower bound of the range [-2<sup>31</sup>, 2

```kotlin
class Solution {
fun myAtoi(str: String?): Int {
if (str.isNullOrEmpty()) {
fun myAtoi(str: String): Int {
if (str.isEmpty()) {
return 0
}
var i = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ Given an integer, convert it to a roman numeral.

```kotlin
class Solution {
fun intToRoman(num: Int): String? {
fun intToRoman(num: Int): String {
var localNum = num
val sb = StringBuilder()
val m = 1000
Expand Down
2 changes: 0 additions & 2 deletions src/main/kotlin/g0001_0100/s0015_3sum/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@ Notice that the solution set must not contain duplicate triplets.
## Solution

```kotlin
import kotlin.collections.ArrayList

class Solution {
fun threeSum(nums: IntArray): List<List<Int>> {
nums.sort()
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/g0001_0100/s0016_3sum_closest/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ You may assume that each input would have exactly one solution.

```kotlin
class Solution {
fun threeSumClosest(nums: IntArray?, target: Int): Int {
if (nums == null || nums.size < 3) {
fun threeSumClosest(nums: IntArray, target: Int): Int {
if (nums.size < 3) {
return 0
}
if (nums.size == 3) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ You may not alter the values in the list's nodes, only nodes themselves may be c
## Solution

```kotlin
9
import com_github_leetcode.ListNode

/*
Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/g0001_0100/s0031_next_permutation/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ class Solution {
while (j >= i + 1 && nums[i] >= nums[j]) { j-- }
nums[i] = nums[j].also { nums[j] = nums[i] }
}
var l = i + 1; var r = nums.size - 1
var l = i + 1
var r = nums.size - 1
while (l < r) {
nums[l] = nums[r].also { nums[r] = nums[l] }
l++
Expand Down
14 changes: 7 additions & 7 deletions src/main/kotlin/g0001_0100/s0044_wildcard_matching/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,18 @@ The matching should cover the **entire** input string (not partial).

```kotlin
class Solution {
fun isMatch(inputString: String, pattern: String): Boolean {
fun isMatch(s: String, p: String): Boolean {
var i = 0
var j = 0
var starIdx = -1
var lastMatch = -1
while (i < inputString.length) {
if (j < pattern.length &&
(inputString[i] == pattern[j] || pattern[j] == '?')
while (i < s.length) {
if (j < p.length &&
(s[i] == p[j] || p[j] == '?')
) {
i++
j++
} else if (j < pattern.length && pattern[j] == '*') {
} else if (j < p.length && p[j] == '*') {
starIdx = j
lastMatch = i
j++
Expand All @@ -76,10 +76,10 @@ class Solution {
}
}
var isMatch = true
while (j < pattern.length && pattern[j] == '*') {
while (j < p.length && p[j] == '*') {
j++
}
if (i != inputString.length || j != pattern.length) {
if (i != s.length || j != p.length) {
isMatch = false
}
return isMatch
Expand Down
38 changes: 38 additions & 0 deletions src/main/kotlin/g2401_2500/s2413_smallest_even_multiple/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
[![](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)

## 2413\. Smallest Even Multiple

Easy

Given a **positive** integer `n`, return _the smallest positive integer that is a multiple of **both**_ `2` _and_ `n`.

**Example 1:**

**Input:** n = 5

**Output:** 10

**Explanation:** The smallest multiple of both 5 and 2 is 10.

**Example 2:**

**Input:** n = 6

**Output:** 6

**Explanation:** The smallest multiple of both 6 and 2 is 6. Note that a number is a multiple of itself.

**Constraints:**

* `1 <= n <= 150`

## Solution

```kotlin
class Solution {
fun smallestEvenMultiple(n: Int): Int {
return if (n % 2 == 0) n else n * 2
}
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
[![](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)

## 2414\. Length of the Longest Alphabetical Continuous Substring

Medium

An **alphabetical continuous string** is a string consisting of consecutive letters in the alphabet. In other words, it is any substring of the string `"abcdefghijklmnopqrstuvwxyz"`.

* For example, `"abc"` is an alphabetical continuous string, while `"acb"` and `"za"` are not.

Given a string `s` consisting of lowercase letters only, return the _length of the **longest** alphabetical continuous substring._

**Example 1:**

**Input:** s = "abacaba"

**Output:** 2

**Explanation:** There are 4 distinct continuous substrings: "a", "b", "c" and "ab".

"ab" is the longest continuous substring.

**Example 2:**

**Input:** s = "abcde"

**Output:** 5

**Explanation:** "abcde" is the longest continuous substring.

**Constraints:**

* <code>1 <= s.length <= 10<sup>5</sup></code>
* `s` consists of only English lowercase letters.

## Solution

```kotlin
class Solution {
fun longestContinuousSubstring(s: String): Int {
var ans = 0
var cnt = 1
var j = 1
while (j < s.length) {
if (s[j].code == s[j - 1].code + 1) {
cnt++
} else {
ans = ans.coerceAtLeast(cnt)
cnt = 1
}
j++
}
return ans.coerceAtLeast(cnt)
}
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
[![](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)

## 2415\. Reverse Odd Levels of Binary Tree

Medium

Given the `root` of a **perfect** binary tree, reverse the node values at each **odd** level of the tree.

* For example, suppose the node values at level 3 are `[2,1,3,4,7,11,29,18]`, then it should become `[18,29,11,7,4,3,1,2]`.

Return _the root of the reversed tree_.

A binary tree is **perfect** if all parent nodes have two children and all leaves are on the same level.

The **level** of a node is the number of edges along the path between it and the root node.

**Example 1:**

![](https://assets.leetcode.com/uploads/2022/07/28/first_case1.png)

**Input:** root = [2,3,5,8,13,21,34]

**Output:** [2,5,3,8,13,21,34]

**Explanation:**

The tree has only one odd level.

The nodes at level 1 are 3, 5 respectively, which are reversed and become 5, 3.

**Example 2:**

![](https://assets.leetcode.com/uploads/2022/07/28/second_case3.png)

**Input:** root = [7,13,11]

**Output:** [7,11,13]

**Explanation:**

The nodes at level 1 are 13, 11, which are reversed and become 11, 13.

**Example 3:**

**Input:** root = [0,1,2,0,0,0,0,1,1,1,1,2,2,2,2]

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

**Explanation:**

The odd levels have non-zero values.

The nodes at level 1 were 1, 2, and are 2, 1 after the reversal.

The nodes at level 3 were 1, 1, 1, 1, 2, 2, 2, 2, and are 2, 2, 2, 2, 1, 1, 1, 1 after the reversal.

**Constraints:**

* The number of nodes in the tree is in the range <code>[1, 2<sup>14</sup>]</code>.
* <code>0 <= Node.val <= 10<sup>5</sup></code>
* `root` is a **perfect** binary tree.

## Solution

```kotlin
import com_github_leetcode.TreeNode
import java.util.LinkedList
import java.util.Queue

/*
* Example:
* var ti = TreeNode(5)
* var v = ti.`val`
* Definition for a binary tree node.
* class TreeNode(var `val`: Int) {
* var left: TreeNode? = null
* var right: TreeNode? = null
* }
*/
class Solution {
private val list: MutableList<Int> = ArrayList()
fun reverseOddLevels(root: TreeNode): TreeNode? {
solve(root)
return enrich(list, 0)
}

private fun enrich(list: List<Int>, i: Int): TreeNode? {
var root: TreeNode? = null
if (i < list.size) {
root = TreeNode(list[i])
root.left = enrich(list, 2 * i + 1)
root.right = enrich(list, 2 * i + 2)
}
return root
}

private fun solve(root: TreeNode) {
val q: Queue<TreeNode?> = LinkedList()
q.add(root)
var level = 0
while (q.isNotEmpty()) {
val size = q.size
val res: MutableList<Int> = ArrayList()
for (i in 0 until size) {
val cur = q.remove()
res.add(cur!!.`val`)
if (cur.left != null) {
q.add(cur.left)
}
if (cur.right != null) {
q.add(cur.right)
}
}
if (level % 2 != 0) {
for (i in res.indices.reversed()) {
list.add(res[i])
}
} else {
list.addAll(res)
}
level++
}
}
}
```
Loading