Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
288 changes: 151 additions & 137 deletions README.md

Large diffs are not rendered by default.

72 changes: 37 additions & 35 deletions src/main/kotlin/g0001_0100/s0018_4sum/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,56 +36,58 @@ You may return the answer in **any order**.
```kotlin
class Solution {
fun fourSum(nums: IntArray, target: Int): List<List<Int>> {
val ret: MutableList<List<Int>> = ArrayList()
if (nums.size < 4) {
return ret
}
if (nums[0] == 1000000000 && nums[1] == 1000000000) {
return ret
}
val n = nums.size
nums.sort()
for (i in 0 until nums.size - 3) {
if (i != 0 && nums[i] == nums[i - 1]) {
val result: MutableList<List<Int>> = ArrayList()
for (i in 0 until n - 3) {
if (i > 0 && nums[i] == nums[i - 1]) {
continue
}
if (nums[i].toLong() + nums[i + 1] + nums[i + 2] + nums[i + 3] > target) {
break
}
if (nums[i].toLong() + nums[n - 3] + nums[n - 2] + nums[n - 1] < target) {
continue
}
for (j in i + 1 until nums.size - 2) {
if (j != i + 1 && nums[j] == nums[j - 1]) {
for (j in i + 1 until n - 2) {
if (j > i + 1 && nums[j] == nums[j - 1]) {
continue
}
var left = j + 1
var right = nums.size - 1
val half = nums[i] + nums[j]
if (half + nums[left] + nums[left + 1] > target) {
continue
if (nums[j].toLong() + nums[j + 1] + nums[j + 2] > target - nums[i]) {
break
}
if (half + nums[right] + nums[right - 1] < target) {
if (nums[j].toLong() + nums[n - 2] + nums[n - 1] < target - nums[i]) {
continue
}
while (left < right) {
val sum = nums[left] + nums[right] + half
if (sum == target) {
ret.add(listOf(nums[left++], nums[right--], nums[i], nums[j]))
while (nums[left] == nums[left - 1] && left < right) {
left++
val tempTarget = target - (nums[i] + nums[j])
var low = j + 1
var high = n - 1
while (low < high) {
val curSum = nums[low] + nums[high]
if (curSum == tempTarget) {
val tempList: MutableList<Int> = ArrayList()
tempList.add(nums[i])
tempList.add(nums[j])
tempList.add(nums[low])
tempList.add(nums[high])
result.add(tempList)
low++
high--
while (low < high && nums[low] == nums[low - 1]) {
low++
}
while (nums[right] == nums[right + 1] && left < right) {
right--
}
} else if (sum < target) {
left++
while (nums[left] == nums[left - 1] && left < right) {
left++
while (low < high && nums[high] == nums[high + 1]) {
high--
}
} else if (curSum < tempTarget) {
low++
} else {
right--
while (nums[right] == nums[right + 1] && left < right) {
right--
}
high--
}
}
}
}
return ret
return result
}
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ _Merge all the linked-lists into one sorted linked-list and return it._
```kotlin
import com_github_leetcode.ListNode

