Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat: add kotlin solutions to lc problems #3492

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
yanglbme merged 15 commits into doocs:main from Zuoqiu-Yingyi:feat/solutions
Sep 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
c5caac7
feat: add kotlin solution to lc problem: No.0001
Zuoqiu-Yingyi Jul 30, 2024
0b032a2
feat: add kotlin solution to lc problem: No.0034
Zuoqiu-Yingyi Jul 31, 2024
9ec2933
feat: add kotlin solution to lc problem: No.1870
Zuoqiu-Yingyi Jul 31, 2024
394c28d
feat: add solutions to lc problem: No.1898
Zuoqiu-Yingyi Aug 1, 2024
1638bd6
feat: add solutions to lc problem: No.0912
Zuoqiu-Yingyi Aug 1, 2024
384d59a
feat: add kotlin solution to lc problem: No.0415
Zuoqiu-Yingyi Aug 26, 2024
22aa558
feat: add kotlin solution to lc problem: No.0043
Zuoqiu-Yingyi Aug 25, 2024
abc99d4
feat: add kotlin solution to lc problem: No.0303
Zuoqiu-Yingyi Aug 30, 2024
5074e18
feat: add kotlin solution to lc problem: No.0304
Zuoqiu-Yingyi Aug 30, 2024
b97c6d3
feat: add kotlin solution to lc problem: No.2132
Zuoqiu-Yingyi Sep 4, 2024
a5c969d
feat: add kotlin solution to lc problem: No.0003
Zuoqiu-Yingyi Sep 5, 2024
4192b92
feat: add kotlin solution to lc problem: No.0713
Zuoqiu-Yingyi Sep 5, 2024
6651f77
feat: add solutions to lc problem: No.0191
Zuoqiu-Yingyi Sep 5, 2024
143d9a3
feat: add solutions to lc problem: No.0056
Zuoqiu-Yingyi Sep 6, 2024
68e192c
Merge branch 'main' into feat/solutions
Zuoqiu-Yingyi Sep 6, 2024
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
19 changes: 19 additions & 0 deletions solution/0000-0099/0001.Two Sum/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,25 @@ proc twoSum(nums: seq[int], target: int): seq[int] =
return @[]
```

#### Kotlin

```kotlin
class Solution {
fun twoSum(nums: IntArray, target: Int): IntArray {
val m = mutableMapOf<Int, Int>()
nums.forEachIndexed { i, x ->
val y = target - x
val j = m.get(y)
if (j != null) {
return intArrayOf(j, i)
}
m[x] = i
}
return intArrayOf()
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
19 changes: 19 additions & 0 deletions solution/0000-0099/0001.Two Sum/README_EN.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,25 @@ proc twoSum(nums: seq[int], target: int): seq[int] =
return @[]
```

#### Kotlin

```kotlin
class Solution {
fun twoSum(nums: IntArray, target: Int): IntArray {
val m = mutableMapOf<Int, Int>()
nums.forEachIndexed { i, x ->
val y = target - x
val j = m.get(y)
if (j != null) {
return intArrayOf(j, i)
}
m[x] = i
}
return intArrayOf()
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
14 changes: 14 additions & 0 deletions solution/0000-0099/0001.Two Sum/Solution.kt
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Solution {
fun twoSum(nums: IntArray, target: Int): IntArray {
val m = mutableMapOf<Int, Int>()
nums.forEachIndexed { i, x ->
val y = target - x
val j = m.get(y)
if (j != null) {
return intArrayOf(j, i)
}
m[x] = i
}
return intArrayOf()
}
}
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ tags:

<pre>
<strong>输入: </strong>s = "abcabcbb"
<strong>输出: </strong>3
<strong>输出: </strong>3
<strong>解释:</strong> 因为无重复字符的最长子串是 <code>"abc"</code>,所以其长度为 3。
</pre>

Expand Down Expand Up @@ -309,6 +309,27 @@ proc lengthOfLongestSubstring(s: string): int =
result = res # result has the default return value
```

#### Kotlin

```kotlin
class Solution {
fun lengthOfLongestSubstring(s: String): Int {
var char_set = BooleanArray(128)
var left = 0
var ans = 0
s.forEachIndexed { right, c ->
while (char_set[c.code]) {
char_set[s[left].code] = false
left++
}
char_set[c.code] = true
ans = Math.max(ans, right - left + 1)
}
return ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,27 @@ proc lengthOfLongestSubstring(s: string): int =
result = res # result has the default return value
```

#### Kotlin

```kotlin
class Solution {
fun lengthOfLongestSubstring(s: String): Int {
var char_set = BooleanArray(128)
var left = 0
var ans = 0
s.forEachIndexed { right, c ->
while (char_set[c.code]) {
char_set[s[left].code] = false
left++
}
char_set[c.code] = true
ans = Math.max(ans, right - left + 1)
}
return ans
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution {
fun lengthOfLongestSubstring(s: String): Int {
var char_set = BooleanArray(128)
var left = 0
var ans = 0
s.forEachIndexed { right, c ->
while (char_set[c.code]) {
char_set[s[left].code] = false
left++
}
char_set[c.code] = true
ans = Math.max(ans, right - left + 1)
}
return ans
}
}
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,32 @@ class Solution {
}
```

#### Kotlin

```kotlin
class Solution {
fun searchRange(nums: IntArray, target: Int): IntArray {
val left = this.search(nums, target)
val right = this.search(nums, target + 1)
return if (left == right) intArrayOf(-1, -1) else intArrayOf(left, right - 1)
}

private fun search(nums: IntArray, target: Int): Int {
var left = 0
var right = nums.size
while (left < right) {
val middle = (left + right) / 2
if (nums[middle] < target) {
left = middle + 1
} else {
right = middle
}
}
return left
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,32 @@ class Solution {
}
```

#### Kotlin

```kotlin
class Solution {
fun searchRange(nums: IntArray, target: Int): IntArray {
val left = this.search(nums, target)
val right = this.search(nums, target + 1)
return if (left == right) intArrayOf(-1, -1) else intArrayOf(left, right - 1)
}

private fun search(nums: IntArray, target: Int): Int {
var left = 0
var right = nums.size
while (left < right) {
val middle = (left + right) / 2
if (nums[middle] < target) {
left = middle + 1
} else {
right = middle
}
}
return left
}
}
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution {
fun searchRange(nums: IntArray, target: Int): IntArray {
val left = this.search(nums, target)
val right = this.search(nums, target + 1)
return if (left == right) intArrayOf(-1, -1) else intArrayOf(left, right - 1)
}

private fun search(nums: IntArray, target: Int): Int {
var left = 0
var right = nums.size
while (left < right) {
val middle = (left + right) / 2
if (nums[middle] < target) {
left = middle + 1
} else {
right = middle
}
}
return left
}
}
82 changes: 82 additions & 0 deletions solution/0000-0099/0043.Multiply Strings/README.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,88 @@ class Solution {
}
```

#### Kotlin

```kotlin
class Solution {
fun multiply(num1: String, num2: String): String {
if (num1 == "0" || num2 == "0") return "0"

val chars_1 = num1.toCharArray().reversedArray()
val chars_2 = num2.toCharArray().reversedArray()

val result = mutableListOf<Int>()

chars_1.forEachIndexed { i, c1 ->
val multiplier_1 = c1 - '0'
var over = 0
var index = 0

fun sum(product: Int = 0): Unit {
while (index >= result.size) {
result.add(0)
}
val value = product + over + result[index]
result[index] = value % 10
over = value / 10
return
}

chars_2.forEachIndexed { j, c2 ->
index = i + j
val multiplier_2 = c2 - '0'
sum(multiplier_1 * multiplier_2)
}

while (over > 0) {
index++
sum()
}
}

return result.reversed().joinToString("")
}
}
```

#### JavaScript

```js
/**
* @param {string} num1
* @param {string} num2
* @return {string}
*/
var multiply = function (num1, num2) {
if (num1 === '0' || num2 === '0') return '0';

const result = Array(num1.length + num2.length).fill(0);
const code_0 = '0'.charCodeAt(0);

const num1_len = num1.length;
const num2_len = num2.length;

for (let i = 0; i < num1_len; ++i) {
const multiplier_1 = num1.charCodeAt(num1_len - i - 1) - code_0;
for (let j = 0; j < num2_len; ++j) {
const multiplier_2 = num2.charCodeAt(num2_len - j - 1) - code_0;
result[i + j] += multiplier_1 * multiplier_2;
}
}

result.reduce((carry, value, index) => {
const sum = carry + value;
result[index] = sum % 10;
return (sum / 10) | 0;
}, 0);

return result
.slice(0, result.findLastIndex(d => d !== 0) + 1)
.reverse()
.join('');
};
```

<!-- tabs:end -->

<!-- solution:end -->
Expand Down
Loading
Loading

AltStyle によって変換されたページ (->オリジナル) /