Skip to content

Commit bd0d9eb

Browse files
committed
Added tasks 14, 15.
1 parent bebd258 commit bd0d9eb

File tree

7 files changed

+180
-0
lines changed

7 files changed

+180
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g0001_0100.s0014_longest_common_prefix
2+
3+
// #Easy #Top_Interview_Questions #String
4+
5+
internal class Solution {
6+
fun longestCommonPrefix(strs: Array<String>): String {
7+
if (strs.size < 1) {
8+
return ""
9+
}
10+
if (strs.size == 1) {
11+
return strs[0]
12+
}
13+
var temp = strs[0]
14+
var i = 1
15+
var cur: String
16+
while (temp.length > 0 && i < strs.size) {
17+
if (temp.length > strs[i].length) {
18+
temp = temp.substring(0, strs[i].length)
19+
}
20+
cur = strs[i].substring(0, temp.length)
21+
if (cur != temp) {
22+
temp = temp.substring(0, temp.length - 1)
23+
} else {
24+
i++
25+
}
26+
}
27+
return temp
28+
}
29+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
14\. Longest Common Prefix
2+
3+
Easy
4+
5+
Write a function to find the longest common prefix string amongst an array of strings.
6+
7+
If there is no common prefix, return an empty string `""`.
8+
9+
**Example 1:**
10+
11+
**Input:** strs = ["flower","flow","flight"]
12+
13+
**Output:** "fl"
14+
15+
**Example 2:**
16+
17+
**Input:** strs = ["dog","racecar","car"]
18+
19+
**Output:** ""
20+
21+
**Explanation:** There is no common prefix among the input strings.
22+
23+
**Constraints:**
24+
25+
* `1 <= strs.length <= 200`
26+
* `0 <= strs[i].length <= 200`
27+
* `strs[i]` consists of only lower-case English letters.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package g0001_0100.s0015_3sum
2+
3+
import java.util.Arrays
4+
import kotlin.collections.ArrayList
5+
6+
class Solution {
7+
fun threeSum(nums: IntArray): List<List<Int>> {
8+
Arrays.sort(nums)
9+
val len = nums.size
10+
val result: MutableList<List<Int>> = ArrayList()
11+
var l: Int
12+
var r: Int
13+
var i = 0
14+
while (i < len - 2) {
15+
l = i + 1
16+
r = len - 1
17+
while (r > l) {
18+
val sum = nums[i] + nums[l] + nums[r]
19+
if (sum < 0) {
20+
l++
21+
} else if (sum > 0) {
22+
r--
23+
} else {
24+
val list: MutableList<Int> = ArrayList()
25+
list.add(nums[i])
26+
list.add(nums[l])
27+
list.add(nums[r])
28+
result.add(list)
29+
while (l < r && nums[l + 1] == nums[l]) {
30+
l++
31+
}
32+
while (r > l && nums[r - 1] == nums[r]) {
33+
r--
34+
}
35+
l++
36+
r--
37+
}
38+
}
39+
while (i < len - 1 && nums[i + 1] == nums[i]) {
40+
i++
41+
}
42+
i++
43+
}
44+
return result
45+
}
46+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
15\. 3Sum
2+
3+
Medium
4+
5+
Given an integer array nums, return all the triplets `[nums[i], nums[j], nums[k]]` such that `i != j`, `i != k`, and `j != k`, and `nums[i] + nums[j] + nums[k] == 0`.
6+
7+
Notice that the solution set must not contain duplicate triplets.
8+
9+
**Example 1:**
10+
11+
**Input:** nums = [-1,0,1,2,-1,-4]
12+
13+
**Output:** [[-1,-1,2],[-1,0,1]]
14+
15+
**Example 2:**
16+
17+
**Input:** nums = []
18+
19+
**Output:** []
20+
21+
**Example 3:**
22+
23+
**Input:** nums = [0]
24+
25+
**Output:** []
26+
27+
**Constraints:**
28+
29+
* `0 <= nums.length <= 3000`
30+
* <code>-10<sup>5</sup> <= nums[i] <= 10<sup>5</sup></code>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com_github_leetcode
2+
3+
import kotlin.collections.ArrayList
4+
5+
object ArrayUtils {
6+
fun getLists(expected: Array<IntArray>): List<List<Int>> {
7+
val expectedList: MutableList<List<Int>> = ArrayList()
8+
for (value in expected) {
9+
val expectedItem: MutableList<Int> = ArrayList()
10+
expectedList.add(expectedItem)
11+
for (item in value) {
12+
expectedItem.add(item)
13+
}
14+
}
15+
return expectedList
16+
}
17+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package g0001_0100.s0014_longest_common_prefix
2+
3+
import org.hamcrest.CoreMatchers.equalTo
4+
import org.hamcrest.MatcherAssert.assertThat
5+
import org.junit.jupiter.api.Test
6+
7+
internal class SolutionTest {
8+
@Test
9+
fun longestCommonPrefix() {
10+
assertThat(
11+
Solution().longestCommonPrefix(arrayOf("flower", "flow", "flight")),
12+
equalTo("fl")
13+
)
14+
}
15+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package g0001_0100.s0015_3sum
2+
3+
import com_github_leetcode.ArrayUtils
4+
import org.hamcrest.CoreMatchers.equalTo
5+
import org.hamcrest.MatcherAssert.assertThat
6+
import org.junit.jupiter.api.Test
7+
8+
internal class SolutionTest {
9+
@Test
10+
fun threeSum() {
11+
assertThat(
12+
Solution().threeSum(intArrayOf(-1, 0, 1, 2, -1, -4)),
13+
equalTo(ArrayUtils.getLists(arrayOf(intArrayOf(-1, -1, 2), intArrayOf(-1, 0, 1))))
14+
)
15+
}
16+
}

0 commit comments

Comments
 (0)