mirror of
https://github.com/krahets/hello-algo.git
synced 2025-12-19 07:17:54 +08:00
Update worst_best_time_complexity,
leetcode_two_sum
This commit is contained in:
@@ -4,7 +4,7 @@
|
||||
|
||||
package chapter_computational_complexity
|
||||
|
||||
// twoSumBruteForce
|
||||
/* 方法一:暴力枚举 */
|
||||
func twoSumBruteForce(nums []int, target int) []int {
|
||||
size := len(nums)
|
||||
// 两层循环,时间复杂度 O(n^2)
|
||||
@@ -18,7 +18,7 @@ func twoSumBruteForce(nums []int, target int) []int {
|
||||
return nil
|
||||
}
|
||||
|
||||
// twoSumHashTable
|
||||
/* 方法二:辅助哈希表 */
|
||||
func twoSumHashTable(nums []int, target int) []int {
|
||||
// 辅助哈希表,空间复杂度 O(n)
|
||||
hashTable := map[int]int{}
|
||||
|
||||
@@ -26,6 +26,8 @@ func randomNumbers(n int) []int {
|
||||
/* 查找数组 nums 中数字 1 所在索引 */
|
||||
func findOne(nums []int) int {
|
||||
for i := 0; i < len(nums); i++ {
|
||||
// 当元素 1 在数组头部时,达到最佳时间复杂度 O(1)
|
||||
// 当元素 1 在数组尾部时,达到最差时间复杂度 O(n)
|
||||
if nums[i] == 1 {
|
||||
return i
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user