Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
asahiocean
LeetCode
Commits
18646142
Unverified
Commit
18646142
authored
Jul 21, 2021
by
ASAHI OCEAN
Committed by
GitHub
Jul 21, 2021
Browse files
Merge pull request #211 from asahiocean/develop
32. Longest Valid Parentheses
parents
aab41f1e
4c1f0f8f
Changes
3
Hide whitespace changes
Inline
Side-by-side
String/.DS_Store
View file @
18646142
No preview for this file type
String/Longest Valid Parentheses/longest-valid-parentheses.playground/Contents.swift
0 → 100644
View file @
18646142
import
Foundation
// 32. Longest Valid Parentheses
// https://leetcode.com/problems/longest-valid-parentheses/
// Discuss: https://vk.cc/c47dUk
class
Solution
{
func
longestValidParentheses
(
_
s
:
String
)
->
Int
{
var
value
=
0
,
start
=
0
,
stack
=
[
Int
]()
for
(
i
,
ch
)
in
s
.
enumerated
()
{
switch
ch
==
"("
{
case
true
:
stack
.
append
(
i
)
default
:
if
!
stack
.
isEmpty
{
stack
.
removeLast
()
if
let
last
=
stack
.
last
{
value
=
max
(
value
,
i
-
last
)
}
else
{
value
=
max
(
value
,
i
-
start
+
1
)
}
}
else
{
start
=
i
+
1
}
}
}
return
value
}
}
import
XCTest
// Executed 3 tests, with 0 failures (0 unexpected) in 0.013 (0.015) seconds
class
Tests
:
XCTestCase
{
private
let
s
=
Solution
()
func
test0
()
{
let
res
=
s
.
longestValidParentheses
(
"(()"
)
XCTAssertEqual
(
res
,
2
)
}
func
test1
()
{
let
res
=
s
.
longestValidParentheses
(
")()())"
)
XCTAssertEqual
(
res
,
4
)
}
func
test2
()
{
let
res
=
s
.
longestValidParentheses
(
""
)
XCTAssertEqual
(
res
,
0
)
}
}
Tests
.
defaultTestSuite
.
run
()
String/Longest Valid Parentheses/longest-valid-parentheses.playground/contents.xcplayground
0 → 100644
View file @
18646142
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground
version=
'5.0'
target-platform=
'ios'
>
<timeline
fileName=
'timeline.xctimeline'
/>
</playground>
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment