File tree 5 files changed +149
-0
lines changed
longest-substring-without-repeating-characters
5 files changed +149
-0
lines changed Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ func lengthOfLongestSubstring( _ s: String ) -> Int {
3
+ var charIndexMap = [ Character: Int] ( )
4
+ var maxLength = 0
5
+ var startIndex = 0
6
+
7
+ for (i, char) in Array ( s) . enumerated ( ) {
8
+ if let lastIndex = charIndexMap [ char] , lastIndex >= startIndex {
9
+ startIndex = lastIndex + 1
10
+ }
11
+
12
+ let currentLength = i - startIndex + 1
13
+ maxLength = max ( maxLength, currentLength)
14
+
15
+ charIndexMap [ char] = i
16
+ }
17
+
18
+ return maxLength
19
+ }
20
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ func numIslands( _ grid: [ [ Character ] ] ) -> Int {
3
+ guard !grid. isEmpty else { return 0 }
4
+
5
+ let rows = grid. count
6
+ let colums = grid [ 0 ] . count
7
+ var visited = grid
8
+ var islandCount = 0
9
+
10
+ func dfs( _ row: Int , _ col: Int ) {
11
+ guard row >= 0 && row < rows && col >= 0 && col < colums && visited [ row] [ col] == " 1 " else {
12
+ return
13
+ }
14
+
15
+ visited [ row] [ col] = " 0 "
16
+
17
+ dfs ( row - 1 , col)
18
+ dfs ( row + 1 , col)
19
+ dfs ( row, col - 1 )
20
+ dfs ( row, col + 1 )
21
+ }
22
+
23
+ for row in 0 ..< rows {
24
+ for col in 0 ..< colums {
25
+ if visited [ row] [ col] == " 1 " {
26
+ islandCount += 1
27
+ dfs ( row, col)
28
+ }
29
+ }
30
+ }
31
+
32
+ return islandCount
33
+ }
34
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ func reverseList( _ head: ListNode ? ) -> ListNode ? {
3
+ var current = head, previous : ListNode ? = nil
4
+
5
+ while current != nil {
6
+ let next = current? . next
7
+ current? . next = previous
8
+ previous = current
9
+ current = next
10
+ }
11
+
12
+ return previous
13
+ }
14
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ func setZeroes( _ matrix: inout [ [ Int ] ] ) {
3
+ let m = matrix. count
4
+ let n = matrix [ 0 ] . count
5
+
6
+ var firstRowHasZero = false
7
+ var firstColHasZero = false
8
+
9
+ for i in 0 ..< m {
10
+ if matrix [ i] [ 0 ] == 0 {
11
+ firstColHasZero = true
12
+ break
13
+ }
14
+ }
15
+
16
+ for j in 0 ..< n {
17
+ if matrix [ 0 ] [ j] == 0 {
18
+ firstRowHasZero = true
19
+ break
20
+ }
21
+ }
22
+
23
+ for i in 1 ..< m {
24
+ for j in 1 ..< n {
25
+ if matrix [ i] [ j] == 0 {
26
+ matrix [ i] [ 0 ] = 0
27
+ matrix [ 0 ] [ j] = 0
28
+ }
29
+ }
30
+ }
31
+
32
+ for i in 1 ..< m {
33
+ if matrix [ i] [ 0 ] == 0 {
34
+ for j in 1 ..< n {
35
+ matrix [ i] [ j] = 0
36
+ }
37
+ }
38
+ }
39
+
40
+ for j in 1 ..< n {
41
+ if matrix [ 0 ] [ j] == 0 {
42
+ for i in 1 ..< m {
43
+ matrix [ i] [ j] = 0
44
+ }
45
+ }
46
+ }
47
+
48
+ if firstRowHasZero {
49
+ for j in 0 ..< n {
50
+ matrix [ 0 ] [ j] = 0
51
+ }
52
+ }
53
+
54
+ if firstColHasZero {
55
+ for i in 0 ..< m {
56
+ matrix [ i] [ 0 ] = 0
57
+ }
58
+ }
59
+ }
60
+ }
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ func uniquePaths( _ m: Int , _ n: Int ) -> Int {
3
+ var dp = Array ( repeating: Array ( repeating: 0 , count: n) , count: m)
4
+
5
+ for j in 0 ..< n {
6
+ dp [ 0 ] [ j] = 1
7
+ }
8
+
9
+ for i in 0 ..< m {
10
+ dp [ i] [ 0 ] = 1
11
+ }
12
+
13
+ for i in 1 ..< m {
14
+ for j in 1 ..< n {
15
+ dp [ i] [ j] = dp [ i- 1 ] [ j] + dp[ i] [ j- 1 ]
16
+ }
17
+ }
18
+
19
+ return dp [ m- 1 ] [ n- 1 ]
20
+ }
21
+ }
You can’t perform that action at this time.
0 commit comments