File tree Expand file tree Collapse file tree 5 files changed +164
-0
lines changed
container-with-most-water
design-add-and-search-words-data-structure
longest-increasing-subsequence Expand file tree Collapse file tree 5 files changed +164
-0
lines changed Original file line number Diff line number Diff line change 1+ class Solution {
2+ func maxArea( _ height: [ Int ] ) -> Int {
3+ var start = 0
4+ var end = height. count - 1
5+ var result = 0
6+
7+ while start < end {
8+ let width = end - start
9+ let waters = width * ( min ( height [ start] , height [ end] ) )
10+
11+ result = max ( result, waters)
12+
13+ if height [ start] > height [ end] {
14+ end = end - 1
15+ } else {
16+ start = start + 1
17+ }
18+ }
19+
20+ return result
21+ }
22+ }
Original file line number Diff line number Diff line change 1+ class WordDictionary {
2+ class TrieNode {
3+ var children : [ Character : TrieNode ] = [ : ]
4+ var isEndOfWord : Bool = false
5+ }
6+
7+ private let root : TrieNode
8+
9+ init ( ) {
10+ root = TrieNode ( )
11+ }
12+
13+ func addWord( _ word: String ) {
14+ let key = word. lowercased ( )
15+ var current = root
16+
17+ for char in key {
18+ if current. children [ char] == nil {
19+ current. children [ char] = TrieNode ( )
20+ }
21+
22+ if let node = current. children [ char] {
23+ current = node
24+ }
25+ }
26+
27+ current. isEndOfWord = true
28+ }
29+
30+ func search( _ word: String ) -> Bool {
31+ let key = word. lowercased ( )
32+ return searchInNode ( Array ( key) , 0 , root)
33+ }
34+
35+ private func searchInNode( _ word: [ Character ] , _ index: Int , _ node: TrieNode ) -> Bool {
36+ if index == word. count {
37+ return node. isEndOfWord
38+ }
39+
40+ let currentChar = word [ index]
41+
42+ if currentChar == " . " {
43+ for (_, childNode) in node. children {
44+ if searchInNode ( word, index + 1 , childNode) {
45+ return true
46+ }
47+ }
48+ return false
49+ } else {
50+ guard let nextNode = node. children [ currentChar] else {
51+ return false
52+ }
53+
54+ return searchInNode ( word, index + 1 , nextNode)
55+ }
56+ }
57+ }
Original file line number Diff line number Diff line change 1+ class Solution {
2+ func lengthOfLIS( _ nums: [ Int ] ) -> Int {
3+ if nums. isEmpty {
4+ return 0
5+ }
6+
7+ let n = nums. count
8+ var dp = Array ( repeating: 1 , count: n)
9+
10+ for i in 1 ..< n {
11+ for j in 0 ..< i {
12+ if nums [ i] > nums [ j] {
13+ dp [ i] = max ( dp [ i] , dp [ j] + 1 )
14+ }
15+ }
16+ }
17+
18+ return dp. max ( ) ?? 1
19+ }
20+ }
Original file line number Diff line number Diff line change 1+ class Solution {
2+ func spiralOrder( _ matrix: [ [ Int ] ] ) -> [ Int ] {
3+ guard !matrix. isEmpty else { return [ ] }
4+
5+ var answer : [ Int ] = [ ]
6+
7+ var top = 0
8+ var bottom = matrix. count - 1
9+ var left = 0
10+ var right = matrix [ 0 ] . count - 1
11+
12+ while top <= bottom && left <= right {
13+ for i in left... right {
14+ answer. append ( matrix [ top] [ i] )
15+ }
16+ top += 1
17+
18+ if top <= bottom {
19+ for i in top... bottom {
20+ answer. append ( matrix [ i] [ right] )
21+ }
22+ }
23+ right -= 1
24+
25+ if top <= bottom && left <= right {
26+ for i in stride ( from: right, through: left, by: - 1 ) {
27+ answer. append ( matrix [ bottom] [ i] )
28+ }
29+ bottom -= 1
30+ }
31+
32+ if left <= right && top <= bottom {
33+ for i in stride ( from: bottom, through: top, by: - 1 ) {
34+ answer. append ( matrix [ i] [ left] )
35+ }
36+ left += 1
37+ }
38+ }
39+
40+ return answer
41+ }
42+ }
Original file line number Diff line number Diff line change 1+ class Solution {
2+ func isValid( _ s: String ) -> Bool {
3+ let pair : [ Character : Character ] = [
4+ " ( " : " ) " ,
5+ " [ " : " ] " ,
6+ " { " : " } "
7+ ]
8+
9+ var stack : [ Character ] = [ ]
10+
11+ for char in s {
12+ if pair. keys. contains ( char) {
13+ stack. append ( char)
14+ } else {
15+ if stack. isEmpty || pair [ stack. removeLast ( ) ] != char {
16+ return false
17+ }
18+ }
19+ }
20+
21+ return stack. isEmpty
22+ }
23+ }
You can’t perform that action at this time.
0 commit comments