-
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.
- Loading branch information
Showing
3 changed files
with
300 additions
and
0 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,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
40
Algorithms/540. Single Element in a Sorted Array/main.swift
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,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)) |
Oops, something went wrong.