Skip to content

Commit ed6dea3

Browse files
authored
Added tasks 43-58.
1 parent 9f1de1d commit ed6dea3

File tree

11 files changed

+546
-12
lines changed

11 files changed

+546
-12
lines changed

README.md

Lines changed: 23 additions & 7 deletions
Large diffs are not rendered by default.

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ Given a linked list, swap every two adjacent nodes and return its head. You must
3737
```kotlin
3838
import com_github_leetcode.ListNode
3939

40+
/*
41+
* Example:
42+
* var li = ListNode(5)
43+
* var v = li.`val`
44+
* Definition for singly-linked list.
45+
* class ListNode(var `val`: Int) {
46+
* var next: ListNode? = null
47+
* }
48+
*/
4049
class Solution {
4150
fun swapPairs(head: ListNode?): ListNode? {
4251
if (head == null) {
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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+
## 43\. Multiply Strings
5+
6+
Medium
7+
8+
Given two non-negative integers `num1` and `num2` represented as strings, return the product of `num1` and `num2`, also represented as a string.
9+
10+
**Note:** You must not use any built-in BigInteger library or convert the inputs to integer directly.
11+
12+
**Example 1:**
13+
14+
**Input:** num1 = "2", num2 = "3"
15+
16+
**Output:** "6"
17+
18+
**Example 2:**
19+
20+
**Input:** num1 = "123", num2 = "456"
21+
22+
**Output:** "56088"
23+
24+
**Constraints:**
25+
26+
* `1 <= num1.length, num2.length <= 200`
27+
* `num1` and `num2` consist of digits only.
28+
* Both `num1` and `num2` do not contain any leading zero, except the number `0` itself.
29+
30+
## Solution
31+
32+
```kotlin
33+
class Solution {
34+
private fun getIntArray(s: String): IntArray {
35+
val chars = s.toCharArray()
36+
val arr = IntArray(chars.size)
37+
for (i in chars.indices) {
38+
arr[i] = chars[i] - '0'
39+
}
40+
return arr
41+
}
42+
43+
private fun convertToStr(res: IntArray, i: Int): String {
44+
var index = i
45+
val chars = StringBuffer()
46+
while (index < res.size) {
47+
chars.append(res[index].toString())
48+
index++
49+
}
50+
System.out.println(chars)
51+
return chars.toString()
52+
}
53+
54+
fun multiply(num1: String, num2: String): String {
55+
val arr1 = getIntArray(num1)
56+
val arr2 = getIntArray(num2)
57+
val res = IntArray(arr1.size + arr2.size)
58+
var index = arr1.size + arr2.size - 1
59+
for (i in arr2.indices.reversed()) {
60+
var k = index--
61+
for (j in arr1.indices.reversed()) {
62+
res[k] += arr2[i] * arr1[j]
63+
k--
64+
}
65+
}
66+
index = arr1.size + arr2.size - 1
67+
var carry = 0
68+
for (i in index downTo 0) {
69+
val temp = res[i] + carry
70+
res[i] = temp % 10
71+
carry = temp / 10
72+
}
73+
var i = 0
74+
while (i < res.size && res[i] == 0) {
75+
i++
76+
}
77+
return if (i > index) {
78+
"0"
79+
} else {
80+
convertToStr(res, i)
81+
}
82+
}
83+
}
84+
```
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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+
## 44\. Wildcard Matching
5+
6+
Hard
7+
8+
Given an input string (`s`) and a pattern (`p`), implement wildcard pattern matching with support for `'?'` and `'*'` where:
9+
10+
* `'?'` Matches any single character.
11+
* `'*'` Matches any sequence of characters (including the empty sequence).
12+
13+
The matching should cover the **entire** input string (not partial).
14+
15+
**Example 1:**
16+
17+
**Input:** s = "aa", p = "a"
18+
19+
**Output:** false
20+
21+
**Explanation:** "a" does not match the entire string "aa".
22+
23+
**Example 2:**
24+
25+
**Input:** s = "aa", p = "\*"
26+
27+
**Output:** true
28+
29+
**Explanation:** '\*' matches any sequence.
30+
31+
**Example 3:**
32+
33+
**Input:** s = "cb", p = "?a"
34+
35+
**Output:** false
36+
37+
**Explanation:** '?' matches 'c', but the second letter is 'a', which does not match 'b'.
38+
39+
**Constraints:**
40+
41+
* `0 <= s.length, p.length <= 2000`
42+
* `s` contains only lowercase English letters.
43+
* `p` contains only lowercase English letters, `'?'` or `'*'`.
44+
45+
## Solution
46+
47+
```kotlin
48+
class Solution {
49+
fun isMatch(inputString: String, pattern: String): Boolean {
50+
var i = 0
51+
var j = 0
52+
var starIdx = -1
53+
var lastMatch = -1
54+
while (i < inputString.length) {
55+
if (j < pattern.length &&
56+
(inputString[i] == pattern[j] || pattern[j] == '?')
57+
) {
58+
i++
59+
j++
60+
} else if (j < pattern.length && pattern[j] == '*') {
61+
starIdx = j
62+
lastMatch = i
63+
j++
64+
} else if (starIdx != -1) {
65+
// there is a no match and there was a previous star, we will reset the j to indx
66+
// after star_index
67+
// lastMatch will tell from which index we start comparing the string if we
68+
// encounter * in pattern
69+
j = starIdx + 1
70+
// we are saying we included more characters in * so we incremented the
71+
lastMatch++
72+
// index
73+
i = lastMatch
74+
} else {
75+
return false
76+
}
77+
}
78+
var isMatch = true
79+
while (j < pattern.length && pattern[j] == '*') {
80+
j++
81+
}
82+
if (i != inputString.length || j != pattern.length) {
83+
isMatch = false
84+
}
85+
return isMatch
86+
}
87+
}
88+
```
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
## 47\. Permutations II
5+
6+
Medium
7+
8+
Given a collection of numbers, `nums`, that might contain duplicates, return _all possible unique permutations **in any order**._
9+
10+
**Example 1:**
11+
12+
**Input:** nums = [1,1,2]
13+
14+
**Output:** [[1,1,2], [1,2,1], [2,1,1]]
15+
16+
**Example 2:**
17+
18+
**Input:** nums = [1,2,3]
19+
20+
**Output:** [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
21+
22+
**Constraints:**
23+
24+
* `1 <= nums.length <= 8`
25+
* `-10 <= nums[i] <= 10`
26+
27+
## Solution
28+
29+
```kotlin
30+
class Solution {
31+
private var ans: MutableList<List<Int>>? = null
32+
fun permuteUnique(nums: IntArray): List<List<Int>> {
33+
ans = ArrayList()
34+
permute(nums, 0)
35+
return ans as ArrayList<List<Int>>
36+
}
37+
38+
private fun permute(nums: IntArray, p: Int) {
39+
if (p >= nums.size - 1) {
40+
val t: MutableList<Int> = ArrayList(nums.size)
41+
for (n in nums) {
42+
t.add(n)
43+
}
44+
ans!!.add(t)
45+
return
46+
}
47+
permute(nums, p + 1)
48+
val used = BooleanArray(30)
49+
for (i in p + 1 until nums.size) {
50+
if (nums[i] != nums[p] && !used[10 + nums[i]]) {
51+
used[10 + nums[i]] = true
52+
swap(nums, p, i)
53+
permute(nums, p + 1)
54+
swap(nums, p, i)
55+
}
56+
}
57+
}
58+
59+
private fun swap(nums: IntArray, i: Int, j: Int) {
60+
val t = nums[i]
61+
nums[i] = nums[j]
62+
nums[j] = t
63+
}
64+
}
65+
```
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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+
## 50\. Pow(x, n)
5+
6+
Medium
7+
8+
Implement [pow(x, n)](http://www.cplusplus.com/reference/valarray/pow/), which calculates `x` raised to the power `n` (i.e., <code>x<sup>n</sup></code>).
9+
10+
**Example 1:**
11+
12+
**Input:** x = 2.00000, n = 10
13+
14+
**Output:** 1024.00000
15+
16+
**Example 2:**
17+
18+
**Input:** x = 2.10000, n = 3
19+
20+
**Output:** 9.26100
21+
22+
**Example 3:**
23+
24+
**Input:** x = 2.00000, n = -2
25+
26+
**Output:** 0.25000
27+
28+
**Explanation:** 2<sup>\-2</sup> = 1/2<sup>2</sup> = 1/4 = 0.25
29+
30+
**Constraints:**
31+
32+
* `-100.0 < x < 100.0`
33+
* <code>-2<sup>31</sup> <= n <= 2<sup>31</sup>-1</code>
34+
* `n` is an integer.
35+
* <code>-10<sup>4</sup> <= x<sup>n</sup> <= 10<sup>4</sup></code>
36+
37+
## Solution
38+
39+
```kotlin
40+
class Solution {
41+
fun myPow(x: Double, n: Int): Double {
42+
var x = x
43+
var nn = n.toLong()
44+
var res = 1.0
45+
if (n < 0) {
46+
nn *= -1
47+
}
48+
while (nn > 0) {
49+
if (nn % 2 == 1L) {
50+
nn--
51+
res *= x
52+
} else {
53+
x *= x
54+
nn /= 2
55+
}
56+
}
57+
return if (n < 0) {
58+
1.0 / res
59+
} else res
60+
}
61+
}
62+
```
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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+
## 52\. N-Queens II
5+
6+
Hard
7+
8+
The **n-queens** puzzle is the problem of placing `n` queens on an `n x n` chessboard such that no two queens attack each other.
9+
10+
Given an integer `n`, return _the number of distinct solutions to the **n-queens puzzle**_.
11+
12+
**Example 1:**
13+
14+
![](https://assets.leetcode.com/uploads/2020/11/13/queens.jpg)
15+
16+
**Input:** n = 4
17+
18+
**Output:** 2
19+
20+
**Explanation:** There are two distinct solutions to the 4-queens puzzle as shown.
21+
22+
**Example 2:**
23+
24+
**Input:** n = 1
25+
26+
**Output:** 1
27+
28+
**Constraints:**
29+
30+
* `1 <= n <= 9`
31+
32+
## Solution
33+
34+
```kotlin
35+
class Solution {
36+
fun totalNQueens(n: Int): Int {
37+
val row = BooleanArray(n)
38+
val col = BooleanArray(n)
39+
val diagonal = BooleanArray(n + n - 1)
40+
val antiDiagonal = BooleanArray(n + n - 1)
41+
return totalNQueens(n, 0, row, col, diagonal, antiDiagonal)
42+
}
43+
44+
private fun totalNQueens(
45+
n: Int,
46+
r: Int,
47+
row: BooleanArray,
48+
col: BooleanArray,
49+
diagonal: BooleanArray,
50+
antiDiagonal: BooleanArray
51+
): Int {
52+
if (r == n) {
53+
return 1
54+
}
55+
var count = 0
56+
for (c in 0 until n) {
57+
if (!row[r] && !col[c] && !diagonal[r + c] && !antiDiagonal[r - c + n - 1]) {
58+
antiDiagonal[r - c + n - 1] = true
59+
diagonal[r + c] = antiDiagonal[r - c + n - 1]
60+
col[c] = diagonal[r + c]
61+
row[r] = col[c]
62+
count += totalNQueens(n, r + 1, row, col, diagonal, antiDiagonal)
63+
antiDiagonal[r - c + n - 1] = false
64+
diagonal[r + c] = antiDiagonal[r - c + n - 1]
65+
col[c] = diagonal[r + c]
66+
row[r] = col[c]
67+
}
68+
}
69+
return count
70+
}
71+
}
72+
```

0 commit comments

Comments
 (0)