Upgrade to Pro — share decks privately, control downloads, hide ads and more …

swift warmup 9/6

Johnlin
September 06, 2016

swift warmup 9/6

Johnlin

September 06, 2016
Tweet

More Decks by Johnlin

Other Decks in Programming

Transcript

  1. • Given a string, determine if it is a palindrome,

    considering only alphanumeric characters and ignoring cases. • څ㟬Ұݸࣈ۲ɼ൑Ꮧଞੋෆੋճจɻ୞ཁߟྀӳจࣈ ฼࿨Ꮠࣈɼࠌུେখሜ
  2. • For example,
 "A man, a plan, a canal: Panama"

    is a palindrome.
 "race a car" is not a palindrome. • Ꭿྫိ㘸
 "A man, a plan, a canal: Panama" ੋճจ.
 "race a car" ෆੋճจ.
  3. • Note:
 Have you consider that the string might be

    empty? This is a good question to ask during an interview.
 For the purpose of this problem, we define empty string as valid palindrome. • ༗ߟྀաۭࣈ۲త৘گ䆩ʁṜੋ໘ࢼ࣌త޷໰୊ɻࡏ Ṝཫզ၇ఆٛଞੋճจɻ
  4. ୊໨ࠎՍ class Solution { func isPalindrome(s: String) -> Bool {

    }
 } let s = Solution() s.isPalindrome("A man, a plan, a canal: Panama")
  5. ೺ॴ༗తࣈ౎Ꮣখሜ class Solution { func isPalindrome(s: String) -> Bool {

    let lowerString = s.lowercaseString }
 }
 let s = Solution() s.isPalindrome("A man, a plan, a canal: Panama")
  6. ୞ཹԼӳᏐࣈ class Solution { func isPalindrome(s: String) -> Bool {

    let lowerString = s.lowercaseString var chars:[Character] = [] for c in lowerString.characters { if (c >= Character("a") && c <= Character("z")) || (c >= Character("0") && c <= Character("9")) { chars.append(c) } } }
 }
 let s = Solution() s.isPalindrome("A man, a plan, a canal: Panama")
  7. ᒾҰ࿨൓աိ༗ᔒ༗૬౳ class Solution { func isPalindrome(s: String) -> Bool {

    let lowerString = s.lowercaseString var chars:[Character] = [] for c in lowerString.characters { if (c >= Character("a") && c <= Character("z")) || (c >= Character("0") && c <= Character("9")) { chars.append(c) } } for idx in 0..<chars.count { if chars[idx] != chars[chars.count-idx-1] { return false } } return true }
 }
 let s = Solution() s.isPalindrome("A man, a plan, a canal: Panama")
  8. ՄҎ༻filter ိ႔ཧӳᏐࣈ class Solution { func isPalindrome(s: String) -> Bool

    { let chars = s.lowercaseString.characters.filter { ($0 >= Character("a") && $0 <= Character("z")) || ($0 >= Character("0") && $0 <= Character("9")) } for idx in 0..<chars.count { if chars[idx] != chars[chars.count-idx-1] { return false } } return true }
 }
 let s = Solution() s.isPalindrome("A man, a plan, a canal: Panama")
  9. ՄҎ௚઀reverse ޙᒾҰ૬౳ class Solution { func isPalindrome(s: String) -> Bool

    { let chars = s.lowercaseString.characters.filter { ($0 >= Character("a") && $0 <= Character("z")) || ($0 >= Character("0") && $0 <= Character("9")) } return chars.reverse() == chars }
 }
 let s = Solution() s.isPalindrome("A man, a plan, a canal: Panama")
  10. ࢖༻ਖ਼نදࣔࣜ func isPalindrome2(s: String) -> Bool { let reg =

    try? NSRegularExpression(pattern: "[^a-z0-9]", options: NSRegularExpressionOptions.CaseInsensitive) let range = NSMakeRange(0, s.characters.count) let alphanums = reg!.stringByReplacingMatchesInString(s, options: NSMatchingOptions(rawValue:0), range: range, withTemplate: "").lowercaseString let rev = alphanums.characters.reverse() for (a,b) in zip(alphanums.characters, rev) { if a != b { return false} } return true }