mirror of
https://github.com/krahets/hello-algo.git
synced 2025-11-02 12:58:42 +08:00
Update worst_best_time_complexity,
leetcode_two_sum
This commit is contained in:
@ -4,6 +4,7 @@
|
||||
* Author: nuomi1 (nuomi1@qq.com)
|
||||
*/
|
||||
|
||||
/* 方法一:暴力枚举 */
|
||||
func twoSumBruteForce(nums: [Int], target: Int) -> [Int] {
|
||||
// 两层循环,时间复杂度 O(n^2)
|
||||
for i in nums.indices.dropLast() {
|
||||
@ -16,6 +17,7 @@ func twoSumBruteForce(nums: [Int], target: Int) -> [Int] {
|
||||
return [0]
|
||||
}
|
||||
|
||||
/* 方法二:辅助哈希表 */
|
||||
func twoSumHashTable(nums: [Int], target: Int) -> [Int] {
|
||||
// 辅助哈希表,空间复杂度 O(n)
|
||||
var dic: [Int: Int] = [:]
|
||||
|
||||
@ -16,6 +16,8 @@ func randomNumbers(n: Int) -> [Int] {
|
||||
/* 查找数组 nums 中数字 1 所在索引 */
|
||||
func findOne(nums: [Int]) -> Int {
|
||||
for i in nums.indices {
|
||||
// 当元素 1 在数组头部时,达到最佳时间复杂度 O(1)
|
||||
// 当元素 1 在数组尾部时,达到最差时间复杂度 O(n)
|
||||
if nums[i] == 1 {
|
||||
return i
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user