diff --git a/src/main/kotlin/g0001_0100/s0014_longest_common_prefix/Solution.kt b/src/main/kotlin/g0001_0100/s0014_longest_common_prefix/Solution.kt new file mode 100644 index 000000000..d8aa410fe --- /dev/null +++ b/src/main/kotlin/g0001_0100/s0014_longest_common_prefix/Solution.kt @@ -0,0 +1,29 @@ +package g0001_0100.s0014_longest_common_prefix + +// #Easy #Top_Interview_Questions #String + +internal class Solution { + fun longestCommonPrefix(strs: Array): String { + if (strs.size < 1) { + return "" + } + if (strs.size == 1) { + return strs[0] + } + var temp = strs[0] + var i = 1 + var cur: String + while (temp.length > 0 && i < strs.size) { + if (temp.length > strs[i].length) { + temp = temp.substring(0, strs[i].length) + } + cur = strs[i].substring(0, temp.length) + if (cur != temp) { + temp = temp.substring(0, temp.length - 1) + } else { + i++ + } + } + return temp + } +} diff --git a/src/main/kotlin/g0001_0100/s0014_longest_common_prefix/readme.md b/src/main/kotlin/g0001_0100/s0014_longest_common_prefix/readme.md new file mode 100644 index 000000000..577a365d3 --- /dev/null +++ b/src/main/kotlin/g0001_0100/s0014_longest_common_prefix/readme.md @@ -0,0 +1,27 @@ +14\. Longest Common Prefix + +Easy + +Write a function to find the longest common prefix string amongst an array of strings. + +If there is no common prefix, return an empty string `""`. + +**Example 1:** + +**Input:** strs = ["flower","flow","flight"] + +**Output:** "fl" + +**Example 2:** + +**Input:** strs = ["dog","racecar","car"] + +**Output:** "" + +**Explanation:** There is no common prefix among the input strings. + +**Constraints:** + +* `1 <= strs.length <= 200` +* `0 <= strs[i].length <= 200` +* `strs[i]` consists of only lower-case English letters. \ No newline at end of file diff --git a/src/main/kotlin/g0001_0100/s0015_3sum/Solution.kt b/src/main/kotlin/g0001_0100/s0015_3sum/Solution.kt new file mode 100644 index 000000000..f8ec780f4 --- /dev/null +++ b/src/main/kotlin/g0001_0100/s0015_3sum/Solution.kt @@ -0,0 +1,46 @@ +package g0001_0100.s0015_3sum + +import java.util.Arrays +import kotlin.collections.ArrayList + +class Solution { + fun threeSum(nums: IntArray): List> { + Arrays.sort(nums) + val len = nums.size + val result: MutableList> = ArrayList() + var l: Int + var r: Int + var i = 0 + while (i < len - 2) { + l = i + 1 + r = len - 1 + while (r > l) { + val sum = nums[i] + nums[l] + nums[r] + if (sum < 0) { + l++ + } else if (sum > 0) { + r-- + } else { + val list: MutableList = ArrayList() + list.add(nums[i]) + list.add(nums[l]) + list.add(nums[r]) + result.add(list) + while (l < r && nums[l + 1] == nums[l]) { + l++ + } + while (r > l && nums[r - 1] == nums[r]) { + r-- + } + l++ + r-- + } + } + while (i < len - 1 && nums[i + 1] == nums[i]) { + i++ + } + i++ + } + return result + } +} diff --git a/src/main/kotlin/g0001_0100/s0015_3sum/readme.md b/src/main/kotlin/g0001_0100/s0015_3sum/readme.md new file mode 100644 index 000000000..67e5ddfd7 --- /dev/null +++ b/src/main/kotlin/g0001_0100/s0015_3sum/readme.md @@ -0,0 +1,30 @@ +15\. 3Sum + +Medium + +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`. + +Notice that the solution set must not contain duplicate triplets. + +**Example 1:** + +**Input:** nums = [-1,0,1,2,-1,-4] + +**Output:** [[-1,-1,2],[-1,0,1]] + +**Example 2:** + +**Input:** nums = [] + +**Output:** [] + +**Example 3:** + +**Input:** nums = [0] + +**Output:** [] + +**Constraints:** + +* `0 <= nums.length <= 3000` +* -105 <= nums[i] <= 105 \ No newline at end of file diff --git a/src/test/kotlin/com_github_leetcode/ArrayUtils.kt b/src/test/kotlin/com_github_leetcode/ArrayUtils.kt new file mode 100644 index 000000000..5f4abff63 --- /dev/null +++ b/src/test/kotlin/com_github_leetcode/ArrayUtils.kt @@ -0,0 +1,17 @@ +package com_github_leetcode + +import kotlin.collections.ArrayList + +object ArrayUtils { + fun getLists(expected: Array): List> { + val expectedList: MutableList> = ArrayList() + for (value in expected) { + val expectedItem: MutableList = ArrayList() + expectedList.add(expectedItem) + for (item in value) { + expectedItem.add(item) + } + } + return expectedList + } +} diff --git a/src/test/kotlin/g0001_0100/s0014_longest_common_prefix/SolutionTest.kt b/src/test/kotlin/g0001_0100/s0014_longest_common_prefix/SolutionTest.kt new file mode 100644 index 000000000..21403e0f0 --- /dev/null +++ b/src/test/kotlin/g0001_0100/s0014_longest_common_prefix/SolutionTest.kt @@ -0,0 +1,15 @@ +package g0001_0100.s0014_longest_common_prefix + +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun longestCommonPrefix() { + assertThat( + Solution().longestCommonPrefix(arrayOf("flower", "flow", "flight")), + equalTo("fl") + ) + } +} diff --git a/src/test/kotlin/g0001_0100/s0015_3sum/SolutionTest.kt b/src/test/kotlin/g0001_0100/s0015_3sum/SolutionTest.kt new file mode 100644 index 000000000..860966b37 --- /dev/null +++ b/src/test/kotlin/g0001_0100/s0015_3sum/SolutionTest.kt @@ -0,0 +1,16 @@ +package g0001_0100.s0015_3sum + +import com_github_leetcode.ArrayUtils +import org.hamcrest.CoreMatchers.equalTo +import org.hamcrest.MatcherAssert.assertThat +import org.junit.jupiter.api.Test + +internal class SolutionTest { + @Test + fun threeSum() { + assertThat( + Solution().threeSum(intArrayOf(-1, 0, 1, 2, -1, -4)), + equalTo(ArrayUtils.getLists(arrayOf(intArrayOf(-1, -1, 2), intArrayOf(-1, 0, 1)))) + ) + } +}