forked from soapyigu/LeetCode-Swift
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add solution to Strobogrammatic Number I and II
- Loading branch information
Showing
3 changed files
with
92 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* Question Link: https://leetcode.com/problems/strobogrammatic-number/ | ||
* Primary idea: Two pointers, compare two characters until they are all valid | ||
* | ||
* Time Complexity: O(n), Space Complexity: O(1) | ||
* | ||
*/ | ||
|
||
class StrobogrammaticNumber { | ||
func isStrobogrammatic(_ num: String) -> Bool { | ||
let numChars = Array(num) | ||
var i = 0, j = num.count - 1 | ||
|
||
while i <= j { | ||
if isValid(numChars[i], numChars[j]) { | ||
i += 1 | ||
j -= 1 | ||
} else { | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} | ||
|
||
fileprivate func isValid(_ charA: Character, _ charB: Character) -> Bool { | ||
if charA == charB { | ||
return ["0", "1", "8"].contains(charA) | ||
} else { | ||
if (charA == "6" && charB == "9") || (charA == "9" && charB == "6") { | ||
return true | ||
} else { | ||
return false | ||
} | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* Question Link: https://leetcode.com/problems/strobogrammatic-number-ii/ | ||
* Primary idea: Classic DFS, set two places with correspond characters; | ||
* starting from head and tail, and then move towards middle to cover all places. | ||
|
||
* Time Complexity: O(m^n), here m is 5; Space Complexity: O(n) | ||
* | ||
*/ | ||
|
||
class StrobogrammaticNumberII { | ||
func findStrobogrammatic(_ n: Int) -> [String] { | ||
var res = [String]() | ||
|
||
guard n >= 1 else { | ||
return res | ||
} | ||
|
||
let left = Array("01689"), right = Array("01986") | ||
var path = Array(repeating: Character("-"), count: n) | ||
|
||
dfs(&res, left, right, 0, n - 1, &path) | ||
|
||
return res | ||
} | ||
|
||
fileprivate func dfs(_ res: inout [String], _ left: [Character], _ right: [Character], _ leftIdx: Int, _ path: inout [Character]) { | ||
let rightIdx = path.count - leftIdx - 1 | ||
|
||
if leftIdx > rightIdx { | ||
res.append(String(path)) | ||
return | ||
} | ||
|
||
for i in 0..<left.count { | ||
if leftIdx == 0 && leftIdx != rightIdx && left[i] == "0" { | ||
continue | ||
} | ||
|
||
if leftIdx == rightIdx && (left[i] == "6" || left[i] == "9") { | ||
continue | ||
} | ||
|
||
path[leftIdx] = left[i] | ||
path[rightIdx] = right[i] | ||
|
||
dfs(&res, left, right, leftIdx + 1, rightIdx - 1, &path) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters