Add examples on Swift 3.0.2 (#11)
* 01_binary_search Swift 3.0.2 * 01_binary_search Swift 3.0.2 * add Chapter 2 - 01_selection_sort Swift 3.0.2 example * 01_binary_search cosmetic note updates Swift 3.0.2 * 03_recursion Swift 3.0.2 examples * 04_quicksort Swift 3.0.2 examples * fix typo on quicksort example. Swift 3.0.2 * add 05_hash_tables examples on Swift 3.0.2 * add 01_breadth-first_search Swift 3.0.2 example * 01_breadth-first_search fixing typo Swift 3.0.2 * add 01_dijkstras_algorithm on Swift 3.0.2 * add 08_greedy_algorithms Swift 3.0.2 example * 01_longest_common_subsequence Swift 3.0.2 example
This commit is contained in:
committed by
Aditya Bhargava
parent
62ed616954
commit
12265a8c61
@@ -0,0 +1,40 @@
|
||||
import Foundation
|
||||
|
||||
if wordA[i] == wordB[j] {
|
||||
// The letters match
|
||||
cell[i][j] = cell[i-1][j-1] + 1
|
||||
} else {
|
||||
// The letters don't match.
|
||||
cell[i][j] = max(cell[i-1][j], cell[i][j-1])
|
||||
}
|
||||
|
||||
/* Working example for "play" with
|
||||
|
||||
var wordA = "fish"
|
||||
var wordB = "hish"
|
||||
var cell : [[Int]] = Array(repeating: Array(repeating: 0, count: wordA.characters.count), count: wordB.characters.count)
|
||||
let wordAArray = wordA.characters.map { String($0) }
|
||||
let wordBArray = wordB.characters.map { String($0) }
|
||||
|
||||
for i in 0...wordA.characters.count-1 {
|
||||
for j in 0...wordB.characters.count-1 {
|
||||
// The letters match
|
||||
if wordAArray[i] == wordBArray[j] {
|
||||
if i > 0 && j > 0 {
|
||||
cell[i][j] = cell[i-1][j-1] + 1
|
||||
} else {
|
||||
cell[i][j] = 1
|
||||
}
|
||||
} else {
|
||||
// The letters don't match.
|
||||
if i > 0 && j > 0 {
|
||||
cell[i][j] = max(cell[i-1][j], cell[i][j-1])
|
||||
} else {
|
||||
cell[i][j] = 0
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user