/*
* Example:
* var li = ListNode(5)
* var v = li.`val`
* Definition for singly-linked list.
* class ListNode(var `val`: Int) {
* var next: ListNode? = null
* }
*/
class Solution {
fun mergeKLists(lists: Array<ListNode>): ListNode? {
return if (lists.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ You may not alter the values in the list's nodes, only nodes themselves may be c
```kotlin
import com_github_leetcode.ListNode

/*
* Example:
* var li = ListNode(5)
* var v = li.`val`
* Definition for singly-linked list.
* class ListNode(var `val`: Int) {
* var next: ListNode? = null
* }
*/
class Solution {
fun reverseKGroup(head: ListNode?, k: Int): ListNode? {
if (head?.next == null || k == 1) {
Expand Down
1 change: 1 addition & 0 deletions src/main/kotlin/g0001_0100/s0047_permutations_ii/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Given a collection of numbers, `nums`, that might contain duplicates, return _al
```kotlin
class Solution {
private var ans: MutableList<List<Int>>? = null

fun permuteUnique(nums: IntArray): List<List<Int>> {
ans = ArrayList()
permute(nums, 0)
Expand Down
32 changes: 16 additions & 16 deletions src/main/kotlin/g0001_0100/s0056_merge_intervals/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,24 @@ Given an array of `intervals` where <code>intervals[i] = [start<sub>i</sub>, end
```kotlin
class Solution {
fun merge(intervals: Array<IntArray>): Array<IntArray> {
intervals.sortWith { a: IntArray, b: IntArray ->
Integer.compare(
a[0],
b[0]
)
}
val list: MutableList<IntArray> = ArrayList()
var current = intervals[0]
list.add(current)
for (next in intervals) {
if (current[1] >= next[0]) {
current[1] = Math.max(current[1], next[1])
} else {
current = next
list.add(current)
val result: MutableList<IntArray> = mutableListOf()
if (intervals.size <= 1) return intervals
intervals.sortBy { it.first() }
var currentRange = 1
var begin = intervals[0][0]
var end = intervals[0][1]
while (currentRange < intervals.size) {
if (intervals[currentRange][0] > end) {
result.add(intArrayOf(begin, end))
begin = intervals[currentRange][0]
end = intervals[currentRange][1]
} else if (intervals[currentRange][1] > end) {
end = intervals[currentRange][1]
}
currentRange++
}
return list.toTypedArray()
result.add(intArrayOf(begin, end))
return result.toTypedArray()
}
}
```
6 changes: 3 additions & 3 deletions src/main/kotlin/g0101_0200/s0112_path_sum/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ import com_github_leetcode.TreeNode
* }
*/
class Solution {
fun hasPathSum(root: TreeNode?, sum: Int): Boolean {
fun hasPathSum(root: TreeNode?, targetSum: Int): Boolean {
if (root == null) {
return false
}
return if (sum == root.`val` && root.left == null && root.right == null) {
return if (targetSum == root.`val` && root.left == null && root.right == null) {
true
} else hasPathSum(root.left, sum - root.`val`) || hasPathSum(root.right, sum - root.`val`)
} else hasPathSum(root.left, targetSum - root.`val`) || hasPathSum(root.right, targetSum - root.`val`)
}
}
```
14 changes: 7 additions & 7 deletions src/main/kotlin/g0101_0200/s0115_distinct_subsequences/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,23 @@ The test cases are generated so that the answer fits on a 32-bit signed integer.

```kotlin
class Solution {
fun numDistinct(text: String, text2: String): Int {
if (text.length < text2.length) {
fun numDistinct(s: String, t: String): Int {
if (s.length < t.length) {
return 0
}
if (text.length == text2.length) {
return if (text == text2) 1 else 0
if (s.length == t.length) {
return if (s == t) 1 else 0
}
val move = text.length - text2.length + 2
val move = s.length - t.length + 2
// Only finite number of character in s can occupy first position in T. Same applies for
// every character in T.
val dp = IntArray(move)
var j = 1
var k = 1
for (i in 0 until text2.length) {
for (i in 0 until t.length) {
var firstMatch = true
while (j < move) {
if (text2[i] == text[i + j - 1]) {
if (t[i] == s[i + j - 1]) {
if (firstMatch) {
// Keep track of first match. To avoid useless comparisons on next
// iteration.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ import com_github_leetcode.left_right.Node
* var next: Node? = null
* }
*/

class Solution {
fun connect(root: Node?): Node? {
if (root == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,32 +56,23 @@ import java.util.Queue
*/
class Solution {
fun connect(root: Node?): Node? {

if (root == null) return null

val bfsQueue: Queue<Node> = LinkedList()

bfsQueue.offer(root)
root.next = null

var temp: Node?
var prev: Node?

while (!bfsQueue.isEmpty()) {

val size = bfsQueue.size
prev = null

for (j in 0 until size) {

temp = bfsQueue.poll()
if (prev != null) prev.next = temp
if (temp!!.left != null) bfsQueue.offer(temp.left)
if (temp.right != null) bfsQueue.offer(temp.right)
prev = temp
}
}

return root
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/g0101_0200/s0120_triangle/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ The minimum path sum from top to bottom is 2 + 3 + 5 + 1 = 11 (underlined above)
import java.util.Arrays

class Solution {
fun minimumTotal(triangle: List<List<Int>>?): Int {
if (triangle == null || triangle.isEmpty()) {
fun minimumTotal(triangle: List<List<Int>>): Int {
if (triangle.isEmpty()) {
return 0
}
val dp = Array(triangle.size) { IntArray(triangle[triangle.size - 1].size) }
Expand Down
12 changes: 6 additions & 6 deletions src/main/kotlin/g0101_0200/s0126_word_ladder_ii/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ import java.util.LinkedList
import java.util.Queue

class Solution {
fun findLadders(beginWord: String, endWord: String, wordList: List<String>?): List<List<String?>> {
val ans: MutableList<List<String?>> = ArrayList()
fun findLadders(beginWord: String, endWord: String, wordList: List<String>): List<List<String>> {
val ans: MutableList<List<String>> = ArrayList()
// reverse graph start from endWord
val reverse: MutableMap<String, MutableSet<String>> = HashMap()
// remove the duplicate words
Expand Down Expand Up @@ -103,7 +103,7 @@ class Solution {
if (!findEnd) {
return ans
}
val path: MutableSet<String?> = LinkedHashSet()
val path: MutableSet<String> = LinkedHashSet()
path.add(endWord)
// traverse reverse graph from endWord to beginWord
findPath(endWord, beginWord, reverse, ans, path)
Expand All @@ -114,14 +114,14 @@ class Solution {
endWord: String,
beginWord: String,
graph: Map<String, MutableSet<String>>,
ans: MutableList<List<String?>>,
path: MutableSet<String?>
ans: MutableList<List<String>>,
path: MutableSet<String>
) {
val next = graph[endWord] ?: return
for (word in next) {
path.add(word)
if (beginWord == word) {
val shortestPath: List<String?> = ArrayList(path)
val shortestPath: List<String> = ArrayList(path)
// reverse words in shortest path
Collections.reverse(shortestPath)
// add the shortest path to ans.
Expand Down
1 change: 0 additions & 1 deletion src/main/kotlin/g0101_0200/s0133_clone_graph/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ import com_github_leetcode.neighbors.Node
* var neighbors: ArrayList<Node?> = ArrayList<Node?>()
* }
*/

class Solution {
fun cloneGraph(node: Node?): Node? {
return cloneGraph(node, HashMap())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ Your code will **only** be given the `head` of the original linked list.
```kotlin
import com_github_leetcode.random.Node

/*
* Example:
* var ti = Node(5)
* var v = ti.`val`
* Definition for a Node.
* class Node(var `val`: Int) {
* var next: Node? = null
* var random: Node? = null
* }
*/
class Solution {
fun copyRandomList(head: Node?): Node? {
if (head == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ There is a cycle in a linked list if there is some node in the list that can be
```kotlin
import com_github_leetcode.ListNode

/*
* Example:
* var li = ListNode(5)
* var v = li.`val`
* Definition for singly-linked list.
* class ListNode(var `val`: Int) {
* var next: ListNode? = null
* }
*/
class Solution {
fun detectCycle(head: ListNode?): ListNode? {
if (head?.next == null) {
Expand Down
12 changes: 9 additions & 3 deletions src/main/kotlin/g0101_0200/s0146_lru_cache/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,10 @@ lRUCache.get(4); // return 4

```kotlin
class LRUCache(capacity: Int) {

private val nodeMap = HashMap<Int, Node>()
val head = Node()
private val head = Node()
private val tail = Node()
var cacheCapacity = 0
private var cacheCapacity = 0

init {
head.next = tail
Expand Down Expand Up @@ -116,4 +115,11 @@ class LRUCache(capacity: Int) {

data class Node(var key: Int = -1, var value: Int = -1, var next: Node? = null, var prev: Node? = null)
}

/*
* Your LRUCache object will be instantiated and called as such:
* var obj = LRUCache(capacity)
* var param_1 = obj.get(key)
* obj.put(key,value)
*/
```
Loading