Skip to content

Commit 83ea32e

Browse files
feat: add kotlin solutions to lc problems (#3492)
1 parent 1680a82 commit 83ea32e

File tree

49 files changed

+1841
-8
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1841
-8
lines changed

solution/0000-0099/0001.Two Sum/README.md

+19
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,25 @@ proc twoSum(nums: seq[int], target: int): seq[int] =
318318
return @[]
319319
```
320320

321+
#### Kotlin
322+
323+
```kotlin
324+
class Solution {
325+
fun twoSum(nums: IntArray, target: Int): IntArray {
326+
val m = mutableMapOf<Int, Int>()
327+
nums.forEachIndexed { i, x ->
328+
val y = target - x
329+
val j = m.get(y)
330+
if (j != null) {
331+
return intArrayOf(j, i)
332+
}
333+
m[x] = i
334+
}
335+
return intArrayOf()
336+
}
337+
}
338+
```
339+
321340
<!-- tabs:end -->
322341

323342
<!-- solution:end -->

solution/0000-0099/0001.Two Sum/README_EN.md

+19
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,25 @@ proc twoSum(nums: seq[int], target: int): seq[int] =
315315
return @[]
316316
```
317317

318+
#### Kotlin
319+
320+
```kotlin
321+
class Solution {
322+
fun twoSum(nums: IntArray, target: Int): IntArray {
323+
val m = mutableMapOf<Int, Int>()
324+
nums.forEachIndexed { i, x ->
325+
val y = target - x
326+
val j = m.get(y)
327+
if (j != null) {
328+
return intArrayOf(j, i)
329+
}
330+
m[x] = i
331+
}
332+
return intArrayOf()
333+
}
334+
}
335+
```
336+
318337
<!-- tabs:end -->
319338

320339
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Solution {
2+
fun twoSum(nums: IntArray, target: Int): IntArray {
3+
val m = mutableMapOf<Int, Int>()
4+
nums.forEachIndexed { i, x ->
5+
val y = target - x
6+
val j = m.get(y)
7+
if (j != null) {
8+
return intArrayOf(j, i)
9+
}
10+
m[x] = i
11+
}
12+
return intArrayOf()
13+
}
14+
}

solution/0000-0099/0003.Longest Substring Without Repeating Characters/README.md

+22-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ tags:
2626

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

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

312+
#### Kotlin
313+
314+
```kotlin
315+
class Solution {
316+
fun lengthOfLongestSubstring(s: String): Int {
317+
var char_set = BooleanArray(128)
318+
var left = 0
319+
var ans = 0
320+
s.forEachIndexed { right, c ->
321+
while (char_set[c.code]) {
322+
char_set[s[left].code] = false
323+
left++
324+
}
325+
char_set[c.code] = true
326+
ans = Math.max(ans, right - left + 1)
327+
}
328+
return ans
329+
}
330+
}
331+
```
332+
312333
<!-- tabs:end -->
313334

314335
<!-- solution:end -->

solution/0000-0099/0003.Longest Substring Without Repeating Characters/README_EN.md

+21
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,27 @@ proc lengthOfLongestSubstring(s: string): int =
307307
result = res # result has the default return value
308308
```
309309

310+
#### Kotlin
311+
312+
```kotlin
313+
class Solution {
314+
fun lengthOfLongestSubstring(s: String): Int {
315+
var char_set = BooleanArray(128)
316+
var left = 0
317+
var ans = 0
318+
s.forEachIndexed { right, c ->
319+
while (char_set[c.code]) {
320+
char_set[s[left].code] = false
321+
left++
322+
}
323+
char_set[c.code] = true
324+
ans = Math.max(ans, right - left + 1)
325+
}
326+
return ans
327+
}
328+
}
329+
```
330+
310331
<!-- tabs:end -->
311332

312333
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
fun lengthOfLongestSubstring(s: String): Int {
3+
var char_set = BooleanArray(128)
4+
var left = 0
5+
var ans = 0
6+
s.forEachIndexed { right, c ->
7+
while (char_set[c.code]) {
8+
char_set[s[left].code] = false
9+
left++
10+
}
11+
char_set[c.code] = true
12+
ans = Math.max(ans, right - left + 1)
13+
}
14+
return ans
15+
}
16+
}

solution/0000-0099/0034.Find First and Last Position of Element in Sorted Array/README.md

+26
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,32 @@ class Solution {
289289
}
290290
```
291291

292+
#### Kotlin
293+
294+
```kotlin
295+
class Solution {
296+
fun searchRange(nums: IntArray, target: Int): IntArray {
297+
val left = this.search(nums, target)
298+
val right = this.search(nums, target + 1)
299+
return if (left == right) intArrayOf(-1, -1) else intArrayOf(left, right - 1)
300+
}
301+
302+
private fun search(nums: IntArray, target: Int): Int {
303+
var left = 0
304+
var right = nums.size
305+
while (left < right) {
306+
val middle = (left + right) / 2
307+
if (nums[middle] < target) {
308+
left = middle + 1
309+
} else {
310+
right = middle
311+
}
312+
}
313+
return left
314+
}
315+
}
316+
```
317+
292318
<!-- tabs:end -->
293319

294320
<!-- solution:end -->

solution/0000-0099/0034.Find First and Last Position of Element in Sorted Array/README_EN.md

+26
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,32 @@ class Solution {
279279
}
280280
```
281281

282+
#### Kotlin
283+
284+
```kotlin
285+
class Solution {
286+
fun searchRange(nums: IntArray, target: Int): IntArray {
287+
val left = this.search(nums, target)
288+
val right = this.search(nums, target + 1)
289+
return if (left == right) intArrayOf(-1, -1) else intArrayOf(left, right - 1)
290+
}
291+
292+
private fun search(nums: IntArray, target: Int): Int {
293+
var left = 0
294+
var right = nums.size
295+
while (left < right) {
296+
val middle = (left + right) / 2
297+
if (nums[middle] < target) {
298+
left = middle + 1
299+
} else {
300+
right = middle
301+
}
302+
}
303+
return left
304+
}
305+
}
306+
```
307+
282308
<!-- tabs:end -->
283309

284310
<!-- solution:end -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
class Solution {
2+
fun searchRange(nums: IntArray, target: Int): IntArray {
3+
val left = this.search(nums, target)
4+
val right = this.search(nums, target + 1)
5+
return if (left == right) intArrayOf(-1, -1) else intArrayOf(left, right - 1)
6+
}
7+
8+
private fun search(nums: IntArray, target: Int): Int {
9+
var left = 0
10+
var right = nums.size
11+
while (left < right) {
12+
val middle = (left + right) / 2
13+
if (nums[middle] < target) {
14+
left = middle + 1
15+
} else {
16+
right = middle
17+
}
18+
}
19+
return left
20+
}
21+
}

solution/0000-0099/0043.Multiply Strings/README.md

+82
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,88 @@ class Solution {
325325
}
326326
```
327327

328+
#### Kotlin
329+
330+
```kotlin
331+
class Solution {
332+
fun multiply(num1: String, num2: String): String {
333+
if (num1 == "0" || num2 == "0") return "0"
334+
335+
val chars_1 = num1.toCharArray().reversedArray()
336+
val chars_2 = num2.toCharArray().reversedArray()
337+
338+
val result = mutableListOf<Int>()
339+
340+
chars_1.forEachIndexed { i, c1 ->
341+
val multiplier_1 = c1 - '0'
342+
var over = 0
343+
var index = 0
344+
345+
fun sum(product: Int = 0): Unit {
346+
while (index >= result.size) {
347+
result.add(0)
348+
}
349+
val value = product + over + result[index]
350+
result[index] = value % 10
351+
over = value / 10
352+
return
353+
}
354+
355+
chars_2.forEachIndexed { j, c2 ->
356+
index = i + j
357+
val multiplier_2 = c2 - '0'
358+
sum(multiplier_1 * multiplier_2)
359+
}
360+
361+
while (over > 0) {
362+
index++
363+
sum()
364+
}
365+
}
366+
367+
return result.reversed().joinToString("")
368+
}
369+
}
370+
```
371+
372+
#### JavaScript
373+
374+
```js
375+
/**
376+
* @param {string} num1
377+
* @param {string} num2
378+
* @return {string}
379+
*/
380+
var multiply = function (num1, num2) {
381+
if (num1 === '0' || num2 === '0') return '0';
382+
383+
const result = Array(num1.length + num2.length).fill(0);
384+
const code_0 = '0'.charCodeAt(0);
385+
386+
const num1_len = num1.length;
387+
const num2_len = num2.length;
388+
389+
for (let i = 0; i < num1_len; ++i) {
390+
const multiplier_1 = num1.charCodeAt(num1_len - i - 1) - code_0;
391+
for (let j = 0; j < num2_len; ++j) {
392+
const multiplier_2 = num2.charCodeAt(num2_len - j - 1) - code_0;
393+
result[i + j] += multiplier_1 * multiplier_2;
394+
}
395+
}
396+
397+
result.reduce((carry, value, index) => {
398+
const sum = carry + value;
399+
result[index] = sum % 10;
400+
return (sum / 10) | 0;
401+
}, 0);
402+
403+
return result
404+
.slice(0, result.findLastIndex(d => d !== 0) + 1)
405+
.reverse()
406+
.join('');
407+
};
408+
```
409+
328410
<!-- tabs:end -->
329411

330412
<!-- solution:end -->

0 commit comments

Comments
 (0)