Skip to content

Commit

Permalink
finished 14, 540
Browse files Browse the repository at this point in the history
  • Loading branch information
aelam committed May 10, 2017
1 parent 3361d84 commit e63afec
Show file tree
Hide file tree
Showing 3 changed files with 300 additions and 0 deletions.
83 changes: 83 additions & 0 deletions Algorithms/14. Longest Common Prefix/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
//
// main.swift
// 14. Longest Common Prefix
//
// Created by ryan on 10/05/2017.
// Copyright © 2017 ryan. All rights reserved.
//

class Solution {
// func longestCommonPrefix(_ strs: [String]) -> String {
// let totalCount = strs.count
// if totalCount == 0 {
// return ""
// }
//
// let firstString = strs[0]
//
// var commonPrefixEndIndex = firstString.characters.count
// var index = 0
// while commonPrefixEndIndex > index {
// var qualifiedC: Character? = nil
// for i in 0..<totalCount {
// let str = strs[i]
//
// let currentCount = str.characters.count
// commonPrefixEndIndex = min(commonPrefixEndIndex, currentCount)
// if index >= currentCount {
// qualifiedC = nil
// break
// }
//
// let strC = str[str.index(str.startIndex, offsetBy: index)]
//
// if i == 0 && qualifiedC == nil {
// qualifiedC = strC
// } else if qualifiedC != strC {
// commonPrefixEndIndex = index
// break
// }
// }
//
// if qualifiedC == nil {
// commonPrefixEndIndex = index
// break
// }
//
// index += 1
// }
//
// return firstString[firstString.startIndex..<firstString.index(firstString.startIndex, offsetBy: commonPrefixEndIndex)]
// }

func longestCommonPrefix(_ strs: [String]) -> String {
if strs.isEmpty {
return ""
}

var commonPrefix = strs[0]
for i in 1..<strs.count {
let comparingStr = strs[i]
if comparingStr.characters.count < commonPrefix.characters.count {
commonPrefix = commonPrefix[commonPrefix.startIndex..<commonPrefix.index(commonPrefix.startIndex, offsetBy: comparingStr.characters.count)]
}

if commonPrefix == "" {
return commonPrefix
}

while !comparingStr.hasPrefix(commonPrefix) && commonPrefix != "" {
commonPrefix = commonPrefix[commonPrefix.startIndex..<commonPrefix.index(commonPrefix.startIndex, offsetBy: commonPrefix.characters.count - 1)]
}
}

return commonPrefix
}
}

let solution = Solution()

//print(solution.longestCommonPrefix(["abab","aba",""]))
//print(solution.longestCommonPrefix(["aca","cba"]))
print(solution.longestCommonPrefix(["a","b"]))

40 changes: 40 additions & 0 deletions Algorithms/540. Single Element in a Sorted Array/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// main.swift
// 540. Single Element in a Sorted Array
//
// Created by ryan on 10/05/2017.
// Copyright © 2017 ryan. All rights reserved.
//

class Solution {
func singleNonDuplicate(_ nums: [Int]) -> Int {

let low = 0
let high = nums.count/2

let index = singleNonDuplicate(nums, low: low, high: high)
return nums[index]
}

func singleNonDuplicate(_ nums: [Int], low: Int, high: Int) -> Int {
let mid = (low + high)/2
if low < high {
if nums[2 * mid] != nums[2 * mid + 1] {
return singleNonDuplicate(nums, low: low, high: mid)
} else {
return singleNonDuplicate(nums, low: mid + 1, high: high)
}
}

return 2 * low
}

}


let solution = Solution()

//let array = [1,1,2,3,3,4,4,8,8]
//let array = [1,1,2]
let array = [1,1,2,3,3,4,4,8,8]
print(solution.singleNonDuplicate(array))
Loading

0 comments on commit e63afec

Please sign in to comment.