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
114 changes: 79 additions & 35 deletions README.md

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions src/main/kotlin/g0001_0100/s0002_add_two_numbers/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@ You may assume the two numbers do not contain any leading zero, except the numbe
import com_github_leetcode.ListNode

/*
* Example:
* var li = ListNode(5)
* var v = li.`val`
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* class ListNode(var `val`: Int) {
* var next: ListNode? = null
* }
*/
class Solution {
Expand Down
1 change: 1 addition & 0 deletions src/main/kotlin/g0001_0100/s0050_powx_n/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Implement [pow(x, n)](http://www.cplusplus.com/reference/valarray/pow/), which c
## Solution

```kotlin
@Suppress("NAME_SHADOWING")
class Solution {
fun myPow(x: Double, n: Int): Double {
var x = x
Expand Down
64 changes: 64 additions & 0 deletions src/main/kotlin/g0001_0100/s0059_spiral_matrix_ii/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
[![](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)

## 59\. Spiral Matrix II

Medium

Given a positive integer `n`, generate an `n x n` `matrix` filled with elements from `1` to <code>n<sup>2</sup></code> in spiral order.

**Example 1:**

![](https://assets.leetcode.com/uploads/2020/11/13/spiraln.jpg)

**Input:** n = 3

**Output:** [[1,2,3],[8,9,4],[7,6,5]]

**Example 2:**

**Input:** n = 1

**Output:** [[1]]

**Constraints:**

* `1 <= n <= 20`

## Solution

```kotlin
class Solution {
fun generateMatrix(n: Int): Array<IntArray> {
var num = 1
var rStart = 0
var rEnd = n - 1
var cStart = 0
var cEnd = n - 1
val spiral = Array(n) { IntArray(n) }
while (rStart <= rEnd && cStart <= cEnd) {
for (k in cStart..cEnd) {
spiral[rStart][k] = num++
}
rStart++
for (k in rStart..rEnd) {
spiral[k][cEnd] = num++
}
cEnd--
if (rStart <= rEnd) {
for (k in cEnd downTo cStart) {
spiral[rEnd][k] = num++
}
}
rEnd--
if (cStart <= cEnd) {
for (k in rEnd downTo rStart) {
spiral[k][cStart] = num++
}
}
cStart++
}
return spiral
}
}
```
77 changes: 77 additions & 0 deletions src/main/kotlin/g0001_0100/s0060_permutation_sequence/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
[![](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)

## 60\. Permutation Sequence

Hard

The set `[1, 2, 3, ..., n]` contains a total of `n!` unique permutations.

By listing and labeling all of the permutations in order, we get the following sequence for `n = 3`:

1. `"123"`
2. `"132"`
3. `"213"`
4. `"231"`
5. `"312"`
6. `"321"`

Given `n` and `k`, return the <code>k<sup>th</sup></code> permutation sequence.

**Example 1:**

**Input:** n = 3, k = 3

**Output:** "213"

**Example 2:**

**Input:** n = 4, k = 9

**Output:** "2314"

**Example 3:**

**Input:** n = 3, k = 1

**Output:** "123"

**Constraints:**

* `1 <= n <= 9`
* `1 <= k <= n!`

## Solution

```kotlin
@Suppress("NAME_SHADOWING")
class Solution {
fun getPermutation(n: Int, k: Int): String {
var k = k
val res = CharArray(n)
// We want the permutation sequence to be zero-indexed
k = k - 1
// The set bits indicate the available digits
var a = (1 shl n) - 1
var m = 1
for (i in 2 until n) {
// m = (n - 1)!
m *= i
}
for (i in 0 until n) {
var b = a
for (j in 0 until k / m) {
b = b and b - 1
}
// b is the bit corresponding to the digit we want
b = b and b.inv() + 1
res[i] = ('1'.code + Integer.bitCount(b - 1)).toChar()
// Remove b from the set of available digits
a = a and b.inv()
k %= m
m /= Math.max(1, n - i - 1)
}
return String(res)
}
}
```
80 changes: 80 additions & 0 deletions src/main/kotlin/g0001_0100/s0061_rotate_list/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
[![](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)

## 61\. Rotate List

Medium

Given the `head` of a linked list, rotate the list to the right by `k` places.

**Example 1:**

![](https://assets.leetcode.com/uploads/2020/11/13/rotate1.jpg)

**Input:** head = [1,2,3,4,5], k = 2

**Output:** [4,5,1,2,3]

**Example 2:**

![](https://assets.leetcode.com/uploads/2020/11/13/roate2.jpg)

**Input:** head = [0,1,2], k = 4

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

**Constraints:**

* The number of nodes in the list is in the range `[0, 500]`.
* `-100 <= Node.val <= 100`
* <code>0 <= k <= 2 * 10<sup>9</sup></code>

## Solution

```kotlin
import com_github_leetcode.ListNode

/**
* Example:
* var li = ListNode(5)
* var v = li.`val`
* Definition for singly-linked list.
* class ListNode(var `val`: Int) {
* var next: ListNode? = null
* }
*/
class Solution {
fun rotateRight(head: ListNode?, k: Int): ListNode? {
if (head == null || k == 0) {
return head
}
var tail = head
// find the count and let tail points to last node
var count = 1
while (tail != null && tail.next != null) {
count++
tail = tail.next
}
// calculate number of times to rotate by count modulas
val times = k % count
if (times == 0) {
return head
}
var temp = head
// iterate and go to the K+1 th node from the end or count - K - 1 node from
// start
var i = 1
while (i <= count - times - 1 && temp != null) {
temp = temp.next
i++
}
var newHead: ListNode? = null
if (temp != null && tail != null) {
newHead = temp.next
temp.next = null
tail.next = head
}
return newHead
}
}
```
85 changes: 85 additions & 0 deletions src/main/kotlin/g0001_0100/s0063_unique_paths_ii/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
[![](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)

## 63\. Unique Paths II

Medium

You are given an `m x n` integer array `grid`. There is a robot initially located at the **top-left corner** (i.e., `grid[0][0]`). The robot tries to move to the **bottom-right corner** (i.e., `grid[m-1][n-1]`). The robot can only move either down or right at any point in time.

An obstacle and space are marked as `1` or `0` respectively in `grid`. A path that the robot takes cannot include **any** square that is an obstacle.

Return _the number of possible unique paths that the robot can take to reach the bottom-right corner_.

The testcases are generated so that the answer will be less than or equal to <code>2 * 10<sup>9</sup></code>.

**Example 1:**

![](https://assets.leetcode.com/uploads/2020/11/04/robot1.jpg)

**Input:** obstacleGrid = \[\[0,0,0],[0,1,0],[0,0,0]]

**Output:** 2

**Explanation:** There is one obstacle in the middle of the 3x3 grid above.

There are two ways to reach the bottom-right corner:

1. Right -> Right -> Down -> Down

2. Down -> Down -> Right -> Right

**Example 2:**

![](https://assets.leetcode.com/uploads/2020/11/04/robot2.jpg)

**Input:** obstacleGrid = \[\[0,1],[0,0]]

**Output:** 1

**Constraints:**

* `m == obstacleGrid.length`
* `n == obstacleGrid[i].length`
* `1 <= m, n <= 100`
* `obstacleGrid[i][j]` is `0` or `1`.

## Solution

```kotlin
class Solution {
fun uniquePathsWithObstacles(obstacleGrid: Array<IntArray>): Int {
// if start point has obstacle, there's no path
if (obstacleGrid[0][0] == 1) {
return 0
}
obstacleGrid[0][0] = 1
val m = obstacleGrid.size
val n: Int = obstacleGrid[0].size
for (i in 1 until m) {
if (obstacleGrid[i][0] == 1) {
obstacleGrid[i][0] = 0
} else {
obstacleGrid[i][0] = obstacleGrid[i - 1][0]
}
}
for (j in 1 until n) {
if (obstacleGrid[0][j] == 1) {
obstacleGrid[0][j] = 0
} else {
obstacleGrid[0][j] = obstacleGrid[0][j - 1]
}
}
for (i in 1 until m) {
for (j in 1 until n) {
if (obstacleGrid[i][j] == 1) {
obstacleGrid[i][j] = 0
} else {
obstacleGrid[i][j] = obstacleGrid[i - 1][j] + obstacleGrid[i][j - 1]
}
}
}
return obstacleGrid[m - 1][n - 1]
}
}
```
Loading