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 033e005

Browse files
author
C5141506
committed
NonOverlapingInterval leetcode problem
1 parent e2b62b7 commit 033e005

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package kotlin_problem
2+
3+
import java.util.Stack
4+
5+
/*
6+
* takes in a string of parentheses (e.g. "(()())") and returns true if the parentheses are balanced,
7+
* false otherwise. A string of parentheses is balanced if every opening parenthesis has a matching
8+
* closing parenthesis in the correct order.Example: Input: "(()())" Output: trueInput: "(()(" Output: false
9+
*
10+
*
11+
* */
12+
13+
fun main() {
14+
val inuptStr = "s()"
15+
println(isValid(inuptStr))
16+
17+
}
18+
19+
fun isValid(inuptStr: String): Boolean {
20+
val stack = Stack<Char>()
21+
var i = 0
22+
while (i < inuptStr.length) {
23+
if (inuptStr[i] == '(') {
24+
stack.push('(')
25+
} else {
26+
stack.pop()
27+
}
28+
i++;
29+
}
30+
31+
32+
33+
return !stack.isNotEmpty()
34+
}

0 commit comments

Comments
(0)

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