File tree Expand file tree Collapse file tree 3 files changed +112
-0
lines changed Expand file tree Collapse file tree 3 files changed +112
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ func isValid( _ s: String ) -> Bool {
3
+ var stack1 = Stack ( Array ( s) )
4
+ var stack2 = Stack ( [ Character] ( ) )
5
+
6
+ while !stack1. isEmpty {
7
+ let a = stack1. pop ( ) !
8
+ if let b = stack2. peek ( ) {
9
+ if map ( a) > 0 && map ( a) + map( b) == 0 {
10
+ stack2. pop ( )
11
+ } else {
12
+ stack2. push ( a)
13
+ }
14
+ } else {
15
+ stack2. push ( a)
16
+ }
17
+ }
18
+
19
+ if stack2. isEmpty {
20
+ return true
21
+ } else {
22
+ return false
23
+ }
24
+ }
25
+
26
+ func map( _ character: Character ) -> Int {
27
+ switch character {
28
+ case " ( " : return 1
29
+ case " ) " : return - 1
30
+ case " { " : return 2
31
+ case " } " : return - 2
32
+ case " [ " : return 4
33
+ case " ] " : return - 4
34
+ default : return 0
35
+ }
36
+ }
37
+ }
38
+
39
+ struct Stack < Element> {
40
+
41
+ private var storage : [ Element ] = [ ]
42
+
43
+ public init ( _ elements: [ Element ] ) {
44
+ storage = elements
45
+ }
46
+
47
+ public mutating func push( _ element: Element ) {
48
+ storage. append ( element)
49
+ }
50
+
51
+ @discardableResult
52
+ public mutating func pop( ) -> Element ? {
53
+ return storage. popLast ( )
54
+ }
55
+
56
+ func peek( ) -> Element ? {
57
+ return storage. last
58
+ }
59
+
60
+ public var isEmpty : Bool {
61
+ return peek ( ) == nil
62
+ }
63
+ }
64
+
65
+ // Runtime: 12 ms, faster than 52.39% of Swift online submissions for Valid Parentheses.
66
+ // Memory Usage: 19.7 MB, less than 12.25% of Swift online submissions for Valid Parentheses.
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ func search( _ nums: [ Int ] , _ target: Int ) -> Int {
3
+ if nums. count == 0 { return - 1 }
4
+ let breakPoint = find_breakPoint ( nums)
5
+ return binary_search ( nums, target, offset: breakPoint)
6
+ }
7
+
8
+ func find_breakPoint( _ numbers: [ Int ] ) -> Int {
9
+ for (index, number) in numbers. enumerated ( ) {
10
+ if number < numbers [ 0 ] { return index }
11
+ }
12
+ return 0
13
+ }
14
+
15
+ func binary_search( _ numbers: [ Int ] , _ target: Int , offset: Int ) -> Int {
16
+ if numbers. count == 0 { return - 1 }
17
+ var low = 0
18
+ var high = numbers. count - 1
19
+
20
+ while low <= high {
21
+ let middle = ( low + high) / 2
22
+ let index = realIndex ( middle, count: numbers. count, offset: offset)
23
+ let number = numbers [ index]
24
+ if number == target {
25
+ return index
26
+ } else if number < target {
27
+ low = middle + 1
28
+ } else {
29
+ high = middle - 1
30
+ }
31
+ }
32
+
33
+ return - 1
34
+ }
35
+
36
+ func realIndex( _ index: Int , count: Int , offset: Int ) -> Int {
37
+ if index < count- offset {
38
+ return index+ offset
39
+ } else {
40
+ return index+ offset- count
41
+ }
42
+ }
43
+ }
44
+
45
+ // Runtime: 20 ms, faster than 100.00% of Swift online submissions for Search in Rotated Sorted Array.
46
+ // Memory Usage: 19 MB, less than 33.33% of Swift online submissions for Search in Rotated Sorted Array.
File renamed without changes.
You can’t perform that action at this time.
0 commit comments