Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
Next Next commit
Added solutions.
  • Loading branch information
javadev committed Sep 16, 2022
commit 86b97e2dd7e81046c1bdd5272d442b7ddd55ad21
1,520 changes: 1,518 additions & 2 deletions README.md

Large diffs are not rendered by default.

59 changes: 59 additions & 0 deletions src/main/kotlin/g0001_0100/s0001_two_sum/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
[![](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)

## 1\. Two Sum

Easy

Given an array of integers `nums` and an integer `target`, return _indices of the two numbers such that they add up to `target`_.

You may assume that each input would have **_exactly_ one solution**, and you may not use the _same_ element twice.

You can return the answer in any order.

**Example 1:**

**Input:** nums = [2,7,11,15], target = 9

**Output:** [0,1]

**Output:** Because nums[0] + nums[1] == 9, we return [0, 1].

**Example 2:**

**Input:** nums = [3,2,4], target = 6

**Output:** [1,2]

**Example 3:**

**Input:** nums = [3,3], target = 6

**Output:** [0,1]

**Constraints:**

* <code>2 <= nums.length <= 10<sup>4</sup></code>
* <code>-10<sup>9</sup> <= nums[i] <= 10<sup>9</sup></code>
* <code>-10<sup>9</sup> <= target <= 10<sup>9</sup></code>
* **Only one valid answer exists.**

**Follow-up:** Can you come up with an algorithm that is less than <code>O(n<sup>2</sup>) </code>time complexity?

## Solution

```kotlin
class Solution {
fun twoSum(numbers: IntArray, target: Int): IntArray {
val indexMap: MutableMap<Int, Int> = HashMap()
for (i in numbers.indices) {
val requiredNum = target - numbers[i]
if (indexMap.containsKey(requiredNum)) {
return intArrayOf(indexMap[requiredNum]!!, i)
}
indexMap[numbers[i]] = i
}
return intArrayOf(-1, -1)
}
}
```
80 changes: 80 additions & 0 deletions src/main/kotlin/g0001_0100/s0002_add_two_numbers/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
[![](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)

## 2\. Add Two Numbers

Medium

You are given two **non-empty** linked lists representing two non-negative integers. The digits are stored in **reverse order**, and each of their nodes contains a single digit. Add the two numbers and return the sum as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

**Example 1:**

![](https://assets.leetcode.com/uploads/2020/10/02/addtwonumber1.jpg)

**Input:** l1 = [2,4,3], l2 = [5,6,4]

**Output:** [7,0,8]

**Explanation:** 342 + 465 = 807.

**Example 2:**

**Input:** l1 = [0], l2 = [0]

**Output:** [0]

**Example 3:**

**Input:** l1 = [9,9,9,9,9,9,9], l2 = [9,9,9,9]

**Output:** [8,9,9,9,0,0,0,1]

**Constraints:**

* The number of nodes in each linked list is in the range `[1, 100]`.
* `0 <= Node.val <= 9`
* It is guaranteed that the list represents a number that does not have leading zeros.

## Solution

```kotlin
import com_github_leetcode.ListNode

/*
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode(int x) { val = x; }
* }
*/
class Solution {
fun addTwoNumbers(l1: ListNode?, l2: ListNode?): ListNode? {
val dummyHead = ListNode(0)
var p = l1
var q = l2
var curr: ListNode? = dummyHead
var carry = 0
while (p != null || q != null) {
val x = p?.`val` ?: 0
val y = q?.`val` ?: 0
val sum = carry + x + y
carry = sum / 10
curr!!.next = ListNode(sum % 10)
curr = curr.next
if (p != null) {
p = p.next
}
if (q != null) {
q = q.next
}
}
if (carry > 0) {
curr!!.next = ListNode(carry)
}
return dummyHead.next
}
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
[![](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\. Longest Substring Without Repeating Characters

Medium

Given a string `s`, find the length of the **longest substring** without repeating characters.

**Example 1:**

**Input:** s = "abcabcbb"

**Output:** 3

**Explanation:** The answer is "abc", with the length of 3.

**Example 2:**

**Input:** s = "bbbbb"

**Output:** 1

**Explanation:** The answer is "b", with the length of 1.

**Example 3:**

**Input:** s = "pwwkew"

**Output:** 3

**Explanation:** The answer is "wke", with the length of 3. Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.

**Example 4:**

**Input:** s = ""

**Output:** 0

**Constraints:**

* <code>0 <= s.length <= 5 * 10<sup>4</sup></code>
* `s` consists of English letters, digits, symbols and spaces.

## Solution

```kotlin
class Solution {
fun lengthOfLongestSubstring(s: String): Int {
var i = 0
var j = 0
var longest = 0
// 1. if string empty, return 0
if (s.isEmpty()) {
return 0
}
while (j < s.length) {
// 2. if the char at index j already seen, update the longest if needs
if (i != j && s.substring(i, j).indexOf(s[j]) > -1) {
longest = Math.max(j - i, longest)
i++
} else {
// 3. j out of bound already, update longest
if (++j == s.length) {
longest = Math.max(s.length - i, longest)
break
}
}
}
return longest
}
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
[![](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)

## 4\. Median of Two Sorted Arrays

Hard

Given two sorted arrays `nums1` and `nums2` of size `m` and `n` respectively, return **the median** of the two sorted arrays.

The overall run time complexity should be `O(log (m+n))`.

**Example 1:**

**Input:** nums1 = [1,3], nums2 = [2]

**Output:** 2.00000

**Explanation:** merged array = [1,2,3] and median is 2.

**Example 2:**

**Input:** nums1 = [1,2], nums2 = [3,4]

**Output:** 2.50000

**Explanation:** merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5.

**Example 3:**

**Input:** nums1 = [0,0], nums2 = [0,0]

**Output:** 0.00000

**Example 4:**

**Input:** nums1 = [], nums2 = [1]

**Output:** 1.00000

**Example 5:**

**Input:** nums1 = [2], nums2 = []

**Output:** 2.00000

**Constraints:**

* `nums1.length == m`
* `nums2.length == n`
* `0 <= m <= 1000`
* `0 <= n <= 1000`
* `1 <= m + n <= 2000`
* <code>-10<sup>6</sup> <= nums1[i], nums2[i] <= 10<sup>6</sup></code>

## Solution

```kotlin
import kotlin.collections.ArrayList

class Solution {
fun findMedianSortedArrays(nums1: IntArray, nums2: IntArray): Double {
val l: MutableList<Int> = ArrayList()
val f: Double
for (j in nums1) {
l.add(j)
}
for (i in nums2) {
l.add(i)
}
l.sort()
val k = l.size
f = if (k % 2 == 0) {
(l[k / 2 - 1] + l[k / 2]).toDouble() / 2
} else {
l[(k + 1) / 2 - 1].toDouble()
}
return f
}
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
[![](https://img.shields.io/github/stars/javadev/LeetCode-in-Kotlin?label=Stars&style=flat-square)](https://github.com/javadev/LeetCode-in-Kotlin)
[![](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)

## 5\. Longest Palindromic Substring

Medium

Given a string `s`, return _the longest palindromic substring_ in `s`.

**Example 1:**

**Input:** s = "babad"

**Output:** "bab" **Note:** "aba" is also a valid answer.

**Example 2:**

**Input:** s = "cbbd"

**Output:** "bb"

**Example 3:**

**Input:** s = "a"

**Output:** "a"

**Example 4:**

**Input:** s = "ac"

**Output:** "a"

**Constraints:**

* `1 <= s.length <= 1000`
* `s` consist of only digits and English letters.

## Solution

```kotlin
class Solution {
fun longestPalindrome(s: String): String {
val newStr = CharArray(s.length * 2 + 1)
newStr[0] = '#'
for (i in s.indices) {
newStr[2 * i + 1] = s[i]
newStr[2 * i + 2] = '#'
}
val dp = IntArray(newStr.size)
var friendCenter = 0
var friendRadius = 0
var lpsCenter = 0
var lpsRadius = 0
for (i in newStr.indices) {
dp[i] = if (friendCenter + friendRadius > i) Math.min(
dp[friendCenter * 2 - i],
friendCenter + friendRadius - i
) else 1
while (i + dp[i] < newStr.size && i - dp[i] >= 0 && newStr[i + dp[i]] == newStr[i - dp[i]]) {
dp[i]++
}
if (friendCenter + friendRadius < i + dp[i]) {
friendCenter = i
friendRadius = dp[i]
}
if (lpsRadius < dp[i]) {
lpsCenter = i
lpsRadius = dp[i]
}
}
return s.substring((lpsCenter - lpsRadius + 1) / 2, (lpsCenter + lpsRadius - 1) / 2)
}
}
```
Loading