Skip to content

Commit 4c28751

Browse files
authored
Added tasks 59-97.
1 parent ed6dea3 commit 4c28751

File tree

39 files changed

+2200
-40
lines changed

39 files changed

+2200
-40
lines changed

README.md

Lines changed: 79 additions & 35 deletions
Large diffs are not rendered by default.

src/main/kotlin/g0001_0100/s0002_add_two_numbers/readme.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,12 @@ You may assume the two numbers do not contain any leading zero, except the numbe
4343
import com_github_leetcode.ListNode
4444

4545
/*
46+
* Example:
47+
* var li = ListNode(5)
48+
* var v = li.`val`
4649
* Definition for singly-linked list.
47-
* public class ListNode {
48-
* int val;
49-
* ListNode next;
50-
* ListNode(int x) { val = x; }
50+
* class ListNode(var `val`: Int) {
51+
* var next: ListNode? = null
5152
* }
5253
*/
5354
class Solution {

src/main/kotlin/g0001_0100/s0050_powx_n/readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ Implement [pow(x, n)](http://www.cplusplus.com/reference/valarray/pow/), which c
3737
## Solution
3838

3939
```kotlin
40+
@Suppress("NAME_SHADOWING")
4041
class Solution {
4142
fun myPow(x: Double, n: Int): Double {
4243
var x = x
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
## 59\. Spiral Matrix II
5+
6+
Medium
7+
8+
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.
9+
10+
**Example 1:**
11+
12+
![](https://assets.leetcode.com/uploads/2020/11/13/spiraln.jpg)
13+
14+
**Input:** n = 3
15+
16+
**Output:** [[1,2,3],[8,9,4],[7,6,5]]
17+
18+
**Example 2:**
19+
20+
**Input:** n = 1
21+
22+
**Output:** [[1]]
23+
24+
**Constraints:**
25+
26+
* `1 <= n <= 20`
27+
28+
## Solution
29+
30+
```kotlin
31+
class Solution {
32+
fun generateMatrix(n: Int): Array<IntArray> {
33+
var num = 1
34+
var rStart = 0
35+
var rEnd = n - 1
36+
var cStart = 0
37+
var cEnd = n - 1
38+
val spiral = Array(n) { IntArray(n) }
39+
while (rStart <= rEnd && cStart <= cEnd) {
40+
for (k in cStart..cEnd) {
41+
spiral[rStart][k] = num++
42+
}
43+
rStart++
44+
for (k in rStart..rEnd) {
45+
spiral[k][cEnd] = num++
46+
}
47+
cEnd--
48+
if (rStart <= rEnd) {
49+
for (k in cEnd downTo cStart) {
50+
spiral[rEnd][k] = num++
51+
}
52+
}
53+
rEnd--
54+
if (cStart <= cEnd) {
55+
for (k in rEnd downTo rStart) {
56+
spiral[k][cStart] = num++
57+
}
58+
}
59+
cStart++
60+
}
61+
return spiral
62+
}
63+
}
64+
```
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
## 60\. Permutation Sequence
5+
6+
Hard
7+
8+
The set `[1, 2, 3, ..., n]` contains a total of `n!` unique permutations.
9+
10+
By listing and labeling all of the permutations in order, we get the following sequence for `n = 3`:
11+
12+
1. `"123"`
13+
2. `"132"`
14+
3. `"213"`
15+
4. `"231"`
16+
5. `"312"`
17+
6. `"321"`
18+
19+
Given `n` and `k`, return the <code>k<sup>th</sup></code> permutation sequence.
20+
21+
**Example 1:**
22+
23+
**Input:** n = 3, k = 3
24+
25+
**Output:** "213"
26+
27+
**Example 2:**
28+
29+
**Input:** n = 4, k = 9
30+
31+
**Output:** "2314"
32+
33+
**Example 3:**
34+
35+
**Input:** n = 3, k = 1
36+
37+
**Output:** "123"
38+
39+
**Constraints:**
40+
41+
* `1 <= n <= 9`
42+
* `1 <= k <= n!`
43+
44+
## Solution
45+
46+
```kotlin
47+
@Suppress("NAME_SHADOWING")
48+
class Solution {
49+
fun getPermutation(n: Int, k: Int): String {
50+
var k = k
51+
val res = CharArray(n)
52+
// We want the permutation sequence to be zero-indexed
53+
k = k - 1
54+
// The set bits indicate the available digits
55+
var a = (1 shl n) - 1
56+
var m = 1
57+
for (i in 2 until n) {
58+
// m = (n - 1)!
59+
m *= i
60+
}
61+
for (i in 0 until n) {
62+
var b = a
63+
for (j in 0 until k / m) {
64+
b = b and b - 1
65+
}
66+
// b is the bit corresponding to the digit we want
67+
b = b and b.inv() + 1
68+
res[i] = ('1'.code + Integer.bitCount(b - 1)).toChar()
69+
// Remove b from the set of available digits
70+
a = a and b.inv()
71+
k %= m
72+
m /= Math.max(1, n - i - 1)
73+
}
74+
return String(res)
75+
}
76+
}
77+
```
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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+
## 61\. Rotate List
5+
6+
Medium
7+
8+
Given the `head` of a linked list, rotate the list to the right by `k` places.
9+
10+
**Example 1:**
11+
12+
![](https://assets.leetcode.com/uploads/2020/11/13/rotate1.jpg)
13+
14+
**Input:** head = [1,2,3,4,5], k = 2
15+
16+
**Output:** [4,5,1,2,3]
17+
18+
**Example 2:**
19+
20+
![](https://assets.leetcode.com/uploads/2020/11/13/roate2.jpg)
21+
22+
**Input:** head = [0,1,2], k = 4
23+
24+
**Output:** [2,0,1]
25+
26+
**Constraints:**
27+
28+
* The number of nodes in the list is in the range `[0, 500]`.
29+
* `-100 <= Node.val <= 100`
30+
* <code>0 <= k <= 2 * 10<sup>9</sup></code>
31+
32+
## Solution
33+
34+
```kotlin
35+
import com_github_leetcode.ListNode
36+
37+
/**
38+
* Example:
39+
* var li = ListNode(5)
40+
* var v = li.`val`
41+
* Definition for singly-linked list.
42+
* class ListNode(var `val`: Int) {
43+
* var next: ListNode? = null
44+
* }
45+
*/
46+
class Solution {
47+
fun rotateRight(head: ListNode?, k: Int): ListNode? {
48+
if (head == null || k == 0) {
49+
return head
50+
}
51+
var tail = head
52+
// find the count and let tail points to last node
53+
var count = 1
54+
while (tail != null && tail.next != null) {
55+
count++
56+
tail = tail.next
57+
}
58+
// calculate number of times to rotate by count modulas
59+
val times = k % count
60+
if (times == 0) {
61+
return head
62+
}
63+
var temp = head
64+
// iterate and go to the K+1 th node from the end or count - K - 1 node from
65+
// start
66+
var i = 1
67+
while (i <= count - times - 1 && temp != null) {
68+
temp = temp.next
69+
i++
70+
}
71+
var newHead: ListNode? = null
72+
if (temp != null && tail != null) {
73+
newHead = temp.next
74+
temp.next = null
75+
tail.next = head
76+
}
77+
return newHead
78+
}
79+
}
80+
```
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
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+
## 63\. Unique Paths II
5+
6+
Medium
7+
8+
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.
9+
10+
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.
11+
12+
Return _the number of possible unique paths that the robot can take to reach the bottom-right corner_.
13+
14+
The testcases are generated so that the answer will be less than or equal to <code>2 * 10<sup>9</sup></code>.
15+
16+
**Example 1:**
17+
18+
![](https://assets.leetcode.com/uploads/2020/11/04/robot1.jpg)
19+
20+
**Input:** obstacleGrid = \[\[0,0,0],[0,1,0],[0,0,0]]
21+
22+
**Output:** 2
23+
24+
**Explanation:** There is one obstacle in the middle of the 3x3 grid above.
25+
26+
There are two ways to reach the bottom-right corner:
27+
28+
1. Right -> Right -> Down -> Down
29+
30+
2. Down -> Down -> Right -> Right
31+
32+
**Example 2:**
33+
34+
![](https://assets.leetcode.com/uploads/2020/11/04/robot2.jpg)
35+
36+
**Input:** obstacleGrid = \[\[0,1],[0,0]]
37+
38+
**Output:** 1
39+
40+
**Constraints:**
41+
42+
* `m == obstacleGrid.length`
43+
* `n == obstacleGrid[i].length`
44+
* `1 <= m, n <= 100`
45+
* `obstacleGrid[i][j]` is `0` or `1`.
46+
47+
## Solution
48+
49+
```kotlin
50+
class Solution {
51+
fun uniquePathsWithObstacles(obstacleGrid: Array<IntArray>): Int {
52+
// if start point has obstacle, there's no path
53+
if (obstacleGrid[0][0] == 1) {
54+
return 0
55+
}
56+
obstacleGrid[0][0] = 1
57+
val m = obstacleGrid.size
58+
val n: Int = obstacleGrid[0].size
59+
for (i in 1 until m) {
60+
if (obstacleGrid[i][0] == 1) {
61+
obstacleGrid[i][0] = 0
62+
} else {
63+
obstacleGrid[i][0] = obstacleGrid[i - 1][0]
64+
}
65+
}
66+
for (j in 1 until n) {
67+
if (obstacleGrid[0][j] == 1) {
68+
obstacleGrid[0][j] = 0
69+
} else {
70+
obstacleGrid[0][j] = obstacleGrid[0][j - 1]
71+
}
72+
}
73+
for (i in 1 until m) {
74+
for (j in 1 until n) {
75+
if (obstacleGrid[i][j] == 1) {
76+
obstacleGrid[i][j] = 0
77+
} else {
78+
obstacleGrid[i][j] = obstacleGrid[i - 1][j] + obstacleGrid[i][j - 1]
79+
}
80+
}
81+
}
82+
return obstacleGrid[m - 1][n - 1]
83+
}
84+
}
85+
```

0 commit comments

Comments
 (0)