Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 25c659e

Browse files
author
kkarpyshev
committed
Merge remote-tracking branch 'origin/master'
2 parents 8db9190 + 1286cde commit 25c659e

File tree

4 files changed

+146
-1
lines changed

4 files changed

+146
-1
lines changed

‎src/medium/138. Copy List with Random Pointer .kt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import LinkedListTopic
2222
* Your code will only be given the head of the original linked list.
2323
*/
2424

25-
class Medium138: HashTableTopic, LinkedListTopic {
25+
class Medium138: HashTableTopic, LinkedListTopic {
2626

2727
fun copyRandomList(head: Node?): Node? {
2828
head ?: return null

‎src/medium/71. Simplify Path .kt‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package medium
2+
3+
import StackTopic
4+
import StringTopic
5+
import java.util.*
6+
7+
/**
8+
* 71. Simplify Path
9+
* https://leetcode.com/problems/simplify-path/
10+
*
11+
Given a string path, which is an absolute path (starting with a slash '/') to a file or directory in a Unix-style file system, convert it to the simplified canonical path.
12+
13+
In a Unix-style file system, a period '.' refers to the current directory, a double period '..' refers to the directory up a level, and any multiple consecutive slashes (i.e. '//') are treated as a single slash '/'. For this problem, any other format of periods such as '...' are treated as file/directory names.
14+
15+
The canonical path should have the following format:
16+
17+
The path starts with a single slash '/'.
18+
Any two directories are separated by a single slash '/'.
19+
The path does not end with a trailing '/'.
20+
The path only contains the directories on the path from the root directory to the target file or directory (i.e., no period '.' or double period '..')
21+
Return the simplified canonical path.
22+
*/
23+
24+
class Medium71 : StringTopic, StackTopic {
25+
26+
fun simplifyPath(path: String): String {
27+
val stack = Stack<String>()
28+
var start = -1
29+
for (i in 0..path.length) {
30+
if (i == path.length || path[i] == '/') {
31+
if (start > 0) {
32+
when (val dir = path.substring(start, i)) {
33+
"." -> Unit
34+
".." -> if (stack.isNotEmpty()) stack.pop()
35+
else -> stack.add(dir)
36+
}
37+
start = -1
38+
}
39+
} else {
40+
if (start < 0) start = i
41+
}
42+
}
43+
return stack.joinToString("/", "/")
44+
}
45+
}
46+
47+
fun main() {
48+
println(Medium71().simplifyPath("/home/")) // /home
49+
println(Medium71().simplifyPath("/../")) // /
50+
println(Medium71().simplifyPath("/home//foo/")) // /home/foo
51+
println(Medium71().simplifyPath("/a/./b/../../c/")) // /c
52+
println(Medium71().simplifyPath("/...")) // /
53+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package medium
2+
3+
import StackTopic
4+
import StringTopic
5+
import java.util.*
6+
7+
8+
/**
9+
* 856. Score of Parentheses
10+
* https://leetcode.com/problems/score-of-parentheses/
11+
*
12+
Given a balanced parentheses string s, return the score of the string.
13+
The score of a balanced parentheses string is based on the following rule:
14+
"()" has score 1.
15+
AB has score A + B, where A and B are balanced parentheses strings.
16+
(A) has score 2 * A, where A is a balanced parentheses string.
17+
BULLSHIT
18+
*/
19+
20+
class Medium856 : StringTopic, StackTopic {
21+
22+
fun scoreOfParentheses(s: String): Int {
23+
val stack = Stack<Int>()
24+
stack.push(0)
25+
for (i in s.indices) {
26+
if (s[i] == '(') {
27+
stack.push(0)
28+
} else {
29+
val v = stack.pop()
30+
val w = stack.pop()
31+
stack.push(w + maxOf(2 * v, 1))
32+
}
33+
}
34+
return stack.pop()
35+
}
36+
}
37+
38+
fun main() {
39+
println(Medium856().scoreOfParentheses("()"))
40+
println(Medium856().scoreOfParentheses("(())"))
41+
println(Medium856().scoreOfParentheses("()()"))
42+
println(Medium856().scoreOfParentheses("(()(()))"))
43+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package medium
2+
3+
import ArraysTopic
4+
import StackTopic
5+
import java.util.*
6+
7+
/**
8+
* 946. Validate Stack Sequences
9+
* https://leetcode.com/problems/validate-stack-sequences/
10+
*
11+
Given two integer arrays pushed and popped each with distinct values,
12+
return true if this could have been the result of a sequence of push and pop operations on an initially empty stack, or false otherwise.
13+
*/
14+
15+
class Medium946 : ArraysTopic, StackTopic {
16+
17+
fun validateStackSequences(pushed: IntArray, popped: IntArray): Boolean {
18+
val stack = Stack<Int>()
19+
var i = 0
20+
var j = 0
21+
while (i < pushed.size || j < popped.size) {
22+
if (stack.isEmpty()) {
23+
if (i < pushed.size) {
24+
stack.push(pushed[i])
25+
i++
26+
} else return false
27+
} else {
28+
if (j < popped.size) {
29+
if (popped[j] == stack.peek()) {
30+
stack.pop()
31+
j++
32+
} else {
33+
if (i < pushed.size) {
34+
stack.push(pushed[i])
35+
i++
36+
} else return false
37+
}
38+
} else return false
39+
}
40+
}
41+
return true
42+
}
43+
}
44+
45+
fun main() {
46+
println(Medium946().validateStackSequences(intArrayOf(1, 2, 3, 4, 5), intArrayOf(4, 5, 3, 2, 1)))
47+
println(Medium946().validateStackSequences(intArrayOf(1, 2, 3, 4, 5), intArrayOf(4, 3, 5, 1, 2)))
48+
49+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /