Update worst_best_time_complexity,

leetcode_two_sum
This commit is contained in:
Yudong Jin
2023-02-03 18:53:15 +08:00
parent 592965595e
commit 70dead5cd0
24 changed files with 63 additions and 121 deletions

View File

@ -6,7 +6,7 @@
#include "../include/include.h"
/* 暴力解法 */
/* 方法一:暴力枚举 */
int *twoSumBruteForce(int *nums, int numsSize, int target, int *returnSize) {
for (int i = 0; i < numsSize; ++i) {
for (int j = i + 1; j < numsSize; ++j) {
@ -49,7 +49,7 @@ void insert(hashTable *h, int key, int val) {
}
}
/* 方法二:辅助哈希表 */
int *twoSumHashTable(int *nums, int numsSize, int target, int *returnSize) {
hashTable *hashtable = NULL;
for (int i = 0; i < numsSize; i++) {

View File

@ -27,6 +27,8 @@ int *randomNumbers(int n) {
/* 查找数组 nums 中数字 1 所在索引 */
int findOne(int *nums, int n) {
for (int i = 0; i < n; i++) {
// 当元素 1 在数组头部时,达到最佳时间复杂度 O(1)
// 当元素 1 在数组尾部时,达到最差时间复杂度 O(n)
if (nums[i] == 1) return i;
}
return -1;