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
90 changes: 87 additions & 3 deletions README.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/main/kotlin/g0001_0100/s0001_two_sum/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class Solution {
for (i in numbers.indices) {
val requiredNum = target - numbers[i]
if (indexMap.containsKey(requiredNum)) {
return intArrayOf(indexMap[requiredNum]!!, i)
return intArrayOf(indexMap.getValue(requiredNum), i)
}
indexMap[numbers[i]] = i
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Solution {
var temp = strs[0]
var i = 1
var cur: String
while (temp.length > 0 && i < strs.size) {
while (temp.isNotEmpty() && i < strs.size) {
if (temp.length > strs[i].length) {
temp = temp.substring(0, strs[i].length)
}
Expand Down
6 changes: 3 additions & 3 deletions src/main/kotlin/g0001_0100/s0020_valid_parentheses/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ class Solution {
val c = element
if (c == '(' || c == '[' || c == '{') {
stack.push(c)
} else if (c == ')' && !stack.isEmpty() && stack.peek() == '(') {
} else if (c == ')' && stack.isNotEmpty() && stack.peek() == '(') {
stack.pop()
} else if (c == '}' && !stack.isEmpty() && stack.peek() == '{') {
} else if (c == '}' && stack.isNotEmpty() && stack.peek() == '{') {
stack.pop()
} else if (c == ']' && !stack.isEmpty() && stack.peek() == '[') {
} else if (c == ']' && stack.isNotEmpty() && stack.peek() == '[') {
stack.pop()
} else {
return false
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/g0001_0100/s0049_group_anagrams/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class Solution {
hm.computeIfAbsent(
temp
) { _: String? -> ArrayList() }
hm[temp]!!.add(s)
hm.getValue(temp).add(s)
}
return ArrayList<List<String>>(hm.values)
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/g0001_0100/s0071_simplify_path/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class Solution {
}
val s = path.substring(start, end)
if (s == "..") {
if (!stk.isEmpty()) {
if (stk.isNotEmpty()) {
stk.pop()
}
} else if (s != "." && s != "") {
Expand All @@ -77,7 +77,7 @@ class Solution {
start = end + 1
}
val ans = StringBuilder()
while (!stk.isEmpty()) {
while (stk.isNotEmpty()) {
ans.insert(0, stk.pop())
ans.insert(0, "/")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ class Solution {
queue.add(localRoot)
queue.add(null)
var level: MutableList<Int> = ArrayList()
while (!queue.isEmpty()) {
while (queue.isNotEmpty()) {
localRoot = queue.remove()
while (!queue.isEmpty() && localRoot != null) {
while (queue.isNotEmpty() && localRoot != null) {
level.add(localRoot.`val`)
if (localRoot.left != null) {
queue.add(localRoot.left)
Expand All @@ -75,7 +75,7 @@ class Solution {
}
result.add(level)
level = ArrayList()
if (!queue.isEmpty()) {
if (queue.isNotEmpty()) {
queue.add(null)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ class Solution {
q.add(null)
var zig = true
var level = LinkedList<Int>()
while (!q.isEmpty()) {
while (q.isNotEmpty()) {
var node: TreeNode? = q.remove()
while (!q.isEmpty() && node != null) {
while (q.isNotEmpty() && node != null) {
if (zig) {
level.add(node.`val`)
} else {
Expand All @@ -79,7 +79,7 @@ class Solution {
result.add(level)
zig = !zig
level = LinkedList()
if (!q.isEmpty()) {
if (q.isNotEmpty()) {
q.add(null)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ import java.util.HashMap
class Solution {
private var j = 0
private val map: MutableMap<Int, Int> = HashMap()

fun get(key: Int): Int {
return map[key]!!
return map.getValue(key)
}

private fun answer(preorder: IntArray, inorder: IntArray, start: Int, end: Int): TreeNode? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class Solution {
val queue: Queue<TreeNode> = LinkedList()
queue.add(root)
var d = 0
while (!queue.isEmpty()) {
while (queue.isNotEmpty()) {
val size: Int = queue.size
for (i in 0 until size) {
val current: TreeNode = queue.poll()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class Solution {
root.next = null
var temp: Node?
var prev: Node?
while (!bfsQueue.isEmpty()) {
while (bfsQueue.isNotEmpty()) {
val size = bfsQueue.size
prev = null
for (j in 0 until size) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/g0101_0200/s0126_word_ladder_ii/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class Solution {
// find endWord flag
var findEnd = false
// traverse current layer nodes
while (!queue.isEmpty()) {
while (queue.isNotEmpty()) {
val word = queue.remove()
for (next in wordSet) {
// is ladder words
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/g0101_0200/s0127_word_ladder/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class Solution {
val strLen = beginWord.length
beginSet.add(beginWord)
endSet.add(endWord)
while (!beginSet.isEmpty() && !endSet.isEmpty()) {
while (beginSet.isNotEmpty() && endSet.isNotEmpty()) {
if (beginSet.size > endSet.size) {
val temp = beginSet
beginSet = endSet
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class Solution {
var lastNum = Integer.MIN_VALUE
var length = 0
var maxLength = 1
while (!queue.isEmpty()) {
while (queue.isNotEmpty()) {
val num = queue.poll()
if (num == lastNum) {
continue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class Solution {
}
val stack: Stack<TreeNode> = Stack<TreeNode>()
var current: TreeNode? = root
while (current != null || !stack.isEmpty()) {
while (current != null || stack.isNotEmpty()) {
while (current != null) {
result.add(current.`val`)
stack.push(current.right)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class Solution {
if (op.contains(t)) {
val b = stack.removeFirst().toInt()
val a = stack.removeFirst().toInt()
val c = op[t]!!.apply(a, b)
val c = op.getValue(t).apply(a, b)
stack.addFirst(c.toString())
} else {
stack.addFirst(t)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class Solution {
val map: MutableMap<Long, Int> = HashMap()
while (remainder != 0L) {
if (map.containsKey(remainder)) {
sb.insert(map[remainder]!!, "(")
sb.insert(map.getValue(remainder), "(")
sb.append(")")
break
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class Solution {
return
}
if (n == 0 && tmp.size == k) {
a.add(tmp.map { it -> it }.toMutableList())
a.add(tmp.map { it }.toMutableList())
return
}
for (i in index until 10) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ class Solution {
if (d.containsKey(m)) {
return true
}
if (d.containsKey(m - 1) && Math.abs(nums[i] - d[m - 1]!!) < w) {
if (d.containsKey(m - 1) && Math.abs(nums[i] - d.getValue(m - 1)) < w) {
return true
}
if (d.containsKey(m + 1) && Math.abs(nums[i] - d[m + 1]!!) < w) {
if (d.containsKey(m + 1) && Math.abs(nums[i] - d.getValue(m + 1)) < w) {
return true
}
d[m] = nums[i].toLong()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class Solution {
var i = 0
var j = 0
while (j < nums.size) {
while (!dq.isEmpty() && dq.peekLast() < nums[j]) {
while (dq.isNotEmpty() && dq.peekLast() < nums[j]) {
dq.pollLast()
}
dq.addLast(nums[j])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class Solution {

private fun diffWayToCompute(expression: String, hashMap: HashMap<String, List<Int>>): List<Int> {
if (hashMap.containsKey(expression)) {
return hashMap[expression]!!
return hashMap.getValue(expression)
}
val newList = arrayListOf<Int>()
if (!hasOperatorInBetween(expression)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Note that operands in the returned expressions **should not** contain leading ze
class Solution {
fun addOperators(num: String, target: Int): List<String> {
val res: MutableList<String> = ArrayList()
if (num.length == 0 || java.lang.Long.valueOf(num) > Int.MAX_VALUE) {
if (num.isEmpty() || java.lang.Long.valueOf(num) > Int.MAX_VALUE) {
return res
}
val list = num.toCharArray()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class Solution {

private fun dfs(map: Map<String, PriorityQueue<String>>, src: String, ans: LinkedList<String>) {
val temp = map[src]
while (temp != null && !temp.isEmpty()) {
while (!temp.isNullOrEmpty()) {
val nbr = temp.remove()
dfs(map, nbr, ans)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class Solution {
for (i in 0 until nums2.size) {
if (a.getOrDefault(nums2[i], 0)> 0) {
s.add(nums2[i])
a[nums2[i]] = a[nums2[i]]!! - 1
a[nums2[i]] = a.getValue(nums2[i]) - 1
}
}
return s.toIntArray()
Expand Down
8 changes: 4 additions & 4 deletions src/main/kotlin/g0301_0400/s0355_design_twitter/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ class Twitter {
fun getNewsFeed(userId: Int): List<Int> {
checkNewUser(userId)
val res: MutableList<Int> = ArrayList()
val followers = twitterData[userId]!!
val followers = twitterData.getValue(userId)
var t = head.next
while (t != null && res.size < 10) {
if (followers.contains(t.userId)) {
Expand All @@ -89,7 +89,7 @@ class Twitter {
fun follow(followerId: Int, followeeId: Int) {
checkNewUser(followeeId)
checkNewUser(followerId)
twitterData[followerId]!!.add(followeeId)
twitterData.getValue(followerId).add(followeeId)
}

fun unfollow(followerId: Int, followeeId: Int) {
Expand All @@ -99,7 +99,7 @@ class Twitter {
return
}
checkNewUser(followerId)
twitterData[followerId]!!.remove(followeeId)
twitterData.getValue(followerId).remove(followeeId)
}

fun checkNewUser(userId: Int) {
Expand All @@ -108,7 +108,7 @@ class Twitter {
}
twitterData[userId] = HashSet()
// follow yourself
twitterData[userId]!!.add(userId)
twitterData.getValue(userId).add(userId)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class Solution {
}
}
var i = 1
while (i <= k && !queue.isEmpty()) {
while (i <= k && queue.isNotEmpty()) {
val cur = queue.poll()
res.add(cur.al)
val next = cur.index
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,17 +117,17 @@ class Solution {
if (nextLevel < currLevel) {
var j = 0
if (isFile) {
while (!stack.isEmpty() && j < currLevel - nextLevel) {
while (stack.isNotEmpty() && j < currLevel - nextLevel) {
currDirLen -= stack.pop()
j++
}
} else {
while (!stack.isEmpty() && j <= currLevel - nextLevel) {
while (stack.isNotEmpty() && j <= currLevel - nextLevel) {
currDirLen -= stack.pop()
j++
}
}
} else if (nextLevel == currLevel && !isFile && !stack.isEmpty()) {
} else if (nextLevel == currLevel && !isFile && stack.isNotEmpty()) {
currDirLen -= stack.pop()
}
if (nextLevel == 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class Solution {
visited1[r][cols - 1] = true
}
// end build wall
while (!walls.isEmpty()) {
while (walls.isNotEmpty()) {
val min = walls.poll()
visit(heightMap, min, walls)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class Solution {
}
val queue: Queue<Node> = LinkedList<Node>()
queue.offer(root)
while (!queue.isEmpty()) {
while (queue.isNotEmpty()) {
val size: Int = queue.size
val level: MutableList<Int> = ArrayList()
for (i in 0 until size) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class Solution {
val queue: Queue<String> = LinkedList()
queue.offer(start)
var step = 0
while (!queue.isEmpty()) {
while (queue.isNotEmpty()) {
var curSize = queue.size
while (curSize-- > 0) {
val cur = queue.poll()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ class Solution {
fun countSegments(s: String): Int {
var s = s
s = s.trim { it <= ' ' }
if (s.length == 0) {
if (s.isEmpty()) {
return 0
}
val splitted = s.split(" ".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
var result = 0
for (value in splitted) {
if (value.length > 0) {
if (value.isNotEmpty()) {
result++
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/g0401_0500/s0456_132_pattern/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class Solution {
if (nums[i] < s3) {
return true
} else {
while (!stack.isEmpty() && nums[i] > stack.peek()) {
while (stack.isNotEmpty() && nums[i] > stack.peek()) {
s3 = Math.max(s3, stack.pop())
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ For example, "`2001:0db8:85a3:0000:0000:8a2e:0370:7334"` and "`2001:db8:85a3:0:0
```kotlin
class Solution {
fun validIPAddress(ip: String): String {
if (ip.length == 0) {
if (ip.isEmpty()) {
return NEITHER
}
val arr = ip.split("\\.".toRegex()).toTypedArray()
Expand Down
4 changes: 2 additions & 2 deletions src/main/kotlin/g0401_0500/s0488_zuma_game/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,10 @@ class Solution {
}

private fun findMinStepDp(board: String, hand: String, dp: MutableMap<String, MutableMap<String, Int?>?>): Int {
if (board.length == 0) {
if (board.isEmpty()) {
return 0
}
if (hand.length == 0) {
if (hand.isEmpty()) {
return -1
}
if (dp[board] != null && dp[board]!![hand] != null) {
Expand Down
Loading